Skip to content

Commit

Permalink
fix console manager test
Browse files Browse the repository at this point in the history
  • Loading branch information
eokoneyo committed Nov 8, 2024
1 parent 4f4f2d1 commit 6979ee6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,9 @@ export const ConsoleManager = memo<ConsoleManagerProps>(({ storage = {}, childre
validateIdOrThrow(id);

setConsoleStorage((prevState) => {
return {
...prevState,
[id]: {
...prevState[id],
isOpen: false,
},
};
const newState = { ...prevState };
newState[id].isOpen = false;
return newState;
});
},
[validateIdOrThrow] // << IMPORTANT: this callback should have only immutable dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,27 @@ describe('When using ConsoleManager', () => {
);
});

it('should hide a console by `id`', () => {
it('should hide a console by `id`', async () => {
renderHook();
const { id: consoleId } = registerNewConsole();

let consoleClient: ReturnType<ConsoleManagerClient['getOne']>;

act(() => {
consoleClient = renderResult.result.current.getOne(consoleId);
});

act(() => {
renderResult.result.current.show(consoleId);
});

expect(renderResult.result.current.getOne(consoleId)!.isVisible()).toBe(true);
await waitFor(() => expect(consoleClient!.isVisible()).toBe(true));

act(() => {
renderResult.result.current.hide(consoleId);
});

expect(renderResult.result.current.getOne(consoleId)!.isVisible()).toBe(false);
await waitFor(() => expect(consoleClient!.isVisible()).toBe(false));
});

it('should throw if attempting to hide a console with invalid `id`', () => {
Expand Down Expand Up @@ -163,7 +170,9 @@ describe('When using ConsoleManager', () => {
beforeEach(() => {
renderHook();
({ id: consoleId } = registerNewConsole());
registeredConsole = renderResult.result.current.getOne(consoleId)!;
act(() => {
registeredConsole = renderResult.result.current.getOne(consoleId)!;
});
});

it('should have the expected interface', () => {
Expand All @@ -178,20 +187,30 @@ describe('When using ConsoleManager', () => {
});

it('should display the console when `.show()` is called', async () => {
registeredConsole.show();
act(() => {
registeredConsole.show();
});
await waitFor(() => expect(registeredConsole.isVisible()).toBe(true));
});

it('should hide the console when `.hide()` is called', async () => {
registeredConsole.show();
act(() => {
registeredConsole.show();
});

await waitFor(() => expect(registeredConsole.isVisible()).toBe(true));

registeredConsole.hide();
act(() => {
registeredConsole.hide();
});

await waitFor(() => expect(registeredConsole.isVisible()).toBe(false));
});

it('should un-register the console when `.terminate() is called', async () => {
registeredConsole.terminate();
act(() => {
registeredConsole.terminate();
});
await waitFor(() => expect(renderResult.result.current.getOne(consoleId)).toBeUndefined());
});
});
Expand Down

0 comments on commit 6979ee6

Please sign in to comment.