|
| 1 | +import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | + |
| 3 | +describe('common notify helpers', () => { |
| 4 | + beforeEach(() => { |
| 5 | + vi.useFakeTimers(); |
| 6 | + }); |
| 7 | + |
| 8 | + afterEach(() => { |
| 9 | + vi.useRealTimers(); |
| 10 | + }); |
| 11 | + |
| 12 | + it('handles unlimited stacked notifications', () => { |
| 13 | + const calls: Array<{ index: number; data: string }> = []; |
| 14 | + const notify = createStackedNotify<string>((index, data, close) => { |
| 15 | + calls.push({ index, data }); |
| 16 | + close(); |
| 17 | + }); |
| 18 | + |
| 19 | + notify('first'); |
| 20 | + vi.runOnlyPendingTimers(); |
| 21 | + notify('second'); |
| 22 | + vi.runOnlyPendingTimers(); |
| 23 | + |
| 24 | + expect(calls).toEqual([ |
| 25 | + { index: 0, data: 'first' }, |
| 26 | + { index: 0, data: 'second' }, |
| 27 | + ]); |
| 28 | + }); |
| 29 | + |
| 30 | + it('queues stacked notifications when all slots are busy', () => { |
| 31 | + const calls: string[] = []; |
| 32 | + const closers: Array<() => void> = []; |
| 33 | + const notify = createStackedNotify<string>((index, data, close) => { |
| 34 | + calls.push(`${index}:${data}`); |
| 35 | + closers.push(() => close()); |
| 36 | + }, 2); |
| 37 | + |
| 38 | + for (let i = 0; i < 5; i++) { |
| 39 | + notify(`job-${i}`); |
| 40 | + } |
| 41 | + |
| 42 | + notify('queued'); |
| 43 | + expect(calls).toHaveLength(5); |
| 44 | + |
| 45 | + closers[0]!(); |
| 46 | + vi.runOnlyPendingTimers(); |
| 47 | + |
| 48 | + expect(calls).toHaveLength(6); |
| 49 | + expect(calls.at(-1)).toMatch(/\d:queued/); |
| 50 | + }); |
| 51 | + |
| 52 | + it('processes notifications sequentially', () => { |
| 53 | + const calls: string[] = []; |
| 54 | + const notify = createSingledNotify<string>((data, close) => { |
| 55 | + calls.push(data); |
| 56 | + close(); |
| 57 | + }); |
| 58 | + |
| 59 | + notify('a'); |
| 60 | + notify('b'); |
| 61 | + |
| 62 | + expect(calls).toEqual(['a']); |
| 63 | + vi.runOnlyPendingTimers(); |
| 64 | + expect(calls).toEqual(['a', 'b']); |
| 65 | + }); |
| 66 | + |
| 67 | + it('toggles refs and callbacks correctly', () => { |
| 68 | + const modal = ref(false); |
| 69 | + const callback = vi.fn<(open: boolean, payload?: string) => void>(); |
| 70 | + const optional = vi.fn<(open: boolean, payload?: number) => void>(); |
| 71 | + |
| 72 | + const toggled = createToggledNotify<{ |
| 73 | + modal: typeof modal; |
| 74 | + callback: (open: boolean, payload?: string) => void; |
| 75 | + optional: (open: boolean, payload?: number) => void; |
| 76 | + }>(); |
| 77 | + |
| 78 | + toggled.init('modal', modal); |
| 79 | + toggled.init('callback', callback); |
| 80 | + toggled.init('optional', optional, true); |
| 81 | + |
| 82 | + toggled.open('modal'); |
| 83 | + expect(modal.value).toBe(true); |
| 84 | + |
| 85 | + toggled.toggle('modal'); |
| 86 | + expect(modal.value).toBe(false); |
| 87 | + |
| 88 | + toggled.openExclusive('callback', 'payload'); |
| 89 | + expect(callback).toHaveBeenLastCalledWith(true, 'payload'); |
| 90 | + |
| 91 | + toggled.open('optional', 42); |
| 92 | + expect(optional).toHaveBeenLastCalledWith(true, 42); |
| 93 | + |
| 94 | + toggled.closeAll(); |
| 95 | + expect(callback).toHaveBeenLastCalledWith(false, undefined); |
| 96 | + expect(optional).toHaveBeenLastCalledWith(false, undefined); |
| 97 | + }); |
| 98 | + |
| 99 | + it('creates app notify with default warning level', () => { |
| 100 | + const { eventBus, newThrown } = createAppNotify('toast', GlobalNotifyStyle.Toast); |
| 101 | + |
| 102 | + const notifyFromString = newThrown('be careful'); |
| 103 | + expect(notifyFromString).toBeInstanceOf(NotifyThrown); |
| 104 | + expect(notifyFromString.notify).toMatchObject({ |
| 105 | + message: 'be careful', |
| 106 | + notifyLevel: GlobalNotifyLevel.Warning, |
| 107 | + notifyStyle: GlobalNotifyStyle.Toast, |
| 108 | + }); |
| 109 | + |
| 110 | + const notifyFromObject = newThrown({ |
| 111 | + message: 'ok', |
| 112 | + notifyLevel: GlobalNotifyLevel.Success, |
| 113 | + }); |
| 114 | + expect(notifyFromObject.notify).toMatchObject({ |
| 115 | + message: 'ok', |
| 116 | + notifyLevel: GlobalNotifyLevel.Success, |
| 117 | + notifyStyle: GlobalNotifyStyle.Toast, |
| 118 | + }); |
| 119 | + |
| 120 | + expect(typeof eventBus.emit).toBe('function'); |
| 121 | + expect(typeof eventBus.on).toBe('function'); |
| 122 | + }); |
| 123 | +}); |
0 commit comments