|
| 1 | +import { LDClient, LDContext } from '@launchdarkly/js-server-sdk-common'; |
| 2 | + |
| 3 | +import { init, OxygenLDOptions } from '../src/index'; |
| 4 | +import { setupTestEnvironment } from './setup'; |
| 5 | + |
| 6 | +const sdkKey = 'test-sdk-key'; |
| 7 | +const flagKey1 = 'testFlag1'; |
| 8 | +const flagKey2 = 'testFlag2'; |
| 9 | +const flagKey3 = 'testFlag3'; |
| 10 | +const context: LDContext = { kind: 'user', key: 'test-user-key-1' }; |
| 11 | + |
| 12 | +describe('Shopify Oxygen SDK', () => { |
| 13 | + describe('initialization tests', () => { |
| 14 | + beforeEach(async () => { |
| 15 | + await setupTestEnvironment(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('will initialize successfully with default options', async () => { |
| 19 | + const ldClient = init(sdkKey); |
| 20 | + await ldClient.waitForInitialization(); |
| 21 | + expect(ldClient).toBeDefined(); |
| 22 | + ldClient.close(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('will initialize successfully with custom options', async () => { |
| 26 | + const ldClient = init(sdkKey, { |
| 27 | + sendEvents: false, |
| 28 | + cache: { |
| 29 | + enabled: false, |
| 30 | + }, |
| 31 | + } as OxygenLDOptions); |
| 32 | + await ldClient.waitForInitialization(); |
| 33 | + expect(ldClient).toBeDefined(); |
| 34 | + ldClient.close(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('will fail to initialize if there is no SDK key', () => { |
| 38 | + expect(() => init(null as any)).toThrow(); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('polling tests', () => { |
| 43 | + beforeEach(async () => { |
| 44 | + await setupTestEnvironment(); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('without caching', () => { |
| 48 | + let ldClient: LDClient; |
| 49 | + |
| 50 | + beforeEach(async () => { |
| 51 | + // Ensure fetch is set up before creating client |
| 52 | + ldClient = init(sdkKey, { |
| 53 | + cache: { |
| 54 | + enabled: false, |
| 55 | + }, |
| 56 | + } as OxygenLDOptions); |
| 57 | + await ldClient.waitForInitialization(); |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(() => { |
| 61 | + if (ldClient) { |
| 62 | + ldClient.close(); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + it('Should not cache any requests', async () => { |
| 67 | + await ldClient.variation(flagKey1, context, false); |
| 68 | + await ldClient.allFlagsState(context); |
| 69 | + await ldClient.variationDetail(flagKey3, context, false); |
| 70 | + expect(caches.open).toHaveBeenCalledTimes(0); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('flags', () => { |
| 74 | + it('variation default', async () => { |
| 75 | + const value = await ldClient.variation(flagKey1, context, false); |
| 76 | + |
| 77 | + expect(value).toBeTruthy(); |
| 78 | + |
| 79 | + expect(caches.open).toHaveBeenCalledTimes(0); |
| 80 | + }); |
| 81 | + |
| 82 | + it('variation default rollout', async () => { |
| 83 | + const contextWithEmail = { ... context, email: '[email protected]' }; |
| 84 | + const value = await ldClient.variation(flagKey2, contextWithEmail, false); |
| 85 | + const detail = await ldClient.variationDetail(flagKey2, contextWithEmail, false); |
| 86 | + |
| 87 | + expect(detail).toEqual({ |
| 88 | + reason: { kind: 'FALLTHROUGH' }, |
| 89 | + value: true, |
| 90 | + variationIndex: 0, |
| 91 | + }); |
| 92 | + expect(value).toBeTruthy(); |
| 93 | + |
| 94 | + expect(caches.open).toHaveBeenCalledTimes(0); |
| 95 | + }); |
| 96 | + |
| 97 | + it('rule match', async () => { |
| 98 | + const contextWithEmail = { ... context, email: '[email protected]' }; |
| 99 | + const value = await ldClient.variation(flagKey1, contextWithEmail, false); |
| 100 | + const detail = await ldClient.variationDetail(flagKey1, contextWithEmail, false); |
| 101 | + |
| 102 | + expect(detail).toEqual({ |
| 103 | + reason: { kind: 'RULE_MATCH', ruleId: 'rule1', ruleIndex: 0 }, |
| 104 | + value: false, |
| 105 | + variationIndex: 1, |
| 106 | + }); |
| 107 | + expect(value).toBeFalsy(); |
| 108 | + |
| 109 | + expect(caches.open).toHaveBeenCalledTimes(0); |
| 110 | + }); |
| 111 | + |
| 112 | + it('fallthrough', async () => { |
| 113 | + const contextWithEmail = { ... context, email: '[email protected]' }; |
| 114 | + const value = await ldClient.variation(flagKey1, contextWithEmail, false); |
| 115 | + const detail = await ldClient.variationDetail(flagKey1, contextWithEmail, false); |
| 116 | + |
| 117 | + expect(detail).toEqual({ |
| 118 | + reason: { kind: 'FALLTHROUGH' }, |
| 119 | + value: true, |
| 120 | + variationIndex: 0, |
| 121 | + }); |
| 122 | + expect(value).toBeTruthy(); |
| 123 | + |
| 124 | + expect(caches.open).toHaveBeenCalledTimes(0); |
| 125 | + }); |
| 126 | + |
| 127 | + it('allFlags fallthrough', async () => { |
| 128 | + const allFlags = await ldClient.allFlagsState(context); |
| 129 | + |
| 130 | + expect(allFlags).toBeDefined(); |
| 131 | + expect(allFlags.toJSON()).toEqual({ |
| 132 | + $flagsState: { |
| 133 | + testFlag1: { debugEventsUntilDate: 2000, variation: 0, version: 2 }, |
| 134 | + testFlag2: { debugEventsUntilDate: 2000, variation: 0, version: 2 }, |
| 135 | + testFlag3: { debugEventsUntilDate: 2000, variation: 0, version: 2 }, |
| 136 | + }, |
| 137 | + $valid: true, |
| 138 | + testFlag1: true, |
| 139 | + testFlag2: true, |
| 140 | + testFlag3: true, |
| 141 | + }); |
| 142 | + |
| 143 | + expect(caches.open).toHaveBeenCalledTimes(0); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe('segments', () => { |
| 148 | + it('segment by country', async () => { |
| 149 | + const contextWithCountry = { ...context, country: 'australia' }; |
| 150 | + const value = await ldClient.variation(flagKey3, contextWithCountry, false); |
| 151 | + const detail = await ldClient.variationDetail(flagKey3, contextWithCountry, false); |
| 152 | + |
| 153 | + expect(detail).toEqual({ |
| 154 | + reason: { kind: 'RULE_MATCH', ruleId: 'rule1', ruleIndex: 0 }, |
| 155 | + value: false, |
| 156 | + variationIndex: 1, |
| 157 | + }); |
| 158 | + expect(value).toBeFalsy(); |
| 159 | + |
| 160 | + expect(caches.open).toHaveBeenCalledTimes(0); |
| 161 | + }); |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + describe('with caching', () => { |
| 166 | + let ldClient: LDClient; |
| 167 | + |
| 168 | + beforeEach(async () => { |
| 169 | + // Ensure fetch is set up before creating client |
| 170 | + ldClient = init(sdkKey); |
| 171 | + await ldClient.waitForInitialization(); |
| 172 | + }); |
| 173 | + |
| 174 | + afterEach(() => { |
| 175 | + if (ldClient) { |
| 176 | + ldClient.close(); |
| 177 | + } |
| 178 | + }); |
| 179 | + |
| 180 | + it('will cache across multiple variation calls', async () => { |
| 181 | + await ldClient.variation(flagKey1, context, false); |
| 182 | + await ldClient.variation(flagKey2, context, false); |
| 183 | + |
| 184 | + // Should only fetch once due to caching |
| 185 | + expect(caches.open).toHaveBeenCalledTimes(1); |
| 186 | + }); |
| 187 | + |
| 188 | + it('will cache across multiple allFlags calls', async () => { |
| 189 | + await ldClient.allFlagsState(context); |
| 190 | + await ldClient.allFlagsState(context); |
| 191 | + |
| 192 | + expect(caches.open).toHaveBeenCalledTimes(1); |
| 193 | + }); |
| 194 | + |
| 195 | + it('will cache between allFlags and variation', async () => { |
| 196 | + await ldClient.variation(flagKey1, context, false); |
| 197 | + await ldClient.allFlagsState(context); |
| 198 | + |
| 199 | + expect(caches.open).toHaveBeenCalledTimes(1); |
| 200 | + }); |
| 201 | + }); |
| 202 | + }); |
| 203 | +}); |
0 commit comments