|
| 1 | +import { createNoopClient } from '../../src/client/createNoopClient'; |
| 2 | + |
| 3 | +// Ensure we're in an SSR-like environment (no window). |
| 4 | +const originalWindow = global.window; |
| 5 | +beforeAll(() => { |
| 6 | + // @ts-ignore |
| 7 | + delete global.window; |
| 8 | +}); |
| 9 | +afterAll(() => { |
| 10 | + // @ts-ignore |
| 11 | + global.window = originalWindow; |
| 12 | +}); |
| 13 | + |
| 14 | +const bootstrapData = { |
| 15 | + 'bool-flag': true, |
| 16 | + 'number-flag': 42, |
| 17 | + 'string-flag': 'hello', |
| 18 | + 'json-flag': { nested: true }, |
| 19 | + 'null-flag': null, |
| 20 | + $flagsState: { |
| 21 | + 'bool-flag': { variation: 0, version: 5 }, |
| 22 | + 'number-flag': { variation: 1, version: 3 }, |
| 23 | + 'string-flag': { variation: 2, version: 7 }, |
| 24 | + 'json-flag': { variation: 0, version: 1 }, |
| 25 | + 'null-flag': { variation: 1, version: 2 }, |
| 26 | + }, |
| 27 | + $valid: true, |
| 28 | +}; |
| 29 | + |
| 30 | +describe('given bootstrap data', () => { |
| 31 | + const client = createNoopClient(bootstrapData); |
| 32 | + |
| 33 | + it('returns boolean value when key exists and type matches', () => { |
| 34 | + expect(client.boolVariation('bool-flag', false)).toBe(true); |
| 35 | + }); |
| 36 | + |
| 37 | + it('returns default when boolean key exists but type mismatches', () => { |
| 38 | + expect(client.boolVariation('string-flag', false)).toBe(false); |
| 39 | + }); |
| 40 | + |
| 41 | + it('returns default when boolean key is missing', () => { |
| 42 | + expect(client.boolVariation('missing', true)).toBe(true); |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns number value when key exists and type matches', () => { |
| 46 | + expect(client.numberVariation('number-flag', 0)).toBe(42); |
| 47 | + }); |
| 48 | + |
| 49 | + it('returns default when number key exists but type mismatches', () => { |
| 50 | + expect(client.numberVariation('bool-flag', 99)).toBe(99); |
| 51 | + }); |
| 52 | + |
| 53 | + it('returns default when number key is missing', () => { |
| 54 | + expect(client.numberVariation('missing', 7)).toBe(7); |
| 55 | + }); |
| 56 | + |
| 57 | + it('returns string value when key exists and type matches', () => { |
| 58 | + expect(client.stringVariation('string-flag', 'fallback')).toBe('hello'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('returns default when string key exists but type mismatches', () => { |
| 62 | + expect(client.stringVariation('number-flag', 'fallback')).toBe('fallback'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('returns default when string key is missing', () => { |
| 66 | + expect(client.stringVariation('missing', 'fallback')).toBe('fallback'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('returns json value when key exists', () => { |
| 70 | + expect(client.jsonVariation('json-flag', null)).toEqual({ nested: true }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('returns json value even for null', () => { |
| 74 | + expect(client.jsonVariation('null-flag', 'default')).toBeNull(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('returns default when json key is missing', () => { |
| 78 | + expect(client.jsonVariation('missing', 'default')).toBe('default'); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe('detail variants return null variationIndex and reason', () => { |
| 83 | + const client = createNoopClient(bootstrapData); |
| 84 | + |
| 85 | + it('returns value with null variationIndex and reason for boolVariationDetail', () => { |
| 86 | + const detail = client.boolVariationDetail('bool-flag', false); |
| 87 | + expect(detail.value).toBe(true); |
| 88 | + expect(detail.variationIndex).toBeNull(); |
| 89 | + expect(detail.reason).toBeNull(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('returns value with null variationIndex and reason for numberVariationDetail', () => { |
| 93 | + const detail = client.numberVariationDetail('number-flag', 0); |
| 94 | + expect(detail.value).toBe(42); |
| 95 | + expect(detail.variationIndex).toBeNull(); |
| 96 | + expect(detail.reason).toBeNull(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('returns value with null variationIndex and reason for stringVariationDetail', () => { |
| 100 | + const detail = client.stringVariationDetail('string-flag', 'fallback'); |
| 101 | + expect(detail.value).toBe('hello'); |
| 102 | + expect(detail.variationIndex).toBeNull(); |
| 103 | + expect(detail.reason).toBeNull(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('returns value with null variationIndex and reason for jsonVariationDetail', () => { |
| 107 | + const detail = client.jsonVariationDetail('json-flag', null); |
| 108 | + expect(detail.value).toEqual({ nested: true }); |
| 109 | + expect(detail.variationIndex).toBeNull(); |
| 110 | + expect(detail.reason).toBeNull(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('returns default value with null variationIndex for missing key', () => { |
| 114 | + const detail = client.boolVariationDetail('missing', false); |
| 115 | + expect(detail.value).toBe(false); |
| 116 | + expect(detail.variationIndex).toBeNull(); |
| 117 | + expect(detail.reason).toBeNull(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('returns default value in detail when type mismatches', () => { |
| 121 | + const detail = client.boolVariationDetail('string-flag', false); |
| 122 | + expect(detail.value).toBe(false); |
| 123 | + expect(detail.variationIndex).toBeNull(); |
| 124 | + expect(detail.reason).toBeNull(); |
| 125 | + }); |
| 126 | +}); |
| 127 | + |
| 128 | +describe('allFlags returns non-$ keys only', () => { |
| 129 | + const client = createNoopClient(bootstrapData); |
| 130 | + |
| 131 | + it('excludes $flagsState and $valid', () => { |
| 132 | + const flags = client.allFlags(); |
| 133 | + expect(flags).toEqual({ |
| 134 | + 'bool-flag': true, |
| 135 | + 'number-flag': 42, |
| 136 | + 'string-flag': 'hello', |
| 137 | + 'json-flag': { nested: true }, |
| 138 | + 'null-flag': null, |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + it('returns a copy, not a reference', () => { |
| 143 | + const flags1 = client.allFlags(); |
| 144 | + const flags2 = client.allFlags(); |
| 145 | + expect(flags1).not.toBe(flags2); |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | +describe('stubbed LDClient methods', () => { |
| 150 | + const client = createNoopClient(bootstrapData); |
| 151 | + |
| 152 | + it('close resolves immediately', async () => { |
| 153 | + await expect(client.close()).resolves.toBeUndefined(); |
| 154 | + }); |
| 155 | + |
| 156 | + it('flush resolves immediately', async () => { |
| 157 | + await expect(client.flush()).resolves.toBeUndefined(); |
| 158 | + }); |
| 159 | + |
| 160 | + it('identify resolves immediately', async () => { |
| 161 | + await expect(client.identify({ kind: 'user', key: 'test' })).resolves.toBeUndefined(); |
| 162 | + }); |
| 163 | + |
| 164 | + it('track does not throw', () => { |
| 165 | + expect(() => client.track('event-key', undefined)).not.toThrow(); |
| 166 | + }); |
| 167 | + |
| 168 | + it('variation returns value from bootstrap', () => { |
| 169 | + expect(client.variation('bool-flag', false)).toBe(true); |
| 170 | + }); |
| 171 | + |
| 172 | + it('variation returns default for missing key', () => { |
| 173 | + expect(client.variation('missing', 'default')).toBe('default'); |
| 174 | + }); |
| 175 | + |
| 176 | + it('variationDetail returns value with null variationIndex and reason', () => { |
| 177 | + const detail = client.variationDetail('bool-flag', false); |
| 178 | + expect(detail.value).toBe(true); |
| 179 | + expect(detail.variationIndex).toBeNull(); |
| 180 | + expect(detail.reason).toBeNull(); |
| 181 | + }); |
| 182 | + |
| 183 | + it('waitForInitialization resolves immediately', async () => { |
| 184 | + await expect(client.waitForInitialization()).resolves.toBeUndefined(); |
| 185 | + }); |
| 186 | +}); |
| 187 | + |
| 188 | +describe('handles edge cases gracefully', () => { |
| 189 | + it('handles undefined bootstrap', () => { |
| 190 | + const client = createNoopClient(undefined); |
| 191 | + expect(client.stringVariation('any', 'def')).toBe('def'); |
| 192 | + expect(client.allFlags()).toEqual({}); |
| 193 | + }); |
| 194 | + |
| 195 | + it('handles no bootstrap argument', () => { |
| 196 | + const client = createNoopClient(); |
| 197 | + expect(client.stringVariation('any', 'def')).toBe('def'); |
| 198 | + expect(client.allFlags()).toEqual({}); |
| 199 | + }); |
| 200 | + |
| 201 | + it('handles bootstrap missing $flagsState', () => { |
| 202 | + const client = createNoopClient({ 'my-flag': true }); |
| 203 | + expect(client.boolVariation('my-flag', false)).toBe(true); |
| 204 | + |
| 205 | + const detail = client.boolVariationDetail('my-flag', false); |
| 206 | + expect(detail.value).toBe(true); |
| 207 | + expect(detail.variationIndex).toBeNull(); |
| 208 | + }); |
| 209 | + |
| 210 | + it('reports initialization state as complete when bootstrap is provided', () => { |
| 211 | + const client = createNoopClient({}); |
| 212 | + expect(client.getInitializationState()).toBe('complete'); |
| 213 | + }); |
| 214 | + |
| 215 | + it('reports initialization state as complete when bootstrap has flags', () => { |
| 216 | + const client = createNoopClient({ 'my-flag': true }); |
| 217 | + expect(client.getInitializationState()).toBe('complete'); |
| 218 | + }); |
| 219 | + |
| 220 | + it('reports initialization state as initializing when bootstrap is not provided', () => { |
| 221 | + const client = createNoopClient(); |
| 222 | + expect(client.getInitializationState()).toBe('initializing'); |
| 223 | + }); |
| 224 | + |
| 225 | + it('isReady returns true when bootstrap is provided', () => { |
| 226 | + const client = createNoopClient({}); |
| 227 | + expect(client.isReady()).toBe(true); |
| 228 | + }); |
| 229 | + |
| 230 | + it('isReady returns false when bootstrap is not provided', () => { |
| 231 | + const client = createNoopClient(); |
| 232 | + expect(client.isReady()).toBe(false); |
| 233 | + }); |
| 234 | +}); |
| 235 | + |
| 236 | +describe('event and lifecycle stubs do not throw and return expected values', () => { |
| 237 | + const client = createNoopClient(bootstrapData); |
| 238 | + |
| 239 | + it('getContext returns undefined', () => { |
| 240 | + expect(client.getContext()).toBeUndefined(); |
| 241 | + }); |
| 242 | + |
| 243 | + it('shouldUseCamelCaseFlagKeys returns true', () => { |
| 244 | + expect(client.shouldUseCamelCaseFlagKeys()).toBe(true); |
| 245 | + }); |
| 246 | + |
| 247 | + it('on does not throw', () => { |
| 248 | + expect(() => client.on('change:bool-flag', () => {})).not.toThrow(); |
| 249 | + }); |
| 250 | + |
| 251 | + it('off does not throw', () => { |
| 252 | + expect(() => client.off('change:bool-flag', () => {})).not.toThrow(); |
| 253 | + }); |
| 254 | + |
| 255 | + it('onContextChange returns a callable unsubscribe function', () => { |
| 256 | + const unsubscribe = client.onContextChange(() => {}); |
| 257 | + expect(typeof unsubscribe).toBe('function'); |
| 258 | + expect(() => unsubscribe()).not.toThrow(); |
| 259 | + }); |
| 260 | + |
| 261 | + it('onInitializationStatusChange returns a callable unsubscribe function', () => { |
| 262 | + const unsubscribe = client.onInitializationStatusChange(() => {}); |
| 263 | + expect(typeof unsubscribe).toBe('function'); |
| 264 | + expect(() => unsubscribe()).not.toThrow(); |
| 265 | + }); |
| 266 | + |
| 267 | + it('start does not throw', () => { |
| 268 | + expect(() => client.start()).not.toThrow(); |
| 269 | + }); |
| 270 | + |
| 271 | + it('addHook does not throw', () => { |
| 272 | + expect(() => client.addHook({} as any)).not.toThrow(); |
| 273 | + }); |
| 274 | + |
| 275 | + it('setStreaming does not throw', () => { |
| 276 | + expect(() => client.setStreaming(true)).not.toThrow(); |
| 277 | + }); |
| 278 | +}); |
0 commit comments