|
| 1 | +import { EventEmitter } from 'node:events'; |
| 2 | +import { get } from 'svelte/store'; |
| 3 | +import { afterEach, beforeEach, describe, expect, it, Mock, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { initialize, LDClient } from '@launchdarkly/js-client-sdk/compat'; |
| 6 | + |
| 7 | +import { LD } from '../../../src/lib/client/SvelteLDClient'; |
| 8 | + |
| 9 | +vi.mock('@launchdarkly/js-client-sdk/compat', { spy: true }); |
| 10 | + |
| 11 | +const clientSideID = 'test-client-side-id'; |
| 12 | +const rawFlags = { 'test-flag': true, 'another-test-flag': 'flag-value' }; |
| 13 | +const mockContext = { key: 'user1' }; |
| 14 | + |
| 15 | +// used to mock ready and change events on the LDClient |
| 16 | +const mockLDEventEmitter = new EventEmitter(); |
| 17 | + |
| 18 | +const mockLDClient = { |
| 19 | + on: (e: string, cb: () => void) => mockLDEventEmitter.on(e, cb), |
| 20 | + off: vi.fn(), |
| 21 | + allFlags: vi.fn().mockReturnValue(rawFlags), |
| 22 | + variation: vi.fn((_, defaultValue) => defaultValue), |
| 23 | + identify: vi.fn(), |
| 24 | +}; |
| 25 | + |
| 26 | +describe('launchDarkly', () => { |
| 27 | + describe('createLD', () => { |
| 28 | + it('should create a LaunchDarkly instance with correct properties', () => { |
| 29 | + const ld = LD; |
| 30 | + expect(typeof ld).toBe('object'); |
| 31 | + expect(ld).toHaveProperty('identify'); |
| 32 | + expect(ld).toHaveProperty('flags'); |
| 33 | + expect(ld).toHaveProperty('initialize'); |
| 34 | + expect(ld).toHaveProperty('initializing'); |
| 35 | + expect(ld).toHaveProperty('watch'); |
| 36 | + expect(ld).toHaveProperty('useFlag'); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('initialize', async () => { |
| 40 | + const ld = LD; |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + // mocks the initialize function to return the mockLDClient |
| 44 | + (initialize as Mock<typeof initialize>).mockReturnValue( |
| 45 | + mockLDClient as unknown as LDClient, |
| 46 | + ); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + vi.clearAllMocks(); |
| 51 | + mockLDEventEmitter.removeAllListeners(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should throw an error if the client is not initialized', async () => { |
| 55 | + const flagKey = 'test-flag'; |
| 56 | + const user = { key: 'user1' }; |
| 57 | + |
| 58 | + expect(() => ld.useFlag(flagKey, true)).toThrow('LaunchDarkly client not initialized'); |
| 59 | + await expect(() => ld.identify(user)).rejects.toThrow( |
| 60 | + 'LaunchDarkly client not initialized', |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should set the loading status to false when the client is ready', async () => { |
| 65 | + const { initializing } = ld; |
| 66 | + ld.initialize(clientSideID, mockContext); |
| 67 | + |
| 68 | + expect(get(initializing)).toBe(true); // should be true before the ready event is emitted |
| 69 | + mockLDEventEmitter.emit('ready'); |
| 70 | + |
| 71 | + expect(get(initializing)).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should initialize the LaunchDarkly SDK instance', () => { |
| 75 | + ld.initialize(clientSideID, mockContext); |
| 76 | + |
| 77 | + expect(initialize).toHaveBeenCalledWith('test-client-side-id', mockContext); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should register function that gets flag values when client is ready', () => { |
| 81 | + const newFlags = { ...rawFlags, 'new-flag': true }; |
| 82 | + const allFlagsSpy = vi.spyOn(mockLDClient, 'allFlags').mockReturnValue(newFlags); |
| 83 | + |
| 84 | + ld.initialize(clientSideID, mockContext); |
| 85 | + mockLDEventEmitter.emit('ready'); |
| 86 | + |
| 87 | + expect(allFlagsSpy).toHaveBeenCalledOnce(); |
| 88 | + expect(allFlagsSpy).toHaveReturnedWith(newFlags); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should register function that gets flag values when flags changed', () => { |
| 92 | + const changedFlags = { ...rawFlags, 'changed-flag': true }; |
| 93 | + const allFlagsSpy = vi.spyOn(mockLDClient, 'allFlags').mockReturnValue(changedFlags); |
| 94 | + |
| 95 | + ld.initialize(clientSideID, mockContext); |
| 96 | + mockLDEventEmitter.emit('change'); |
| 97 | + |
| 98 | + expect(allFlagsSpy).toHaveBeenCalledOnce(); |
| 99 | + expect(allFlagsSpy).toHaveReturnedWith(changedFlags); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('watch function', () => { |
| 104 | + const ld = LD; |
| 105 | + |
| 106 | + beforeEach(() => { |
| 107 | + // mocks the initialize function to return the mockLDClient |
| 108 | + (initialize as Mock<typeof initialize>).mockReturnValue( |
| 109 | + mockLDClient as unknown as LDClient, |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + afterEach(() => { |
| 114 | + vi.clearAllMocks(); |
| 115 | + mockLDEventEmitter.removeAllListeners(); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should return a derived store that reflects the value of the specified flag', () => { |
| 119 | + const flagKey = 'test-flag'; |
| 120 | + ld.initialize(clientSideID, mockContext); |
| 121 | + |
| 122 | + const flagStore = ld.watch(flagKey); |
| 123 | + |
| 124 | + expect(get(flagStore)).toBe(true); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should update the flag store when the flag value changes', () => { |
| 128 | + const booleanFlagKey = 'test-flag'; |
| 129 | + const stringFlagKey = 'another-test-flag'; |
| 130 | + ld.initialize(clientSideID, mockContext); |
| 131 | + const flagStore = ld.watch(booleanFlagKey); |
| 132 | + const flagStore2 = ld.watch(stringFlagKey); |
| 133 | + |
| 134 | + // emit ready event to set initial flag values |
| 135 | + mockLDEventEmitter.emit('ready'); |
| 136 | + |
| 137 | + // 'test-flag' initial value is true according to `rawFlags` |
| 138 | + expect(get(flagStore)).toBe(true); |
| 139 | + // 'another-test-flag' intial value is 'flag-value' according to `rawFlags` |
| 140 | + expect(get(flagStore2)).toBe('flag-value'); |
| 141 | + |
| 142 | + mockLDClient.allFlags.mockReturnValue({ |
| 143 | + ...rawFlags, |
| 144 | + 'test-flag': false, |
| 145 | + 'another-test-flag': 'new-flag-value', |
| 146 | + }); |
| 147 | + |
| 148 | + // dispatch a change event on ldClient |
| 149 | + mockLDEventEmitter.emit('change'); |
| 150 | + |
| 151 | + expect(get(flagStore)).toBe(false); |
| 152 | + expect(get(flagStore2)).toBe('new-flag-value'); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should return undefined if the flag is not found', () => { |
| 156 | + const flagKey = 'non-existent-flag'; |
| 157 | + ld.initialize(clientSideID, mockContext); |
| 158 | + |
| 159 | + const flagStore = ld.watch(flagKey); |
| 160 | + |
| 161 | + expect(get(flagStore)).toBeUndefined(); |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + describe('useFlag function', () => { |
| 166 | + const ld = LD; |
| 167 | + |
| 168 | + beforeEach(() => { |
| 169 | + // mocks the initialize function to return the mockLDClient |
| 170 | + (initialize as Mock<typeof initialize>).mockReturnValue( |
| 171 | + mockLDClient as unknown as LDClient, |
| 172 | + ); |
| 173 | + }); |
| 174 | + |
| 175 | + afterEach(() => { |
| 176 | + vi.clearAllMocks(); |
| 177 | + mockLDEventEmitter.removeAllListeners(); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should return flag value', () => { |
| 181 | + mockLDClient.variation.mockReturnValue(true); |
| 182 | + const flagKey = 'test-flag'; |
| 183 | + ld.initialize(clientSideID, mockContext); |
| 184 | + |
| 185 | + expect(ld.useFlag(flagKey, false)).toBe(true); |
| 186 | + expect(mockLDClient.variation).toHaveBeenCalledWith(flagKey, false); |
| 187 | + }); |
| 188 | + }); |
| 189 | + |
| 190 | + describe('identify function', () => { |
| 191 | + const ld = LD; |
| 192 | + |
| 193 | + beforeEach(() => { |
| 194 | + // mocks the initialize function to return the mockLDClient |
| 195 | + (initialize as Mock<typeof initialize>).mockReturnValue( |
| 196 | + mockLDClient as unknown as LDClient, |
| 197 | + ); |
| 198 | + }); |
| 199 | + |
| 200 | + afterEach(() => { |
| 201 | + vi.clearAllMocks(); |
| 202 | + mockLDEventEmitter.removeAllListeners(); |
| 203 | + }); |
| 204 | + |
| 205 | + it('should call the identify method on the LaunchDarkly client', () => { |
| 206 | + const user = { key: 'user1' }; |
| 207 | + ld.initialize(clientSideID, user); |
| 208 | + |
| 209 | + ld.identify(user); |
| 210 | + |
| 211 | + expect(mockLDClient.identify).toHaveBeenCalledWith(user); |
| 212 | + }); |
| 213 | + }); |
| 214 | + }); |
| 215 | +}); |
0 commit comments