@servicetitan/launchdarkly-service 40.2.2 → 40.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/feature-flag-provider.d.ts +6 -1
- package/dist/feature-flag-provider.d.ts.map +1 -1
- package/dist/feature-flag-provider.js +4 -3
- package/dist/feature-flag-provider.js.map +1 -1
- package/dist/ld-provider.d.ts.map +1 -1
- package/dist/ld-provider.js +34 -9
- package/dist/ld-provider.js.map +1 -1
- package/package.json +11 -7
- package/src/feature-flag-provider.tsx +11 -3
- package/src/ld-provider.tsx +27 -10
- package/dist/tests/create-ld-client.test.d.ts +0 -2
- package/dist/tests/create-ld-client.test.d.ts.map +0 -1
- package/dist/tests/create-ld-client.test.js +0 -58
- package/dist/tests/create-ld-client.test.js.map +0 -1
- package/dist/tests/feature-flag-provider.test.d.ts +0 -2
- package/dist/tests/feature-flag-provider.test.d.ts.map +0 -1
- package/dist/tests/feature-flag-provider.test.js +0 -58
- package/dist/tests/feature-flag-provider.test.js.map +0 -1
- package/dist/tests/feature-flag-store.test.d.ts +0 -2
- package/dist/tests/feature-flag-store.test.d.ts.map +0 -1
- package/dist/tests/feature-flag-store.test.js +0 -46
- package/dist/tests/feature-flag-store.test.js.map +0 -1
- package/dist/tests/flag-used-inspectors.test.d.ts +0 -2
- package/dist/tests/flag-used-inspectors.test.d.ts.map +0 -1
- package/dist/tests/flag-used-inspectors.test.js +0 -95
- package/dist/tests/flag-used-inspectors.test.js.map +0 -1
- package/dist/tests/get-client.test.d.ts +0 -2
- package/dist/tests/get-client.test.d.ts.map +0 -1
- package/dist/tests/get-client.test.js +0 -81
- package/dist/tests/get-client.test.js.map +0 -1
- package/dist/tests/get-ld-client.test.d.ts +0 -2
- package/dist/tests/get-ld-client.test.d.ts.map +0 -1
- package/dist/tests/get-ld-client.test.js +0 -49
- package/dist/tests/get-ld-client.test.js.map +0 -1
- package/dist/tests/get-sdk-version.test.d.ts +0 -2
- package/dist/tests/get-sdk-version.test.d.ts.map +0 -1
- package/dist/tests/get-sdk-version.test.js +0 -41
- package/dist/tests/get-sdk-version.test.js.map +0 -1
- package/dist/tests/get-version-client.test.d.ts +0 -2
- package/dist/tests/get-version-client.test.d.ts.map +0 -1
- package/dist/tests/get-version-client.test.js +0 -63
- package/dist/tests/get-version-client.test.js.map +0 -1
- package/dist/tests/ld-provider.test.d.ts +0 -2
- package/dist/tests/ld-provider.test.d.ts.map +0 -1
- package/dist/tests/ld-provider.test.js +0 -273
- package/dist/tests/ld-provider.test.js.map +0 -1
- package/dist/tests/ld-service-provider.test.d.ts +0 -2
- package/dist/tests/ld-service-provider.test.d.ts.map +0 -1
- package/dist/tests/ld-service-provider.test.js +0 -227
- package/dist/tests/ld-service-provider.test.js.map +0 -1
- package/dist/tests/with-feature-flag.test.d.ts +0 -2
- package/dist/tests/with-feature-flag.test.d.ts.map +0 -1
- package/dist/tests/with-feature-flag.test.js +0 -83
- package/dist/tests/with-feature-flag.test.js.map +0 -1
- package/src/datadog-rum/__tests__/create-datadog-inspector.test.ts +0 -46
- package/src/tests/create-ld-client.test.ts +0 -64
- package/src/tests/feature-flag-provider.test.tsx +0 -62
- package/src/tests/feature-flag-store.test.ts +0 -53
- package/src/tests/flag-used-inspectors.test.tsx +0 -87
- package/src/tests/get-client.test.ts +0 -98
- package/src/tests/get-ld-client.test.ts +0 -58
- package/src/tests/get-sdk-version.test.ts +0 -48
- package/src/tests/get-version-client.test.ts +0 -70
- package/src/tests/ld-provider.test.tsx +0 -304
- package/src/tests/ld-service-provider.test.tsx +0 -259
- package/src/tests/with-feature-flag.test.tsx +0 -95
|
@@ -1,273 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { Log } from '@servicetitan/log-service';
|
|
3
|
-
import { Provider, useDependencies } from '@servicetitan/react-ioc';
|
|
4
|
-
import '@testing-library/jest-dom';
|
|
5
|
-
import { render, screen, waitFor } from '@testing-library/react';
|
|
6
|
-
import { act, Fragment } from 'react';
|
|
7
|
-
import { FeatureFlagStore } from '../feature-flag-store';
|
|
8
|
-
import { getLDClient } from '../get-ld-client';
|
|
9
|
-
import { LDProvider } from '../ld-provider';
|
|
10
|
-
import { LD_SERVICE_TOKEN } from '../types';
|
|
11
|
-
function formatProps(props) {
|
|
12
|
-
return Object.entries(props).map(([key, value])=>{
|
|
13
|
-
return `${key}=${value.toString()}`;
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
function mockComponent(name) {
|
|
17
|
-
return {
|
|
18
|
-
[name]: ({ children, ...props })=>/*#__PURE__*/ _jsxs(Fragment, {
|
|
19
|
-
children: [
|
|
20
|
-
/*#__PURE__*/ _jsx("div", {
|
|
21
|
-
children: `<${name} ${formatProps(props)} />`
|
|
22
|
-
}),
|
|
23
|
-
children
|
|
24
|
-
]
|
|
25
|
-
})
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
jest.mock('launchdarkly-react-client-sdk', ()=>({
|
|
29
|
-
...mockComponent('LDProvider')
|
|
30
|
-
}));
|
|
31
|
-
jest.mock('../feature-flag-provider', ()=>({
|
|
32
|
-
...mockComponent('FeatureFlagProvider')
|
|
33
|
-
}));
|
|
34
|
-
jest.mock('../get-ld-client', ()=>({
|
|
35
|
-
getLDClient: jest.fn()
|
|
36
|
-
}));
|
|
37
|
-
describe(`[launchdarkly-service] ${LDProvider.name}`, ()=>{
|
|
38
|
-
const log = {
|
|
39
|
-
error: jest.fn()
|
|
40
|
-
};
|
|
41
|
-
const ldClient = {
|
|
42
|
-
on: jest.fn(),
|
|
43
|
-
off: jest.fn(),
|
|
44
|
-
waitUntilReady: jest.fn().mockResolvedValue(undefined),
|
|
45
|
-
waitForInitialization: jest.fn().mockResolvedValue(undefined),
|
|
46
|
-
toString: ()=>'ldClient'
|
|
47
|
-
};
|
|
48
|
-
let singletons;
|
|
49
|
-
let props;
|
|
50
|
-
let providedFeatureFlagStore;
|
|
51
|
-
let renderCount;
|
|
52
|
-
const Component = ()=>{
|
|
53
|
-
[providedFeatureFlagStore] = useDependencies(FeatureFlagStore);
|
|
54
|
-
renderCount += 1;
|
|
55
|
-
return /*#__PURE__*/ _jsx("div", {
|
|
56
|
-
children: '<Component />'
|
|
57
|
-
});
|
|
58
|
-
};
|
|
59
|
-
beforeEach(()=>{
|
|
60
|
-
jest.clearAllMocks();
|
|
61
|
-
jest.mocked(getLDClient).mockReturnValue(ldClient);
|
|
62
|
-
renderCount = 0;
|
|
63
|
-
singletons = [];
|
|
64
|
-
props = {
|
|
65
|
-
clientSideID: 'foo',
|
|
66
|
-
projectName: 'bar',
|
|
67
|
-
context: {
|
|
68
|
-
kind: 'user',
|
|
69
|
-
key: 'user-1'
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
});
|
|
73
|
-
const tree = ()=>/*#__PURE__*/ _jsx(Provider, {
|
|
74
|
-
singletons: singletons,
|
|
75
|
-
children: /*#__PURE__*/ _jsx(LDProvider, {
|
|
76
|
-
...props,
|
|
77
|
-
children: /*#__PURE__*/ _jsx(Component, {})
|
|
78
|
-
})
|
|
79
|
-
});
|
|
80
|
-
const subject = ()=>render(tree());
|
|
81
|
-
function expectLDProvider(props) {
|
|
82
|
-
if (props) {
|
|
83
|
-
expect(screen.getByText(`<LDProvider ${formatProps(props)} />`)).toBeInTheDocument();
|
|
84
|
-
} else {
|
|
85
|
-
expect(screen.getByText(/<LDProvider .* \/>/)).toBeInTheDocument();
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
test('renders base LDProvider and FeatureFlagProvider after client is ready and initialized', async ()=>{
|
|
89
|
-
subject();
|
|
90
|
-
await waitFor(()=>expectLDProvider({
|
|
91
|
-
clientSideID: props.clientSideID,
|
|
92
|
-
ldClient
|
|
93
|
-
}));
|
|
94
|
-
expect(screen.getByText('<FeatureFlagProvider />')).toBeInTheDocument();
|
|
95
|
-
expect(screen.getByText('<Component />')).toBeInTheDocument();
|
|
96
|
-
expect(ldClient.waitForInitialization).toHaveBeenCalled();
|
|
97
|
-
});
|
|
98
|
-
describe('when given flags', ()=>{
|
|
99
|
-
beforeEach(()=>props.flags = {
|
|
100
|
-
EnableThing: false
|
|
101
|
-
});
|
|
102
|
-
test('forwards them to the base provider', async ()=>{
|
|
103
|
-
subject();
|
|
104
|
-
await waitFor(()=>expectLDProvider({
|
|
105
|
-
clientSideID: props.clientSideID,
|
|
106
|
-
ldClient,
|
|
107
|
-
flags: props.flags
|
|
108
|
-
}));
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
test('provides FeatureFlagStore', async ()=>{
|
|
112
|
-
subject();
|
|
113
|
-
await waitFor(()=>expectLDProvider());
|
|
114
|
-
expect(providedFeatureFlagStore).toBeDefined();
|
|
115
|
-
});
|
|
116
|
-
describe('when waitForInitialization is false', ()=>{
|
|
117
|
-
let initializedCallback;
|
|
118
|
-
beforeEach(()=>{
|
|
119
|
-
var _jest_mocked;
|
|
120
|
-
props.waitForInitialization = false;
|
|
121
|
-
(_jest_mocked = jest.mocked(ldClient.on)) === null || _jest_mocked === void 0 ? void 0 : _jest_mocked.mockImplementation((event, callback)=>{
|
|
122
|
-
if (event === 'initialized') {
|
|
123
|
-
initializedCallback = callback;
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
test('does not wait for client to initialize', async ()=>{
|
|
128
|
-
subject();
|
|
129
|
-
await waitFor(()=>expectLDProvider());
|
|
130
|
-
expect(ldClient.waitForInitialization).not.toHaveBeenCalled();
|
|
131
|
-
});
|
|
132
|
-
test('re-renders when client is initialized', async ()=>{
|
|
133
|
-
subject();
|
|
134
|
-
await waitFor(()=>expectLDProvider());
|
|
135
|
-
expect(renderCount).toBe(1);
|
|
136
|
-
act(()=>initializedCallback === null || initializedCallback === void 0 ? void 0 : initializedCallback());
|
|
137
|
-
expect(renderCount).toBe(2);
|
|
138
|
-
});
|
|
139
|
-
test('deregisters listener when component is unmounted', async ()=>{
|
|
140
|
-
const { unmount } = subject();
|
|
141
|
-
await waitFor(()=>expectLDProvider());
|
|
142
|
-
unmount();
|
|
143
|
-
expect(jest.mocked(ldClient.off)).toHaveBeenCalledWith('initialized', initializedCallback);
|
|
144
|
-
});
|
|
145
|
-
});
|
|
146
|
-
describe('when initialization fails', ()=>{
|
|
147
|
-
beforeEach(()=>{
|
|
148
|
-
var _jest_mocked;
|
|
149
|
-
(_jest_mocked = jest.mocked(ldClient.waitForInitialization)) === null || _jest_mocked === void 0 ? void 0 : _jest_mocked.mockRejectedValue('Oops!');
|
|
150
|
-
});
|
|
151
|
-
test('renders base LDProvider', async ()=>{
|
|
152
|
-
subject();
|
|
153
|
-
await waitFor(()=>expectLDProvider());
|
|
154
|
-
});
|
|
155
|
-
describe('when log service is provided', ()=>{
|
|
156
|
-
beforeEach(()=>singletons.push({
|
|
157
|
-
provide: Log,
|
|
158
|
-
useValue: log
|
|
159
|
-
}));
|
|
160
|
-
test('logs error', async ()=>{
|
|
161
|
-
subject();
|
|
162
|
-
await waitFor(()=>expectLDProvider());
|
|
163
|
-
expect(log.error).toHaveBeenCalledWith({
|
|
164
|
-
category: 'LaunchDarkly',
|
|
165
|
-
code: 'InitializationFailed',
|
|
166
|
-
error: 'Oops!',
|
|
167
|
-
message: `${props.projectName} client failed to initialize`
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
describe('when no context is provided', ()=>{
|
|
173
|
-
beforeEach(()=>{
|
|
174
|
-
props.context = null;
|
|
175
|
-
});
|
|
176
|
-
test('does not initialize client', ()=>{
|
|
177
|
-
subject();
|
|
178
|
-
expect(getLDClient).not.toHaveBeenCalled();
|
|
179
|
-
});
|
|
180
|
-
test('does not render children', ()=>{
|
|
181
|
-
subject();
|
|
182
|
-
expect(screen.queryByText('<Component />')).not.toBeInTheDocument();
|
|
183
|
-
});
|
|
184
|
-
describe('when context becomes available after mount', ()=>{
|
|
185
|
-
let rerender;
|
|
186
|
-
beforeEach(async ()=>{
|
|
187
|
-
rerender = subject().rerender;
|
|
188
|
-
props.context = {
|
|
189
|
-
kind: 'user',
|
|
190
|
-
key: 'user-1'
|
|
191
|
-
};
|
|
192
|
-
rerender(tree());
|
|
193
|
-
await waitFor(()=>expectLDProvider({
|
|
194
|
-
clientSideID: props.clientSideID,
|
|
195
|
-
ldClient
|
|
196
|
-
}));
|
|
197
|
-
});
|
|
198
|
-
test('initializes client', ()=>{
|
|
199
|
-
expect(getLDClient).toHaveBeenCalledWith(expect.objectContaining({
|
|
200
|
-
context: props.context
|
|
201
|
-
}));
|
|
202
|
-
});
|
|
203
|
-
test('renders children', ()=>{
|
|
204
|
-
expect(screen.queryByText('<Component />')).toBeInTheDocument();
|
|
205
|
-
});
|
|
206
|
-
describe('when context is removed', ()=>{
|
|
207
|
-
beforeEach(()=>{
|
|
208
|
-
props.context = null;
|
|
209
|
-
rerender(tree());
|
|
210
|
-
});
|
|
211
|
-
test('removes children', ()=>{
|
|
212
|
-
expect(screen.queryByText('<Component />')).not.toBeInTheDocument();
|
|
213
|
-
});
|
|
214
|
-
});
|
|
215
|
-
});
|
|
216
|
-
});
|
|
217
|
-
describe('when LDService is provided', ()=>{
|
|
218
|
-
const ldServiceClient = {
|
|
219
|
-
waitUntilReady: jest.fn().mockResolvedValue(undefined),
|
|
220
|
-
waitForInitialization: jest.fn().mockResolvedValue(undefined),
|
|
221
|
-
toString: ()=>'ldServiceClient'
|
|
222
|
-
};
|
|
223
|
-
let ldService;
|
|
224
|
-
beforeEach(()=>{
|
|
225
|
-
ldService = {
|
|
226
|
-
getClient: jest.fn().mockReturnValue(ldServiceClient)
|
|
227
|
-
};
|
|
228
|
-
singletons === null || singletons === void 0 ? void 0 : singletons.push({
|
|
229
|
-
provide: LD_SERVICE_TOKEN,
|
|
230
|
-
useValue: ldService
|
|
231
|
-
});
|
|
232
|
-
});
|
|
233
|
-
test('uses LDService client', async ()=>{
|
|
234
|
-
subject();
|
|
235
|
-
await waitFor(()=>expectLDProvider({
|
|
236
|
-
clientSideID: props.clientSideID,
|
|
237
|
-
ldClient: ldServiceClient
|
|
238
|
-
}));
|
|
239
|
-
expect(ldServiceClient.waitForInitialization).toHaveBeenCalled();
|
|
240
|
-
});
|
|
241
|
-
describe('when LDService has options', ()=>{
|
|
242
|
-
const options = {
|
|
243
|
-
hash: 'abc123'
|
|
244
|
-
};
|
|
245
|
-
beforeEach(()=>ldService.options = options);
|
|
246
|
-
test('forwards options to LDClient', async ()=>{
|
|
247
|
-
subject();
|
|
248
|
-
await waitFor(()=>expectLDProvider());
|
|
249
|
-
expect(ldService.getClient).toHaveBeenCalledWith(expect.objectContaining({
|
|
250
|
-
options
|
|
251
|
-
}));
|
|
252
|
-
});
|
|
253
|
-
});
|
|
254
|
-
describe('when application is provided', ()=>{
|
|
255
|
-
const application = {
|
|
256
|
-
id: 'some-application',
|
|
257
|
-
version: '1.2.3'
|
|
258
|
-
};
|
|
259
|
-
beforeEach(()=>ldService.application = application);
|
|
260
|
-
test('passes application to LDClient', async ()=>{
|
|
261
|
-
subject();
|
|
262
|
-
await waitFor(()=>expectLDProvider());
|
|
263
|
-
expect(ldService.getClient).toHaveBeenCalledWith(expect.objectContaining({
|
|
264
|
-
options: {
|
|
265
|
-
application
|
|
266
|
-
}
|
|
267
|
-
}));
|
|
268
|
-
});
|
|
269
|
-
});
|
|
270
|
-
});
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
//# sourceMappingURL=ld-provider.test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/tests/ld-provider.test.tsx"],"sourcesContent":["import { Log } from '@servicetitan/log-service';\nimport { Provider, useDependencies } from '@servicetitan/react-ioc';\nimport '@testing-library/jest-dom';\nimport { render, screen, waitFor } from '@testing-library/react';\nimport { LDClient } from 'launchdarkly-js-client-sdk';\nimport { act, FC, Fragment, ReactNode } from 'react';\nimport { FeatureFlagStore } from '../feature-flag-store';\nimport { getLDClient } from '../get-ld-client';\nimport { LDProvider } from '../ld-provider';\nimport { LD_SERVICE_TOKEN, LDService } from '../types';\n\nfunction formatProps(props: Record<string, any>) {\n return Object.entries(props).map(([key, value]) => {\n return `${key}=${value.toString()}`;\n });\n}\n\nfunction mockComponent(name: string) {\n return {\n [name]: ({ children, ...props }: any) => (\n <Fragment>\n <div>{`<${name} ${formatProps(props)} />`}</div>\n {children}\n </Fragment>\n ),\n };\n}\n\njest.mock('launchdarkly-react-client-sdk', () => ({\n ...mockComponent('LDProvider'),\n}));\njest.mock('../feature-flag-provider', () => ({\n ...mockComponent('FeatureFlagProvider'),\n}));\njest.mock('../get-ld-client', () => ({\n getLDClient: jest.fn(),\n}));\n\ndescribe(`[launchdarkly-service] ${LDProvider.name}`, () => {\n const log: Partial<Log> = {\n error: jest.fn(),\n };\n const ldClient: Partial<LDClient> & { toString: () => string } = {\n on: jest.fn(),\n off: jest.fn(),\n waitUntilReady: jest.fn().mockResolvedValue(undefined),\n waitForInitialization: jest.fn().mockResolvedValue(undefined),\n toString: () => 'ldClient',\n };\n let singletons: Required<Parameters<typeof Provider>[0]>['singletons'];\n let props: Parameters<typeof LDProvider>[0];\n let providedFeatureFlagStore: FeatureFlagStore;\n let renderCount: number;\n\n const Component: FC = () => {\n [providedFeatureFlagStore] = useDependencies(FeatureFlagStore);\n renderCount += 1;\n return <div>{'<Component />'}</div>;\n };\n\n beforeEach(() => {\n jest.clearAllMocks();\n jest.mocked(getLDClient).mockReturnValue(ldClient as LDClient);\n renderCount = 0;\n singletons = [];\n props = {\n clientSideID: 'foo',\n projectName: 'bar',\n context: { kind: 'user', key: 'user-1' },\n };\n });\n\n const tree = () => (\n <Provider singletons={singletons}>\n <LDProvider {...props}>\n <Component />\n </LDProvider>\n </Provider>\n );\n\n const subject = () => render(tree());\n\n function expectLDProvider(props?: Record<string, any>) {\n if (props) {\n expect(screen.getByText(`<LDProvider ${formatProps(props)} />`)).toBeInTheDocument();\n } else {\n expect(screen.getByText(/<LDProvider .* \\/>/)).toBeInTheDocument();\n }\n }\n\n test('renders base LDProvider and FeatureFlagProvider after client is ready and initialized', async () => {\n subject();\n\n await waitFor(() => expectLDProvider({ clientSideID: props.clientSideID, ldClient }));\n\n expect(screen.getByText('<FeatureFlagProvider />')).toBeInTheDocument();\n expect(screen.getByText('<Component />')).toBeInTheDocument();\n expect(ldClient.waitForInitialization).toHaveBeenCalled();\n });\n\n describe('when given flags', () => {\n beforeEach(() => (props.flags = { EnableThing: false }));\n\n test('forwards them to the base provider', async () => {\n subject();\n\n await waitFor(() =>\n expectLDProvider({ clientSideID: props.clientSideID, ldClient, flags: props.flags })\n );\n });\n });\n\n test('provides FeatureFlagStore', async () => {\n subject();\n\n await waitFor(() => expectLDProvider());\n\n expect(providedFeatureFlagStore).toBeDefined();\n });\n\n describe('when waitForInitialization is false', () => {\n let initializedCallback: Function;\n\n beforeEach(() => {\n props.waitForInitialization = false;\n jest.mocked(ldClient.on)?.mockImplementation((event, callback) => {\n if (event === 'initialized') {\n initializedCallback = callback;\n }\n });\n });\n\n test('does not wait for client to initialize', async () => {\n subject();\n\n await waitFor(() => expectLDProvider());\n\n expect(ldClient.waitForInitialization).not.toHaveBeenCalled();\n });\n\n test('re-renders when client is initialized', async () => {\n subject();\n\n await waitFor(() => expectLDProvider());\n expect(renderCount).toBe(1);\n\n act(() => initializedCallback?.());\n expect(renderCount).toBe(2);\n });\n\n test('deregisters listener when component is unmounted', async () => {\n const { unmount } = subject();\n\n await waitFor(() => expectLDProvider());\n\n unmount();\n\n expect(jest.mocked(ldClient.off)).toHaveBeenCalledWith(\n 'initialized',\n initializedCallback\n );\n });\n });\n\n describe('when initialization fails', () => {\n beforeEach(() => {\n jest.mocked(ldClient.waitForInitialization)?.mockRejectedValue('Oops!');\n });\n\n test('renders base LDProvider', async () => {\n subject();\n\n await waitFor(() => expectLDProvider());\n });\n\n describe('when log service is provided', () => {\n beforeEach(() => singletons.push({ provide: Log, useValue: log }));\n\n test('logs error', async () => {\n subject();\n\n await waitFor(() => expectLDProvider());\n\n expect(log.error).toHaveBeenCalledWith({\n category: 'LaunchDarkly',\n code: 'InitializationFailed',\n error: 'Oops!',\n message: `${props.projectName} client failed to initialize`,\n });\n });\n });\n });\n\n describe('when no context is provided', () => {\n beforeEach(() => {\n props.context = null;\n });\n\n test('does not initialize client', () => {\n subject();\n\n expect(getLDClient).not.toHaveBeenCalled();\n });\n\n test('does not render children', () => {\n subject();\n\n expect(screen.queryByText('<Component />')).not.toBeInTheDocument();\n });\n\n describe('when context becomes available after mount', () => {\n let rerender: (ui: ReactNode) => void;\n\n beforeEach(async () => {\n rerender = subject().rerender;\n props.context = { kind: 'user', key: 'user-1' };\n rerender(tree());\n\n await waitFor(() =>\n expectLDProvider({ clientSideID: props.clientSideID, ldClient })\n );\n });\n\n test('initializes client', () => {\n expect(getLDClient).toHaveBeenCalledWith(\n expect.objectContaining({ context: props.context })\n );\n });\n\n test('renders children', () => {\n expect(screen.queryByText('<Component />')).toBeInTheDocument();\n });\n\n describe('when context is removed', () => {\n beforeEach(() => {\n props.context = null;\n rerender(tree());\n });\n\n test('removes children', () => {\n expect(screen.queryByText('<Component />')).not.toBeInTheDocument();\n });\n });\n });\n });\n\n describe('when LDService is provided', () => {\n const ldServiceClient: Partial<LDClient> & { toString: () => string } = {\n waitUntilReady: jest.fn().mockResolvedValue(undefined),\n waitForInitialization: jest.fn().mockResolvedValue(undefined),\n toString: () => 'ldServiceClient',\n };\n let ldService: LDService;\n\n beforeEach(() => {\n ldService = { getClient: jest.fn().mockReturnValue(ldServiceClient) };\n singletons?.push({ provide: LD_SERVICE_TOKEN, useValue: ldService });\n });\n\n test('uses LDService client', async () => {\n subject();\n\n await waitFor(() =>\n expectLDProvider({ clientSideID: props.clientSideID, ldClient: ldServiceClient })\n );\n\n expect(ldServiceClient.waitForInitialization).toHaveBeenCalled();\n });\n\n describe('when LDService has options', () => {\n const options: LDService['options'] = { hash: 'abc123' };\n\n beforeEach(() => (ldService.options = options));\n\n test('forwards options to LDClient', async () => {\n subject();\n\n await waitFor(() => expectLDProvider());\n\n expect(ldService.getClient).toHaveBeenCalledWith(\n expect.objectContaining({ options })\n );\n });\n });\n\n describe('when application is provided', () => {\n const application = { id: 'some-application', version: '1.2.3' };\n\n beforeEach(() => (ldService.application = application));\n\n test('passes application to LDClient', async () => {\n subject();\n\n await waitFor(() => expectLDProvider());\n\n expect(ldService.getClient).toHaveBeenCalledWith(\n expect.objectContaining({\n options: { application },\n })\n );\n });\n });\n });\n});\n"],"names":["Log","Provider","useDependencies","render","screen","waitFor","act","Fragment","FeatureFlagStore","getLDClient","LDProvider","LD_SERVICE_TOKEN","formatProps","props","Object","entries","map","key","value","toString","mockComponent","name","children","div","jest","mock","fn","describe","log","error","ldClient","on","off","waitUntilReady","mockResolvedValue","undefined","waitForInitialization","singletons","providedFeatureFlagStore","renderCount","Component","beforeEach","clearAllMocks","mocked","mockReturnValue","clientSideID","projectName","context","kind","tree","subject","expectLDProvider","expect","getByText","toBeInTheDocument","test","toHaveBeenCalled","flags","EnableThing","toBeDefined","initializedCallback","mockImplementation","event","callback","not","toBe","unmount","toHaveBeenCalledWith","mockRejectedValue","push","provide","useValue","category","code","message","queryByText","rerender","objectContaining","ldServiceClient","ldService","getClient","options","hash","application","id","version"],"mappings":";AAAA,SAASA,GAAG,QAAQ,4BAA4B;AAChD,SAASC,QAAQ,EAAEC,eAAe,QAAQ,0BAA0B;AACpE,OAAO,4BAA4B;AACnC,SAASC,MAAM,EAAEC,MAAM,EAAEC,OAAO,QAAQ,yBAAyB;AAEjE,SAASC,GAAG,EAAMC,QAAQ,QAAmB,QAAQ;AACrD,SAASC,gBAAgB,QAAQ,wBAAwB;AACzD,SAASC,WAAW,QAAQ,mBAAmB;AAC/C,SAASC,UAAU,QAAQ,iBAAiB;AAC5C,SAASC,gBAAgB,QAAmB,WAAW;AAEvD,SAASC,YAAYC,KAA0B;IAC3C,OAAOC,OAAOC,OAAO,CAACF,OAAOG,GAAG,CAAC,CAAC,CAACC,KAAKC,MAAM;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,IACV8C,iBAAiB;oBAAEN,cAAchC,MAAMgC,YAAY;oBAAEf,UAAUgD;gBAAgB;YAGnF1B,OAAO0B,gBAAgB1C,qBAAqB,EAAEoB,gBAAgB;QAClE;QAEA7B,SAAS,8BAA8B;YACnC,MAAMsD,UAAgC;gBAAEC,MAAM;YAAS;YAEvDzC,WAAW,IAAOsC,UAAUE,OAAO,GAAGA;YAEtC1B,KAAK,gCAAgC;gBACjCL;gBAEA,MAAM7C,QAAQ,IAAM8C;gBAEpBC,OAAO2B,UAAUC,SAAS,EAAEb,oBAAoB,CAC5Cf,OAAOyB,gBAAgB,CAAC;oBAAEI;gBAAQ;YAE1C;QACJ;QAEAtD,SAAS,gCAAgC;YACrC,MAAMwD,cAAc;gBAAEC,IAAI;gBAAoBC,SAAS;YAAQ;YAE/D5C,WAAW,IAAOsC,UAAUI,WAAW,GAAGA;YAE1C5B,KAAK,kCAAkC;gBACnCL;gBAEA,MAAM7C,QAAQ,IAAM8C;gBAEpBC,OAAO2B,UAAUC,SAAS,EAAEb,oBAAoB,CAC5Cf,OAAOyB,gBAAgB,CAAC;oBACpBI,SAAS;wBAAEE;oBAAY;gBAC3B;YAER;QACJ;IACJ;AACJ"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ld-service-provider.test.d.ts","sourceRoot":"","sources":["../../src/tests/ld-service-provider.test.tsx"],"names":[],"mappings":""}
|
|
@@ -1,227 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { Provider, useDependencies } from '@servicetitan/react-ioc';
|
|
3
|
-
import { render } from '@testing-library/react';
|
|
4
|
-
import { getLDClient } from '../get-ld-client';
|
|
5
|
-
import { getVersionClient } from '../get-version-client';
|
|
6
|
-
import { LDServiceProvider } from '../ld-service-provider';
|
|
7
|
-
import { LD_SERVICE_TOKEN } from '../types';
|
|
8
|
-
describe(`[launchdarkly-service] ${LDServiceProvider.name}`, ()=>{
|
|
9
|
-
let providedApplication;
|
|
10
|
-
let providedOptions;
|
|
11
|
-
let providedLDService;
|
|
12
|
-
let singletons;
|
|
13
|
-
let props;
|
|
14
|
-
const Component = ()=>{
|
|
15
|
-
[providedLDService] = useDependencies(LD_SERVICE_TOKEN);
|
|
16
|
-
providedApplication = providedLDService.application;
|
|
17
|
-
providedOptions = providedLDService.options;
|
|
18
|
-
return null;
|
|
19
|
-
};
|
|
20
|
-
beforeEach(()=>{
|
|
21
|
-
singletons = [];
|
|
22
|
-
props = {};
|
|
23
|
-
});
|
|
24
|
-
const subject = ()=>{
|
|
25
|
-
return render(/*#__PURE__*/ _jsx(Provider, {
|
|
26
|
-
singletons: singletons,
|
|
27
|
-
children: /*#__PURE__*/ _jsx(LDServiceProvider, {
|
|
28
|
-
...props,
|
|
29
|
-
children: /*#__PURE__*/ _jsx(Component, {})
|
|
30
|
-
})
|
|
31
|
-
}));
|
|
32
|
-
};
|
|
33
|
-
test('provides LDService instance', ()=>{
|
|
34
|
-
subject();
|
|
35
|
-
expect(providedLDService).toEqual({
|
|
36
|
-
getClient: getLDClient,
|
|
37
|
-
getVersionClient
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
test('provides no application', ()=>{
|
|
41
|
-
subject();
|
|
42
|
-
expect(providedApplication).toBeUndefined();
|
|
43
|
-
});
|
|
44
|
-
test('provides no options', ()=>{
|
|
45
|
-
subject();
|
|
46
|
-
expect(providedOptions).toBeUndefined();
|
|
47
|
-
});
|
|
48
|
-
describe('when LDService is already provided', ()=>{
|
|
49
|
-
const ldService = {
|
|
50
|
-
getClient: jest.fn()
|
|
51
|
-
};
|
|
52
|
-
beforeEach(()=>singletons === null || singletons === void 0 ? void 0 : singletons.push({
|
|
53
|
-
provide: LD_SERVICE_TOKEN,
|
|
54
|
-
useValue: ldService
|
|
55
|
-
}));
|
|
56
|
-
test('provides existing LDService', ()=>{
|
|
57
|
-
subject();
|
|
58
|
-
expect(providedLDService).toBe(ldService);
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
describe('with "application" prop', ()=>{
|
|
62
|
-
const application = {
|
|
63
|
-
id: 'some-application',
|
|
64
|
-
version: '1.0.0'
|
|
65
|
-
};
|
|
66
|
-
beforeEach(()=>props.application = application);
|
|
67
|
-
function itProvidesApplication() {
|
|
68
|
-
test('provides application', ()=>{
|
|
69
|
-
subject();
|
|
70
|
-
expect(providedApplication).toBe(application);
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
itProvidesApplication();
|
|
74
|
-
describe('when rerendered with new application', ()=>{
|
|
75
|
-
const newApplication = {
|
|
76
|
-
id: 'foo'
|
|
77
|
-
};
|
|
78
|
-
beforeEach(()=>{
|
|
79
|
-
const { rerender } = subject();
|
|
80
|
-
rerender(/*#__PURE__*/ _jsx(LDServiceProvider, {
|
|
81
|
-
application: newApplication,
|
|
82
|
-
children: /*#__PURE__*/ _jsx(Component, {})
|
|
83
|
-
}));
|
|
84
|
-
});
|
|
85
|
-
test('provides new application', ()=>{
|
|
86
|
-
expect(providedApplication).toBe(newApplication);
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
describe('when rerendered without application', ()=>{
|
|
90
|
-
beforeEach(()=>{
|
|
91
|
-
const { rerender } = subject();
|
|
92
|
-
rerender(/*#__PURE__*/ _jsx(LDServiceProvider, {
|
|
93
|
-
children: /*#__PURE__*/ _jsx(Component, {})
|
|
94
|
-
}));
|
|
95
|
-
});
|
|
96
|
-
test('drops application', ()=>{
|
|
97
|
-
expect(providedApplication).toBeUndefined();
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
describe('when LDService is already provided', ()=>{
|
|
101
|
-
let providedLDService;
|
|
102
|
-
beforeEach(()=>{
|
|
103
|
-
providedLDService = {
|
|
104
|
-
getClient: jest.fn()
|
|
105
|
-
};
|
|
106
|
-
singletons === null || singletons === void 0 ? void 0 : singletons.push({
|
|
107
|
-
provide: LD_SERVICE_TOKEN,
|
|
108
|
-
useValue: providedLDService
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
itProvidesApplication();
|
|
112
|
-
test('unmount resets LDService.application', ()=>{
|
|
113
|
-
const { unmount } = subject();
|
|
114
|
-
unmount();
|
|
115
|
-
expect(providedLDService.application).toBeUndefined();
|
|
116
|
-
});
|
|
117
|
-
describe('when provided LDService has application', ()=>{
|
|
118
|
-
const otherApplication = {
|
|
119
|
-
id: 'other-application',
|
|
120
|
-
version: '2.0.0'
|
|
121
|
-
};
|
|
122
|
-
beforeEach(()=>providedLDService.application = otherApplication);
|
|
123
|
-
test('provides inherited application', ()=>{
|
|
124
|
-
subject();
|
|
125
|
-
expect(providedApplication).toBe(otherApplication);
|
|
126
|
-
});
|
|
127
|
-
test('unmount preserves LDService.application', ()=>{
|
|
128
|
-
const { unmount } = subject();
|
|
129
|
-
unmount();
|
|
130
|
-
expect(providedLDService.application).toBe(otherApplication);
|
|
131
|
-
});
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
describe('with "options" prop', ()=>{
|
|
136
|
-
const options = {
|
|
137
|
-
hash: 'abc123'
|
|
138
|
-
};
|
|
139
|
-
beforeEach(()=>props.options = options);
|
|
140
|
-
function itProvidesOptions() {
|
|
141
|
-
test('provides options', ()=>{
|
|
142
|
-
subject();
|
|
143
|
-
expect(providedOptions).toBe(options);
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
itProvidesOptions();
|
|
147
|
-
describe('when rerendered with new options', ()=>{
|
|
148
|
-
const newOptions = {
|
|
149
|
-
evaluationReasons: true
|
|
150
|
-
};
|
|
151
|
-
beforeEach(()=>{
|
|
152
|
-
const { rerender } = subject();
|
|
153
|
-
rerender(/*#__PURE__*/ _jsx(LDServiceProvider, {
|
|
154
|
-
options: newOptions,
|
|
155
|
-
children: /*#__PURE__*/ _jsx(Component, {})
|
|
156
|
-
}));
|
|
157
|
-
});
|
|
158
|
-
test('provides new options', ()=>{
|
|
159
|
-
expect(providedOptions).toBe(newOptions);
|
|
160
|
-
});
|
|
161
|
-
});
|
|
162
|
-
describe('when rerendered without options', ()=>{
|
|
163
|
-
beforeEach(()=>{
|
|
164
|
-
const { rerender } = subject();
|
|
165
|
-
rerender(/*#__PURE__*/ _jsx(LDServiceProvider, {
|
|
166
|
-
children: /*#__PURE__*/ _jsx(Component, {})
|
|
167
|
-
}));
|
|
168
|
-
});
|
|
169
|
-
test('drops options', ()=>{
|
|
170
|
-
expect(providedOptions).toBeUndefined();
|
|
171
|
-
});
|
|
172
|
-
});
|
|
173
|
-
describe('when LDService is already provided', ()=>{
|
|
174
|
-
let parentLDService;
|
|
175
|
-
beforeEach(()=>{
|
|
176
|
-
parentLDService = {
|
|
177
|
-
getClient: jest.fn()
|
|
178
|
-
};
|
|
179
|
-
singletons === null || singletons === void 0 ? void 0 : singletons.push({
|
|
180
|
-
provide: LD_SERVICE_TOKEN,
|
|
181
|
-
useValue: parentLDService
|
|
182
|
-
});
|
|
183
|
-
});
|
|
184
|
-
itProvidesOptions();
|
|
185
|
-
test('unmount resets LDService.options', ()=>{
|
|
186
|
-
const { unmount } = subject();
|
|
187
|
-
unmount();
|
|
188
|
-
expect(parentLDService.options).toBeUndefined();
|
|
189
|
-
});
|
|
190
|
-
describe('when provided LDService has options', ()=>{
|
|
191
|
-
const otherOptions = {
|
|
192
|
-
evaluationReasons: true
|
|
193
|
-
};
|
|
194
|
-
beforeEach(()=>parentLDService.options = otherOptions);
|
|
195
|
-
test('provides inherited options', ()=>{
|
|
196
|
-
subject();
|
|
197
|
-
expect(providedOptions).toBe(otherOptions);
|
|
198
|
-
});
|
|
199
|
-
test('unmount preserves LDService.options', ()=>{
|
|
200
|
-
const { unmount } = subject();
|
|
201
|
-
unmount();
|
|
202
|
-
expect(parentLDService.options).toBe(otherOptions);
|
|
203
|
-
});
|
|
204
|
-
});
|
|
205
|
-
});
|
|
206
|
-
});
|
|
207
|
-
describe('with both "application" and "options" props', ()=>{
|
|
208
|
-
const application = {
|
|
209
|
-
id: 'some-application',
|
|
210
|
-
version: '1.0.0'
|
|
211
|
-
};
|
|
212
|
-
const options = {
|
|
213
|
-
hash: 'abc123'
|
|
214
|
-
};
|
|
215
|
-
beforeEach(()=>{
|
|
216
|
-
props.application = application;
|
|
217
|
-
props.options = options;
|
|
218
|
-
});
|
|
219
|
-
test('provides both application and options', ()=>{
|
|
220
|
-
subject();
|
|
221
|
-
expect(providedApplication).toBe(application);
|
|
222
|
-
expect(providedOptions).toBe(options);
|
|
223
|
-
});
|
|
224
|
-
});
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
//# sourceMappingURL=ld-service-provider.test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/tests/ld-service-provider.test.tsx"],"sourcesContent":["import { Provider, useDependencies } from '@servicetitan/react-ioc';\nimport { render } from '@testing-library/react';\nimport { LDOptions } from 'launchdarkly-js-client-sdk';\nimport { ComponentProps, FC } from 'react';\nimport { getLDClient } from '../get-ld-client';\nimport { getVersionClient } from '../get-version-client';\nimport { LDServiceProvider } from '../ld-service-provider';\nimport { LD_SERVICE_TOKEN, LDApplication, LDService } from '../types';\n\ndescribe(`[launchdarkly-service] ${LDServiceProvider.name}`, () => {\n let providedApplication: LDApplication | undefined;\n let providedOptions: LDOptions | undefined;\n let providedLDService: LDService;\n let singletons: Parameters<typeof Provider>[0]['singletons'];\n let props: ComponentProps<typeof LDServiceProvider>;\n\n const Component: FC = () => {\n [providedLDService] = useDependencies(LD_SERVICE_TOKEN);\n providedApplication = providedLDService.application;\n providedOptions = providedLDService.options;\n return null;\n };\n\n beforeEach(() => {\n singletons = [];\n props = {};\n });\n\n const subject = () => {\n return render(\n <Provider singletons={singletons}>\n <LDServiceProvider {...props}>\n <Component />\n </LDServiceProvider>\n </Provider>\n );\n };\n\n test('provides LDService instance', () => {\n subject();\n\n expect(providedLDService).toEqual({ getClient: getLDClient, getVersionClient });\n });\n\n test('provides no application', () => {\n subject();\n\n expect(providedApplication).toBeUndefined();\n });\n\n test('provides no options', () => {\n subject();\n\n expect(providedOptions).toBeUndefined();\n });\n\n describe('when LDService is already provided', () => {\n const ldService: LDService = { getClient: jest.fn() };\n\n beforeEach(() => singletons?.push({ provide: LD_SERVICE_TOKEN, useValue: ldService }));\n\n test('provides existing LDService', () => {\n subject();\n\n expect(providedLDService).toBe(ldService);\n });\n });\n\n describe('with \"application\" prop', () => {\n const application = { id: 'some-application', version: '1.0.0' };\n\n beforeEach(() => (props.application = application));\n\n function itProvidesApplication() {\n test('provides application', () => {\n subject();\n\n expect(providedApplication).toBe(application);\n });\n }\n\n itProvidesApplication();\n\n describe('when rerendered with new application', () => {\n const newApplication = { id: 'foo' };\n\n beforeEach(() => {\n const { rerender } = subject();\n\n rerender(\n <LDServiceProvider application={newApplication}>\n <Component />\n </LDServiceProvider>\n );\n });\n\n test('provides new application', () => {\n expect(providedApplication).toBe(newApplication);\n });\n });\n\n describe('when rerendered without application', () => {\n beforeEach(() => {\n const { rerender } = subject();\n\n rerender(\n <LDServiceProvider>\n <Component />\n </LDServiceProvider>\n );\n });\n\n test('drops application', () => {\n expect(providedApplication).toBeUndefined();\n });\n });\n\n describe('when LDService is already provided', () => {\n let providedLDService: LDService;\n\n beforeEach(() => {\n providedLDService = { getClient: jest.fn() };\n singletons?.push({ provide: LD_SERVICE_TOKEN, useValue: providedLDService });\n });\n\n itProvidesApplication();\n\n test('unmount resets LDService.application', () => {\n const { unmount } = subject();\n unmount();\n\n expect(providedLDService.application).toBeUndefined();\n });\n\n describe('when provided LDService has application', () => {\n const otherApplication = { id: 'other-application', version: '2.0.0' };\n\n beforeEach(() => (providedLDService.application = otherApplication));\n\n test('provides inherited application', () => {\n subject();\n\n expect(providedApplication).toBe(otherApplication);\n });\n\n test('unmount preserves LDService.application', () => {\n const { unmount } = subject();\n unmount();\n\n expect(providedLDService.application).toBe(otherApplication);\n });\n });\n });\n });\n\n describe('with \"options\" prop', () => {\n const options: LDOptions = { hash: 'abc123' };\n\n beforeEach(() => (props.options = options));\n\n function itProvidesOptions() {\n test('provides options', () => {\n subject();\n\n expect(providedOptions).toBe(options);\n });\n }\n\n itProvidesOptions();\n\n describe('when rerendered with new options', () => {\n const newOptions: LDOptions = { evaluationReasons: true };\n\n beforeEach(() => {\n const { rerender } = subject();\n\n rerender(\n <LDServiceProvider options={newOptions}>\n <Component />\n </LDServiceProvider>\n );\n });\n\n test('provides new options', () => {\n expect(providedOptions).toBe(newOptions);\n });\n });\n\n describe('when rerendered without options', () => {\n beforeEach(() => {\n const { rerender } = subject();\n\n rerender(\n <LDServiceProvider>\n <Component />\n </LDServiceProvider>\n );\n });\n\n test('drops options', () => {\n expect(providedOptions).toBeUndefined();\n });\n });\n\n describe('when LDService is already provided', () => {\n let parentLDService: LDService;\n\n beforeEach(() => {\n parentLDService = { getClient: jest.fn() };\n singletons?.push({ provide: LD_SERVICE_TOKEN, useValue: parentLDService });\n });\n\n itProvidesOptions();\n\n test('unmount resets LDService.options', () => {\n const { unmount } = subject();\n unmount();\n\n expect(parentLDService.options).toBeUndefined();\n });\n\n describe('when provided LDService has options', () => {\n const otherOptions: LDOptions = { evaluationReasons: true };\n\n beforeEach(() => (parentLDService.options = otherOptions));\n\n test('provides inherited options', () => {\n subject();\n\n expect(providedOptions).toBe(otherOptions);\n });\n\n test('unmount preserves LDService.options', () => {\n const { unmount } = subject();\n unmount();\n\n expect(parentLDService.options).toBe(otherOptions);\n });\n });\n });\n });\n\n describe('with both \"application\" and \"options\" props', () => {\n const application = { id: 'some-application', version: '1.0.0' };\n const options: LDOptions = { hash: 'abc123' };\n\n beforeEach(() => {\n props.application = application;\n props.options = options;\n });\n\n test('provides both application and options', () => {\n subject();\n\n expect(providedApplication).toBe(application);\n expect(providedOptions).toBe(options);\n });\n });\n});\n"],"names":["Provider","useDependencies","render","getLDClient","getVersionClient","LDServiceProvider","LD_SERVICE_TOKEN","describe","name","providedApplication","providedOptions","providedLDService","singletons","props","Component","application","options","beforeEach","subject","test","expect","toEqual","getClient","toBeUndefined","ldService","jest","fn","push","provide","useValue","toBe","id","version","itProvidesApplication","newApplication","rerender","unmount","otherApplication","hash","itProvidesOptions","newOptions","evaluationReasons","parentLDService","otherOptions"],"mappings":";AAAA,SAASA,QAAQ,EAAEC,eAAe,QAAQ,0BAA0B;AACpE,SAASC,MAAM,QAAQ,yBAAyB;AAGhD,SAASC,WAAW,QAAQ,mBAAmB;AAC/C,SAASC,gBAAgB,QAAQ,wBAAwB;AACzD,SAASC,iBAAiB,QAAQ,yBAAyB;AAC3D,SAASC,gBAAgB,QAAkC,WAAW;AAEtEC,SAAS,CAAC,uBAAuB,EAAEF,kBAAkBG,IAAI,EAAE,EAAE;IACzD,IAAIC;IACJ,IAAIC;IACJ,IAAIC;IACJ,IAAIC;IACJ,IAAIC;IAEJ,MAAMC,YAAgB;QAClB,CAACH,kBAAkB,GAAGV,gBAAgBK;QACtCG,sBAAsBE,kBAAkBI,WAAW;QACnDL,kBAAkBC,kBAAkBK,OAAO;QAC3C,OAAO;IACX;IAEAC,WAAW;QACPL,aAAa,EAAE;QACfC,QAAQ,CAAC;IACb;IAEA,MAAMK,UAAU;QACZ,OAAOhB,qBACH,KAACF;YAASY,YAAYA;sBAClB,cAAA,KAACP;gBAAmB,GAAGQ,KAAK;0BACxB,cAAA,KAACC;;;IAIjB;IAEAK,KAAK,+BAA+B;QAChCD;QAEAE,OAAOT,mBAAmBU,OAAO,CAAC;YAAEC,WAAWnB;YAAaC;QAAiB;IACjF;IAEAe,KAAK,2BAA2B;QAC5BD;QAEAE,OAAOX,qBAAqBc,aAAa;IAC7C;IAEAJ,KAAK,uBAAuB;QACxBD;QAEAE,OAAOV,iBAAiBa,aAAa;IACzC;IAEAhB,SAAS,sCAAsC;QAC3C,MAAMiB,YAAuB;YAAEF,WAAWG,KAAKC,EAAE;QAAG;QAEpDT,WAAW,IAAML,uBAAAA,iCAAAA,WAAYe,IAAI,CAAC;gBAAEC,SAAStB;gBAAkBuB,UAAUL;YAAU;QAEnFL,KAAK,+BAA+B;YA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gBAC5C,MAAMoC,eAA0B;oBAAEF,mBAAmB;gBAAK;gBAE1DxB,WAAW,IAAOyB,gBAAgB1B,OAAO,GAAG2B;gBAE5CxB,KAAK,8BAA8B;oBAC/BD;oBAEAE,OAAOV,iBAAiBoB,IAAI,CAACa;gBACjC;gBAEAxB,KAAK,uCAAuC;oBACxC,MAAM,EAAEiB,OAAO,EAAE,GAAGlB;oBACpBkB;oBAEAhB,OAAOsB,gBAAgB1B,OAAO,EAAEc,IAAI,CAACa;gBACzC;YACJ;QACJ;IACJ;IAEApC,SAAS,+CAA+C;QACpD,MAAMQ,cAAc;YAAEgB,IAAI;YAAoBC,SAAS;QAAQ;QAC/D,MAAMhB,UAAqB;YAAEsB,MAAM;QAAS;QAE5CrB,WAAW;YACPJ,MAAME,WAAW,GAAGA;YACpBF,MAAMG,OAAO,GAAGA;QACpB;QAEAG,KAAK,yCAAyC;YAC1CD;YAEAE,OAAOX,qBAAqBqB,IAAI,CAACf;YACjCK,OAAOV,iBAAiBoB,IAAI,CAACd;QACjC;IACJ;AACJ"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"with-feature-flag.test.d.ts","sourceRoot":"","sources":["../../src/tests/with-feature-flag.test.tsx"],"names":[],"mappings":"AACA,OAAO,2BAA2B,CAAC"}
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { Provider } from '@servicetitan/react-ioc';
|
|
3
|
-
import '@testing-library/jest-dom';
|
|
4
|
-
import { render, screen } from '@testing-library/react';
|
|
5
|
-
import { FeatureFlagStore } from '../feature-flag-store';
|
|
6
|
-
import { WithFeatureFlag } from '../with-feature-flag';
|
|
7
|
-
describe(`[launchdarkly-service] ${WithFeatureFlag.name}`, ()=>{
|
|
8
|
-
const flagKey = 'foo';
|
|
9
|
-
const childContent = 'Flag is ON';
|
|
10
|
-
let props;
|
|
11
|
-
let flagValue;
|
|
12
|
-
const mockFeatureFlagStore = {
|
|
13
|
-
get flags () {
|
|
14
|
-
return {
|
|
15
|
-
[flagKey]: flagValue
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
beforeEach(()=>{
|
|
20
|
-
props = {
|
|
21
|
-
flagKey,
|
|
22
|
-
children: childContent
|
|
23
|
-
};
|
|
24
|
-
});
|
|
25
|
-
const subject = ()=>render(/*#__PURE__*/ _jsx(Provider, {
|
|
26
|
-
singletons: [
|
|
27
|
-
{
|
|
28
|
-
provide: FeatureFlagStore,
|
|
29
|
-
useValue: mockFeatureFlagStore
|
|
30
|
-
}
|
|
31
|
-
],
|
|
32
|
-
children: /*#__PURE__*/ _jsx(WithFeatureFlag, {
|
|
33
|
-
...props
|
|
34
|
-
})
|
|
35
|
-
}));
|
|
36
|
-
function itRendersNothing() {
|
|
37
|
-
test('renders nothing', ()=>{
|
|
38
|
-
subject();
|
|
39
|
-
expect(screen.queryByText(childContent)).not.toBeInTheDocument();
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
function itRendersChildren() {
|
|
43
|
-
test('renders children', ()=>{
|
|
44
|
-
subject();
|
|
45
|
-
expect(screen.getByText(childContent)).toBeInTheDocument();
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
itRendersNothing();
|
|
49
|
-
describe('when flag is turned on', ()=>{
|
|
50
|
-
beforeEach(()=>flagValue = true);
|
|
51
|
-
itRendersChildren();
|
|
52
|
-
describe('with on prop', ()=>{
|
|
53
|
-
const onContent = 'Flag is ENABLED';
|
|
54
|
-
beforeEach(()=>props.on = ()=>onContent);
|
|
55
|
-
test('renders on content', ()=>{
|
|
56
|
-
subject();
|
|
57
|
-
expect(screen.getByText(onContent)).toBeInTheDocument();
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
describe('when flag is turned off', ()=>{
|
|
62
|
-
beforeEach(()=>flagValue = false);
|
|
63
|
-
itRendersNothing();
|
|
64
|
-
describe('with off prop', ()=>{
|
|
65
|
-
const offContent = 'flag is OFF';
|
|
66
|
-
beforeEach(()=>props.off = ()=>offContent);
|
|
67
|
-
test('renders off content', ()=>{
|
|
68
|
-
subject();
|
|
69
|
-
expect(screen.getByText(offContent)).toBeInTheDocument();
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
describe('with flag value', ()=>{
|
|
74
|
-
beforeEach(()=>props.value = 'bar');
|
|
75
|
-
itRendersNothing();
|
|
76
|
-
describe('when flag matches value', ()=>{
|
|
77
|
-
beforeEach(()=>flagValue = props.value);
|
|
78
|
-
itRendersChildren();
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
//# sourceMappingURL=with-feature-flag.test.js.map
|