|
| 1 | +import { ClientOptions, GrowthBookClient, InitOptions } from '@growthbook/growthbook'; |
| 2 | +import { GrowthbookProvider } from './growthbook-provider'; |
| 3 | +import { Client, OpenFeature } from '@openfeature/server-sdk'; |
| 4 | + |
| 5 | +jest.mock('@growthbook/growthbook'); |
| 6 | + |
| 7 | +const testFlagKey = 'flag-key'; |
| 8 | +const growthbookOptionsMock: ClientOptions = { |
| 9 | + apiHost: 'http://api.growthbook.io', |
| 10 | + clientKey: 'sdk-test-key', |
| 11 | + globalAttributes: { |
| 12 | + id: 1, |
| 13 | + }, |
| 14 | +}; |
| 15 | + |
| 16 | +const initOptionsMock: InitOptions = { |
| 17 | + timeout: 5000, |
| 18 | +}; |
| 19 | + |
| 20 | +describe('GrowthbookProvider', () => { |
| 21 | + let gbProvider: GrowthbookProvider; |
| 22 | + let ofClient: Client; |
| 23 | + |
| 24 | + beforeAll(() => { |
| 25 | + gbProvider = new GrowthbookProvider(growthbookOptionsMock, initOptionsMock); |
| 26 | + OpenFeature.setProvider(gbProvider); |
| 27 | + ofClient = OpenFeature.getClient(); |
| 28 | + }); |
| 29 | + beforeEach(() => { |
| 30 | + jest.clearAllMocks(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should be and instance of GrowthbookProvider', () => { |
| 34 | + expect(new GrowthbookProvider(growthbookOptionsMock, initOptionsMock)).toBeInstanceOf(GrowthbookProvider); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('constructor', () => { |
| 38 | + it('should set the growthbook options & initOptions correctly', () => { |
| 39 | + const provider = new GrowthbookProvider(growthbookOptionsMock, initOptionsMock); |
| 40 | + |
| 41 | + expect(provider['options']).toEqual(growthbookOptionsMock); |
| 42 | + expect(provider['_initOptions']).toEqual(initOptionsMock); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('initialize', () => { |
| 47 | + const provider = new GrowthbookProvider(growthbookOptionsMock, initOptionsMock); |
| 48 | + |
| 49 | + it('should call growthbook initialize function with correct arguments', async () => { |
| 50 | + const evalContext = { serverIp: '10.1.1.1' }; |
| 51 | + await provider.initialize({ serverIp: '10.1.1.1' }); |
| 52 | + |
| 53 | + const options = { |
| 54 | + ...provider['options'], |
| 55 | + globalAttributes: { ...provider['options'].globalAttributes, ...evalContext }, |
| 56 | + }; |
| 57 | + |
| 58 | + expect(GrowthBookClient).toHaveBeenCalledWith(options); |
| 59 | + expect(provider['_client']?.init).toHaveBeenCalledWith(initOptionsMock); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('resolveBooleanEvaluation', () => { |
| 64 | + it('handles correct return types for boolean variations', async () => { |
| 65 | + jest.spyOn(GrowthBookClient.prototype, 'evalFeature').mockImplementation(() => ({ |
| 66 | + value: true, |
| 67 | + source: 'experiment', |
| 68 | + on: true, |
| 69 | + off: false, |
| 70 | + ruleId: 'test', |
| 71 | + experimentResult: { |
| 72 | + value: true, |
| 73 | + variationId: 1, |
| 74 | + key: 'treatment', |
| 75 | + inExperiment: true, |
| 76 | + hashAttribute: 'id', |
| 77 | + hashValue: 'abc', |
| 78 | + featureId: testFlagKey, |
| 79 | + }, |
| 80 | + })); |
| 81 | + |
| 82 | + const res = await ofClient.getBooleanDetails(testFlagKey, false); |
| 83 | + expect(res).toEqual({ |
| 84 | + flagKey: testFlagKey, |
| 85 | + flagMetadata: {}, |
| 86 | + value: true, |
| 87 | + reason: 'experiment', |
| 88 | + variant: 'treatment', |
| 89 | + }); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('resolveStringEvaluation', () => { |
| 94 | + it('handles correct return types for string variations', async () => { |
| 95 | + jest.spyOn(GrowthBookClient.prototype, 'evalFeature').mockImplementation(() => ({ |
| 96 | + value: 'Experiment fearlessly, deliver confidently', |
| 97 | + source: 'experiment', |
| 98 | + on: true, |
| 99 | + off: false, |
| 100 | + ruleId: 'test', |
| 101 | + experimentResult: { |
| 102 | + value: 'Experiment fearlessly, deliver confidently', |
| 103 | + variationId: 1, |
| 104 | + key: 'treatment', |
| 105 | + inExperiment: true, |
| 106 | + hashAttribute: 'id', |
| 107 | + hashValue: 'abc', |
| 108 | + featureId: testFlagKey, |
| 109 | + }, |
| 110 | + })); |
| 111 | + |
| 112 | + const res = await ofClient.getStringDetails(testFlagKey, ''); |
| 113 | + expect(res).toEqual({ |
| 114 | + flagKey: testFlagKey, |
| 115 | + flagMetadata: {}, |
| 116 | + value: 'Experiment fearlessly, deliver confidently', |
| 117 | + reason: 'experiment', |
| 118 | + variant: 'treatment', |
| 119 | + }); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('resolveNumberEvaluation', () => { |
| 124 | + it('handles correct return types for number variations', async () => { |
| 125 | + jest.spyOn(GrowthBookClient.prototype, 'evalFeature').mockImplementation(() => ({ |
| 126 | + value: 12345, |
| 127 | + source: 'experiment', |
| 128 | + on: true, |
| 129 | + off: false, |
| 130 | + ruleId: 'test', |
| 131 | + experimentResult: { |
| 132 | + value: 12345, |
| 133 | + variationId: 1, |
| 134 | + key: 'treatment', |
| 135 | + inExperiment: true, |
| 136 | + hashAttribute: 'id', |
| 137 | + hashValue: 'abc', |
| 138 | + featureId: testFlagKey, |
| 139 | + }, |
| 140 | + })); |
| 141 | + |
| 142 | + const res = await ofClient.getNumberDetails(testFlagKey, 1); |
| 143 | + expect(res).toEqual({ |
| 144 | + flagKey: testFlagKey, |
| 145 | + flagMetadata: {}, |
| 146 | + value: 12345, |
| 147 | + reason: 'experiment', |
| 148 | + variant: 'treatment', |
| 149 | + }); |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + describe('resolveObjectEvaluation', () => { |
| 154 | + it('handles correct return types for object variations', async () => { |
| 155 | + jest.spyOn(GrowthBookClient.prototype, 'evalFeature').mockImplementation(() => ({ |
| 156 | + value: { test: true }, |
| 157 | + source: 'experiment', |
| 158 | + on: true, |
| 159 | + off: false, |
| 160 | + ruleId: 'test', |
| 161 | + experimentResult: { |
| 162 | + value: { test: true }, |
| 163 | + variationId: 1, |
| 164 | + key: 'treatment', |
| 165 | + inExperiment: true, |
| 166 | + hashAttribute: 'id', |
| 167 | + hashValue: 'abc', |
| 168 | + featureId: testFlagKey, |
| 169 | + }, |
| 170 | + })); |
| 171 | + |
| 172 | + const res = await ofClient.getObjectDetails(testFlagKey, {}); |
| 173 | + expect(res).toEqual({ |
| 174 | + flagKey: testFlagKey, |
| 175 | + flagMetadata: {}, |
| 176 | + value: { test: true }, |
| 177 | + reason: 'experiment', |
| 178 | + variant: 'treatment', |
| 179 | + }); |
| 180 | + }); |
| 181 | + }); |
| 182 | +}); |
0 commit comments