|
| 1 | +import type { NotivueConfig, NotivueConfigRequired, PushOptionsWithInternals } from 'notivue' |
| 2 | + |
| 3 | +import { describe, expect, test } from 'vitest' |
| 4 | + |
| 5 | +import { mergeNotificationOptions } from '@/core/utils' |
| 6 | + |
| 7 | +import { DEFAULT_NOTIFICATION_OPTIONS } from '@/core/constants' |
| 8 | + |
| 9 | +const baseConfig = DEFAULT_NOTIFICATION_OPTIONS as NotivueConfigRequired['notifications'] |
| 10 | + |
| 11 | +function merge<T extends Record<string, unknown> = Record<string, never>>( |
| 12 | + notifications: NotivueConfig['notifications'] = {}, |
| 13 | + push: PushOptionsWithInternals<T> |
| 14 | +) { |
| 15 | + return mergeNotificationOptions( |
| 16 | + { ...baseConfig, ...notifications } as NotivueConfigRequired['notifications'], |
| 17 | + push |
| 18 | + ) |
| 19 | +} |
| 20 | + |
| 21 | +describe('mergeNotificationOptions', () => { |
| 22 | + test('Per-type options override global config', () => { |
| 23 | + const result = merge( |
| 24 | + { |
| 25 | + global: { title: 'global', duration: 1000 }, |
| 26 | + success: { title: 'typed', duration: 2000 }, |
| 27 | + }, |
| 28 | + { id: '1', type: 'success' } |
| 29 | + ) |
| 30 | + |
| 31 | + expect(result.title).toBe('typed') |
| 32 | + expect(result.duration).toBe(2000) |
| 33 | + }) |
| 34 | + |
| 35 | + test('Push options override config', () => { |
| 36 | + const result = merge( |
| 37 | + { |
| 38 | + global: { title: 'global', duration: 1000 }, |
| 39 | + success: { title: 'typed', duration: 2000 }, |
| 40 | + }, |
| 41 | + { id: '1', type: 'success', title: 'push', duration: 3000 } |
| 42 | + ) |
| 43 | + |
| 44 | + expect(result.title).toBe('push') |
| 45 | + expect(result.duration).toBe(3000) |
| 46 | + }) |
| 47 | + |
| 48 | + test('Loading notifications always use unlimited duration', () => { |
| 49 | + const result = merge( |
| 50 | + { |
| 51 | + global: { duration: 1000 }, |
| 52 | + promise: { duration: 5000 }, |
| 53 | + }, |
| 54 | + { id: '1', type: 'loading', duration: 3000 } |
| 55 | + ) |
| 56 | + |
| 57 | + expect(result.duration).toBe(-1) |
| 58 | + expect(result.type).toBe('loading') |
| 59 | + }) |
| 60 | + |
| 61 | + test('Legacy promise* config overrides canonical loading* defaults', () => { |
| 62 | + const result = merge( |
| 63 | + { |
| 64 | + 'promise-reject': { ariaLive: 'polite', ariaRole: 'status', title: 'legacy' }, |
| 65 | + }, |
| 66 | + { id: '1', type: 'loading-error' } |
| 67 | + ) |
| 68 | + |
| 69 | + expect(result.ariaLive).toBe('polite') |
| 70 | + expect(result.ariaRole).toBe('status') |
| 71 | + expect(result.title).toBe('legacy') |
| 72 | + }) |
| 73 | + |
| 74 | + test('Canonical loading* config applies when legacy alias is unset', () => { |
| 75 | + const result = merge( |
| 76 | + { |
| 77 | + 'loading-error': { ariaLive: 'polite', ariaRole: 'status', title: 'canonical' }, |
| 78 | + }, |
| 79 | + { id: '1', type: 'loading-error' } |
| 80 | + ) |
| 81 | + |
| 82 | + expect(result.ariaLive).toBe('polite') |
| 83 | + expect(result.title).toBe('canonical') |
| 84 | + }) |
| 85 | + |
| 86 | + test('Legacy promise* config overrides canonical loading* config when both are set', () => { |
| 87 | + const result = merge( |
| 88 | + { |
| 89 | + 'loading-error': { ariaLive: 'polite', ariaRole: 'status', title: 'canonical' }, |
| 90 | + 'promise-reject': { ariaLive: 'assertive', ariaRole: 'alert', title: 'legacy' }, |
| 91 | + }, |
| 92 | + { id: '1', type: 'loading-error' } |
| 93 | + ) |
| 94 | + |
| 95 | + expect(result.ariaLive).toBe('assertive') |
| 96 | + expect(result.title).toBe('legacy') |
| 97 | + }) |
| 98 | + |
| 99 | + test('Normalizes deprecated promise types to loading variants', () => { |
| 100 | + expect(merge({}, { id: '1', type: 'promise' }).type).toBe('loading') |
| 101 | + expect(merge({}, { id: '1', type: 'promise-resolve' }).type).toBe('loading-success') |
| 102 | + expect(merge({}, { id: '1', type: 'promise-reject' }).type).toBe('loading-error') |
| 103 | + }) |
| 104 | +}) |
0 commit comments