Skip to content

Commit 1b71c64

Browse files
committed
test: add test for callback onClose
Ref: ENG-483
1 parent b0ffa92 commit 1b71c64

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

src/tests/notification.test.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,4 +674,71 @@ describe('notification accessibility behavior', () => {
674674
expect(screen.getByText('Foo notification')).not.toBeNull();
675675
expect(screen.getByText('Share notification')).not.toBeNull();
676676
});
677+
678+
it('calls onClose callback when timer expires', async () => {
679+
const onCloseMock = vi.fn();
680+
681+
notification.show({
682+
message: 'Notification with onClose',
683+
status: 'info',
684+
hasTimer: true,
685+
onClose: onCloseMock,
686+
});
687+
688+
expect(onCloseMock).not.toHaveBeenCalled();
689+
690+
await vi.advanceTimersByTimeAsync(notification.notificationTimeout);
691+
692+
expect(onCloseMock).toHaveBeenCalledTimes(1);
693+
});
694+
695+
it('calls onClose callback when closed manually', async () => {
696+
const onCloseMock = vi.fn();
697+
698+
const user = userEvent.setup({
699+
advanceTimers: vi.advanceTimersByTime,
700+
});
701+
702+
notification.show({
703+
message: 'Notification with onClose manual',
704+
status: 'info',
705+
hasTimer: true,
706+
onClose: onCloseMock,
707+
});
708+
709+
await user.click(screen.getByRole('button', { name: 'Meldung schließen', hidden: true }));
710+
711+
expect(onCloseMock).toHaveBeenCalledTimes(1);
712+
});
713+
714+
it('does not call onClose when notification is replaced by same group', async () => {
715+
const onCloseMock = vi.fn();
716+
717+
notification.show({
718+
group: 'test-group',
719+
message: 'First notification',
720+
status: 'info',
721+
hasTimer: true,
722+
onClose: onCloseMock,
723+
});
724+
725+
await vi.advanceTimersByTimeAsync(1000);
726+
727+
notification.show({
728+
group: 'test-group',
729+
message: 'Second notification',
730+
status: 'info',
731+
hasTimer: true,
732+
});
733+
734+
// First notification was replaced, onClose should not have been called
735+
expect(onCloseMock).not.toHaveBeenCalled();
736+
expect(screen.queryByText('First notification')).toBeNull();
737+
expect(screen.getByText('Second notification')).not.toBeNull();
738+
739+
await vi.advanceTimersByTimeAsync(notification.notificationTimeout);
740+
741+
// Still not called since first was replaced
742+
expect(onCloseMock).not.toHaveBeenCalled();
743+
});
677744
});

0 commit comments

Comments
 (0)