Skip to content

Commit

Permalink
♻️ [Earwurm] Rename keys event to library
Browse files Browse the repository at this point in the history
  • Loading branch information
beefchimi committed Dec 22, 2023
1 parent 2200439 commit 2e3534c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Earwurm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export class Earwurm extends EmittenCommon<ManagerEventMap> {
this._keys = newKeys;

if (!identicalKeys) {
this.emit('keys', newKeys, oldKeys);
this.emit('library', newKeys, oldKeys);
}
}

Expand Down
49 changes: 26 additions & 23 deletions src/tests/Earwurm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,19 @@ describe('Earwurm component', () => {
expect(mockManager.keys).toHaveLength(4);
});

it('emits `keys` event with new and old `keys`', async () => {
const spyKeys: ManagerEventMap['keys'] = vi.fn((_new, _old) => {});
it('emits `library` event with new and old `keys`', async () => {
const spyLibrary: ManagerEventMap['library'] = vi.fn((_new, _old) => {});

mockManager.on('keys', spyKeys);
expect(spyKeys).not.toBeCalled();
mockManager.on('library', spyLibrary);
expect(spyLibrary).not.toBeCalled();

mockManager.add(...mockEntries);
expect(spyKeys).toBeCalledWith(mockInitialKeys, []);
expect(spyKeys).toBeCalledTimes(1);
expect(spyLibrary).toBeCalledWith(mockInitialKeys, []);
expect(spyLibrary).toBeCalledTimes(1);

// Does not add/remove when both `id + path` are identical.
mockManager.add(mockEntries[0]);
expect(spyKeys).not.toBeCalledTimes(2);
expect(spyLibrary).not.toBeCalledTimes(2);

const mockUniqueEntry: LibraryEntry = {
id: 'Unique',
Expand All @@ -273,8 +273,8 @@ describe('Earwurm component', () => {
];

mockManager.add(...mockChangedEntries);
expect(spyKeys).toBeCalledTimes(2);
expect(spyKeys).toBeCalledWith(
expect(spyLibrary).toBeCalledTimes(2);
expect(spyLibrary).toBeCalledWith(
[...mockInitialKeys, mockUniqueEntry.id],
mockInitialKeys,
);
Expand All @@ -284,10 +284,13 @@ describe('Earwurm component', () => {
// Emits twice as an existing key is removed then re-added
// as a result of the `path` value changing.
mockManager.add({...mockUniqueEntry, path: 'changed'});
expect(spyKeys).toBeCalledTimes(4);
expect(spyLibrary).toBeCalledTimes(4);

expect(spyKeys).toBeCalledWith(mockInitialKeys, keysSnapshot);
expect(spyKeys).toHaveBeenLastCalledWith(keysSnapshot, mockInitialKeys);
expect(spyLibrary).toBeCalledWith(mockInitialKeys, keysSnapshot);
expect(spyLibrary).toHaveBeenLastCalledWith(
keysSnapshot,
mockInitialKeys,
);
});

// TODO: Figure out how best to read `fadeMs` and `request` from Stack.
Expand Down Expand Up @@ -328,17 +331,17 @@ describe('Earwurm component', () => {
expect(capturedKeys).toStrictEqual([]);
});

it('emits `keys` event with new and old `keys`', async () => {
const spyKeys: ManagerEventMap['keys'] = vi.fn((_new, _old) => {});
it('emits `library` event with new and old `keys`', async () => {
const spyLibrary: ManagerEventMap['library'] = vi.fn((_new, _old) => {});

mockManager.add(...mockEntries);
mockManager.on('keys', spyKeys);
mockManager.on('library', spyLibrary);

mockManager.remove('Foo', 'Bar');
expect(spyKeys).not.toBeCalled();
expect(spyLibrary).not.toBeCalled();

mockManager.remove(mockEntries[1].id);
expect(spyKeys).toBeCalledWith(
expect(spyLibrary).toBeCalledWith(
[mockEntries[0].id, mockEntries[2].id],
mockInitialKeys,
);
Expand Down Expand Up @@ -447,16 +450,16 @@ describe('Earwurm component', () => {
expect(mockManager.keys).toStrictEqual([]);
});

it('emits `keys` event with empty array', async () => {
const spyKeys: ManagerEventMap['keys'] = vi.fn((_new, _old) => {});
it('emits `library` event with empty array', async () => {
const spyLibrary: ManagerEventMap['library'] = vi.fn((_new, _old) => {});

mockManager.add(...mockEntries);

mockManager.on('keys', spyKeys);
expect(spyKeys).not.toBeCalled();
mockManager.on('library', spyLibrary);
expect(spyLibrary).not.toBeCalled();

mockManager.teardown();
expect(spyKeys).toBeCalledWith([], mockInitialKeys);
expect(spyLibrary).toBeCalledWith([], mockInitialKeys);
});

it('does not resume the AudioContext', async () => {
Expand Down Expand Up @@ -685,6 +688,6 @@ describe('Earwurm component', () => {
});

// All events are covered in other tests:
// `state`, `keys`, `volume`, `mute`, and `error`.
// `state`, `library`, `volume`, `mute`, and `error`.
// describe('events', () => {});
});
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type ManagerState = AudioContextState | 'suspending' | 'interrupted';
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export type ManagerEventMap = {
state: (current: ManagerState) => void;
keys: (newKeys: StackId[], oldKeys: StackId[]) => void;
library: (newKeys: StackId[], oldKeys: StackId[]) => void;
volume: (level: number) => void;
mute: (muted: boolean) => void;
error: (messages: CombinedErrorMessage) => void;
Expand Down

0 comments on commit 2e3534c

Please sign in to comment.