|
| 1 | +import { EVENT_SAMPLING_RATE, EVENT_THEME_USAGE, eventThemeUsage } from '../theme-usage'; |
| 2 | + |
| 3 | +describe('eventThemeUsage', () => { |
| 4 | + it('should create telemetry event with shadcn theme name', () => { |
| 5 | + const appearance = { |
| 6 | + theme: { |
| 7 | + __type: 'prebuilt_appearance' as const, |
| 8 | + name: 'shadcn', |
| 9 | + variables: { colorPrimary: 'var(--primary)' }, |
| 10 | + }, |
| 11 | + }; |
| 12 | + |
| 13 | + const result = eventThemeUsage(appearance); |
| 14 | + |
| 15 | + expect(result).toEqual({ |
| 16 | + event: EVENT_THEME_USAGE, |
| 17 | + eventSamplingRate: EVENT_SAMPLING_RATE, |
| 18 | + payload: { themeName: 'shadcn' }, |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should handle string themes', () => { |
| 23 | + const appearance = { |
| 24 | + theme: 'clerk' as any, // String themes are valid at runtime |
| 25 | + }; |
| 26 | + |
| 27 | + const result = eventThemeUsage(appearance); |
| 28 | + |
| 29 | + expect(result).toEqual({ |
| 30 | + event: EVENT_THEME_USAGE, |
| 31 | + eventSamplingRate: EVENT_SAMPLING_RATE, |
| 32 | + payload: { themeName: 'clerk' }, |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should handle array of themes', () => { |
| 37 | + const appearance = { |
| 38 | + theme: [ |
| 39 | + 'clerk' as any, // String themes are valid at runtime |
| 40 | + { |
| 41 | + __type: 'prebuilt_appearance' as const, |
| 42 | + name: 'shadcn', |
| 43 | + }, |
| 44 | + ] as any, |
| 45 | + }; |
| 46 | + |
| 47 | + const result = eventThemeUsage(appearance); |
| 48 | + |
| 49 | + expect(result).toEqual({ |
| 50 | + event: EVENT_THEME_USAGE, |
| 51 | + eventSamplingRate: EVENT_SAMPLING_RATE, |
| 52 | + payload: { themeName: 'clerk' }, |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should handle themes without explicit names', () => { |
| 57 | + const appearance = { |
| 58 | + theme: { |
| 59 | + __type: 'prebuilt_appearance' as const, |
| 60 | + variables: { colorPrimary: 'blue' }, |
| 61 | + }, |
| 62 | + }; |
| 63 | + |
| 64 | + const result = eventThemeUsage(appearance); |
| 65 | + |
| 66 | + expect(result).toEqual({ |
| 67 | + event: EVENT_THEME_USAGE, |
| 68 | + eventSamplingRate: EVENT_SAMPLING_RATE, |
| 69 | + payload: { themeName: undefined }, |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should prioritize theme over deprecated baseTheme', () => { |
| 74 | + const appearance = { |
| 75 | + theme: 'clerk' as any, // String themes are valid at runtime |
| 76 | + baseTheme: { |
| 77 | + __type: 'prebuilt_appearance' as const, |
| 78 | + name: 'shadcn', |
| 79 | + }, |
| 80 | + }; |
| 81 | + |
| 82 | + const result = eventThemeUsage(appearance); |
| 83 | + |
| 84 | + expect(result).toEqual({ |
| 85 | + event: EVENT_THEME_USAGE, |
| 86 | + eventSamplingRate: EVENT_SAMPLING_RATE, |
| 87 | + payload: { themeName: 'clerk' }, |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should use baseTheme when theme is not provided', () => { |
| 92 | + const appearance = { |
| 93 | + baseTheme: { |
| 94 | + __type: 'prebuilt_appearance' as const, |
| 95 | + name: 'shadcn', |
| 96 | + }, |
| 97 | + }; |
| 98 | + |
| 99 | + const result = eventThemeUsage(appearance); |
| 100 | + |
| 101 | + expect(result).toEqual({ |
| 102 | + event: EVENT_THEME_USAGE, |
| 103 | + eventSamplingRate: EVENT_SAMPLING_RATE, |
| 104 | + payload: { themeName: 'shadcn' }, |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should handle undefined appearance', () => { |
| 109 | + const result = eventThemeUsage(); |
| 110 | + |
| 111 | + expect(result).toEqual({ |
| 112 | + event: EVENT_THEME_USAGE, |
| 113 | + eventSamplingRate: EVENT_SAMPLING_RATE, |
| 114 | + payload: {}, |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should handle null appearance', () => { |
| 119 | + const result = eventThemeUsage(null as any); |
| 120 | + |
| 121 | + expect(result).toEqual({ |
| 122 | + event: EVENT_THEME_USAGE, |
| 123 | + eventSamplingRate: EVENT_SAMPLING_RATE, |
| 124 | + payload: {}, |
| 125 | + }); |
| 126 | + }); |
| 127 | +}); |
0 commit comments