Skip to content

Commit

Permalink
fix: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
myandrienko committed Dec 18, 2024
1 parent 0fb8703 commit 25022d9
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/components/Dialog/__tests__/DialogsManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ describe('DialogManager', () => {
const dialogManager = new DialogManager({ id });
expect(dialogManager.id).toBe(id);
});

it('initiates with default options', () => {
const mockedId = '12345';
const spy = jest.spyOn(Date.prototype, 'getTime').mockReturnValueOnce(mockedId);
const dialogManager = new DialogManager();
expect(dialogManager.id).toBe(mockedId);
spy.mockRestore();
});

it('creates a new closed dialog', () => {
const dialogManager = new DialogManager();
expect(Object.keys(dialogManager.state.getLatestValue().dialogsById)).toHaveLength(0);
Expand Down Expand Up @@ -142,4 +144,33 @@ describe('DialogManager', () => {
expect(dialogManager.openDialogCount).toBe(1);
expect(Object.keys(dialogManager.state.getLatestValue().dialogsById)).toHaveLength(1);
});

it('marks dialog for removal', () => {
jest.useFakeTimers();

const dialogManager = new DialogManager();
dialogManager.getOrCreate({ id: dialogId });
dialogManager.open({ id: dialogId });
dialogManager.markForRemoval(dialogId);

jest.runAllTimers();

expect(dialogManager.openDialogCount).toBe(0);
expect(Object.keys(dialogManager.state.getLatestValue().dialogsById)).toHaveLength(0);
});

it('cancels dialog removal if it is referenced again quickly', () => {
jest.useFakeTimers();

const dialogManager = new DialogManager();
dialogManager.getOrCreate({ id: dialogId });
dialogManager.open({ id: dialogId });
dialogManager.markForRemoval(dialogId);
dialogManager.getOrCreate({ id: dialogId });

jest.runAllTimers();

expect(dialogManager.openDialogCount).toBe(1);
expect(Object.keys(dialogManager.state.getLatestValue().dialogsById)).toHaveLength(1);
});
});

0 comments on commit 25022d9

Please sign in to comment.