Fix memory leaks via auto-unsubscribing and DI
This commit simplifies event handling, providing a unified and robust
way to handle event lifecycling. This way, it fixes events not being
unsubscribed when state is changed.
Introduce a new function in `EventSubscriptionCollection` to remove
existing events and adding new events. This provides an easier to use
API, which leads to code that's easier to understand. It also prevents
potential bugs that may occur due to forgetting to call both functions.
It fixes `TheScriptsMenu` not unregistering events on state change.
Other improvements include:
- Include a getter to get total amount of registered subcriptions.
This helps in unit testing.
- Have nullish checks to prevent potential errors further down the
execution.
- Use array instead of rest parameters to increase readability and
simplify tests.
Ensure `SliderHandler` stops resizes on unmount, unsubscribing from all
events and resetting state to default.
Update `injectionKeys` to do imports as types to avoid circular
dependencies. Simplify importing `injectionKeys` to enable and strict
typings for iterating injection keys.
Add tests covering new behavior.
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { shallowMount } from '@vue/test-utils';
|
||||
import { useAutoUnsubscribedEvents } from '@/presentation/components/Shared/Hooks/UseAutoUnsubscribedEvents';
|
||||
import { EventSubscriptionCollectionStub } from '@tests/unit/shared/Stubs/EventSubscriptionCollectionStub';
|
||||
import { EventSubscriptionStub } from '@tests/unit/shared/Stubs/EventSubscriptionStub';
|
||||
import { EventSubscriptionCollection } from '@/infrastructure/Events/EventSubscriptionCollection';
|
||||
import { FunctionKeys } from '@/TypeHelpers';
|
||||
|
||||
describe('UseAutoUnsubscribedEvents', () => {
|
||||
describe('event collection handling', () => {
|
||||
it('returns the provided event collection when initialized', () => {
|
||||
// arrange
|
||||
const expectedEvents = new EventSubscriptionCollectionStub();
|
||||
|
||||
// act
|
||||
const { events: actualEvents } = useAutoUnsubscribedEvents(expectedEvents);
|
||||
|
||||
// assert
|
||||
expect(actualEvents).to.equal(expectedEvents);
|
||||
});
|
||||
|
||||
it('uses a default event collection when none is provided during initialization', () => {
|
||||
// arrange
|
||||
const expectedType = EventSubscriptionCollection;
|
||||
|
||||
// act
|
||||
const { events: actualEvents } = useAutoUnsubscribedEvents();
|
||||
|
||||
// assert
|
||||
expect(actualEvents).to.be.instanceOf(expectedType);
|
||||
});
|
||||
|
||||
it('throws error when there are existing subscriptions', () => {
|
||||
// arrange
|
||||
const expectedError = 'there are existing subscriptions, this may lead to side-effects';
|
||||
const events = new EventSubscriptionCollectionStub();
|
||||
events.register([new EventSubscriptionStub(), new EventSubscriptionStub()]);
|
||||
|
||||
// act
|
||||
const act = () => useAutoUnsubscribedEvents(events);
|
||||
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
});
|
||||
describe('event unsubscription', () => {
|
||||
it('unsubscribes from all events when the associated component is destroyed', () => {
|
||||
// arrange
|
||||
const events = new EventSubscriptionCollectionStub();
|
||||
const expectedCall: FunctionKeys<EventSubscriptionCollection> = 'unsubscribeAll';
|
||||
const stubComponent = shallowMount({
|
||||
setup() {
|
||||
useAutoUnsubscribedEvents(events);
|
||||
events.register([new EventSubscriptionStub(), new EventSubscriptionStub()]);
|
||||
},
|
||||
template: '<div></div>',
|
||||
});
|
||||
events.callHistory.length = 0;
|
||||
|
||||
// act
|
||||
stubComponent.destroy();
|
||||
|
||||
// assert
|
||||
expect(events.callHistory).to.have.lengthOf(1);
|
||||
expect(events.callHistory[0].methodName).to.equal(expectedCall);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user