Skip to content

Commit cef8536

Browse files
authored
update proxy-client-js (#139)
1 parent 74fde14 commit cef8536

File tree

4 files changed

+533
-539
lines changed

4 files changed

+533
-539
lines changed

package.json

+10-10
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,23 @@
3535
"author": "",
3636
"license": "Apache-2.0",
3737
"devDependencies": {
38-
"@testing-library/jest-dom": "^5.16.5",
38+
"@testing-library/jest-dom": "^6.1.3",
3939
"@testing-library/react": "^14.0.0",
4040
"@testing-library/react-hooks": "^8.0.1",
41-
"@types/react": "^18.2.11",
42-
"@vitest/coverage-istanbul": "^0.32.0",
43-
"happy-dom": "^9.20.3",
41+
"@types/react": "^18.2.27",
42+
"@vitest/coverage-istanbul": "^0.34.6",
43+
"happy-dom": "^12.9.1",
4444
"jsdom": "^22.1.0",
4545
"react": "^18.2.0",
4646
"react-dom": "^18.2.0",
4747
"react-test-renderer": "^18.2.0",
48-
"typescript": "^5.1.3",
49-
"unleash-proxy-client": "^2.5.0",
50-
"vite": "^4.3.9",
51-
"vite-plugin-dts": "^2.3.0",
52-
"vitest": "^0.32.0"
48+
"typescript": "^5.2.2",
49+
"unleash-proxy-client": "3.0.0-beta.2",
50+
"vite": "^4.4.11",
51+
"vite-plugin-dts": "^3.6.0",
52+
"vitest": "^0.32.4"
5353
},
5454
"peerDependencies": {
55-
"unleash-proxy-client": "^2.5.0"
55+
"unleash-proxy-client": "3.0.0-beta.2"
5656
}
5757
}

src/FlagProvider.test.tsx

+42-90
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
import React, { useContext, useEffect, useState } from 'react';
2-
import { render, screen, RenderOptions } from '@testing-library/react';
3-
import * as UnleashClientModule from 'unleash-proxy-client';
2+
import { render, screen } from '@testing-library/react';
3+
import { type Mock } from 'vitest';
4+
import { UnleashClient, type IVariant, EVENTS } from 'unleash-proxy-client';
45
import FlagProvider from './FlagProvider';
56
import FlagContext from './FlagContext';
6-
77
import '@testing-library/jest-dom';
8-
import { EVENTS } from 'unleash-proxy-client';
9-
interface IFlagProvider {
10-
config: UnleashClientModule.IConfig;
11-
}
12-
interface renderConsumerOptions {
13-
providerProps: IFlagProvider;
14-
renderOptions: RenderOptions;
15-
}
168

179
const getVariantMock = vi.fn().mockReturnValue('A');
1810
const updateContextMock = vi.fn();
@@ -21,7 +13,6 @@ const stopClientMock = vi.fn();
2113
const onMock = vi.fn().mockReturnValue('subscribed');
2214
const offMock = vi.fn();
2315
const isEnabledMock = vi.fn().mockReturnValue(true);
24-
const UnleashClientSpy = vi.spyOn(UnleashClientModule, 'UnleashClient');
2516

2617
const givenConfig = {
2718
appName: 'my-app',
@@ -31,12 +22,7 @@ const givenConfig = {
3122
const givenFlagName = 'test';
3223
const givenContext = { session: 'context' };
3324

34-
beforeEach(() => {
35-
onMock.mockClear();
36-
});
37-
38-
// @ts-expect-error
39-
UnleashClientSpy.mockReturnValue({
25+
const UnleashClientSpy = vi.fn().mockReturnValue({
4026
getVariant: getVariantMock,
4127
updateContext: updateContextMock,
4228
start: startClientMock,
@@ -46,18 +32,24 @@ UnleashClientSpy.mockReturnValue({
4632
off: offMock,
4733
});
4834

35+
vi.mock('unleash-proxy-client', async (importOriginal) => {
36+
const mod = await importOriginal();
37+
38+
return {
39+
...mod as any,
40+
UnleashClient: vi.fn(),
41+
};
42+
});
43+
4944
const noop = () => {};
5045

5146
const FlagConsumerAfterClientInit = () => {
5247
const { updateContext, isEnabled, getVariant, client, on } =
5348
useContext(FlagContext);
5449
const [enabled, setIsEnabled] = useState(false);
55-
const [variant, setVariant] = useState<UnleashClientModule.IVariant | null>(
56-
null
57-
);
50+
const [variant, setVariant] = useState<IVariant | null>(null);
5851
const [context, setContext] = useState<any>('nothing');
59-
const [currentOn, setCurrentOn] =
60-
useState<UnleashClientModule.UnleashClient | null>(null);
52+
const [currentOn, setCurrentOn] = useState<UnleashClient | null>(null);
6153

6254
useEffect(() => {
6355
if (client) {
@@ -82,12 +74,9 @@ const FlagConsumerBeforeClientInit = () => {
8274
const { updateContext, isEnabled, getVariant, client, on } =
8375
useContext(FlagContext);
8476
const [enabled, setIsEnabled] = useState(false);
85-
const [variant, setVariant] = useState<UnleashClientModule.IVariant | null>(
86-
null
87-
);
77+
const [variant, setVariant] = useState<IVariant | null>(null);
8878
const [context, setContext] = useState<any>('nothing');
89-
const [currentOn, setCurrentOn] =
90-
useState<UnleashClientModule.UnleashClient | null>(null);
79+
const [currentOn, setCurrentOn] = useState<UnleashClient | null>(null);
9180

9281
useEffect(() => {
9382
if (!client) {
@@ -101,36 +90,18 @@ const FlagConsumerBeforeClientInit = () => {
10190
return <></>;
10291
};
10392

104-
const renderConsumer = (
105-
ui: any,
106-
{ providerProps, renderOptions }: renderConsumerOptions
107-
) => {
108-
return render(
109-
<FlagProvider config={providerProps.config}>{ui}</FlagProvider>,
110-
renderOptions
111-
);
112-
};
113-
114-
const renderConsumerWithUnleashClient = (
115-
ui: any,
116-
{ providerProps, renderOptions }: renderConsumerOptions
117-
) => {
118-
const client = new UnleashClientModule.UnleashClient(providerProps.config);
119-
return render(
120-
<FlagProvider unleashClient={client}>{ui}</FlagProvider>,
121-
renderOptions
122-
);
123-
};
93+
beforeEach(() => {
94+
onMock.mockClear();
95+
(UnleashClient as Mock).mockImplementation(UnleashClientSpy);
96+
});
12497

12598
test('A consumer that subscribes AFTER client init shows values from provider and calls all the functions', () => {
126-
const providerProps = {
127-
config: givenConfig,
128-
};
129-
130-
renderConsumer(<FlagConsumerAfterClientInit />, {
131-
providerProps,
132-
renderOptions: {},
133-
});
99+
render(
100+
<FlagProvider config={givenConfig}>
101+
<FlagConsumerAfterClientInit />
102+
</FlagProvider>,
103+
{}
104+
);
134105

135106
expect(getVariantMock).toHaveBeenCalledWith(givenFlagName);
136107
expect(isEnabledMock).toHaveBeenCalledWith(givenFlagName);
@@ -150,29 +121,25 @@ test('A consumer that subscribes AFTER client init shows values from provider an
150121
});
151122

152123
test('A consumer that subscribes BEFORE client init shows values from provider and calls all the functions', () => {
153-
const providerProps = {
154-
config: givenConfig,
155-
};
156-
157-
renderConsumer(<FlagConsumerBeforeClientInit />, {
158-
providerProps,
159-
renderOptions: {},
160-
});
124+
render(
125+
<FlagProvider config={givenConfig}>
126+
<FlagConsumerBeforeClientInit />
127+
</FlagProvider>,
128+
{}
129+
);
161130

162131
expect(getVariantMock).toHaveBeenCalledWith(givenFlagName);
163132
expect(isEnabledMock).toHaveBeenCalledWith(givenFlagName);
164133
expect(updateContextMock).toHaveBeenCalledWith(givenContext);
165134
});
166135

167136
test('A consumer should be able to get a variant when the client is passed into the provider as props', () => {
168-
const providerProps = {
169-
config: givenConfig,
170-
};
171-
172-
renderConsumerWithUnleashClient(<FlagConsumerAfterClientInit />, {
173-
providerProps,
174-
renderOptions: {},
175-
});
137+
render(
138+
<FlagProvider unleashClient={new UnleashClient(givenConfig)}>
139+
<FlagConsumerAfterClientInit />
140+
</FlagProvider>,
141+
{}
142+
);
176143

177144
expect(getVariantMock).toHaveBeenCalledWith(givenFlagName);
178145
expect(isEnabledMock).toHaveBeenCalledWith(givenFlagName);
@@ -224,7 +191,6 @@ test('A memoized consumer should not rerender when the context provider values a
224191

225192
test('should update when ready event is sent', () => {
226193
const localMock = vi.fn();
227-
// @ts-expect-error
228194
UnleashClientSpy.mockReturnValue({
229195
getVariant: getVariantMock,
230196
updateContext: updateContextMock,
@@ -235,11 +201,7 @@ test('should update when ready event is sent', () => {
235201
off: offMock,
236202
});
237203

238-
const providerProps = {
239-
config: givenConfig,
240-
};
241-
242-
const client = new UnleashClientModule.UnleashClient(providerProps.config);
204+
const client = new UnleashClient(givenConfig);
243205

244206
render(
245207
<FlagProvider unleashClient={client}>
@@ -258,7 +220,6 @@ test('should update when ready event is sent', () => {
258220

259221
test('should register error when error event is sent', () => {
260222
const localMock = vi.fn();
261-
// @ts-expect-error
262223
UnleashClientSpy.mockReturnValue({
263224
getVariant: getVariantMock,
264225
updateContext: updateContextMock,
@@ -269,11 +230,7 @@ test('should register error when error event is sent', () => {
269230
off: offMock,
270231
});
271232

272-
const providerProps = {
273-
config: givenConfig,
274-
};
275-
276-
const client = new UnleashClientModule.UnleashClient(providerProps.config);
233+
const client = new UnleashClient(givenConfig);
277234

278235
render(
279236
<FlagProvider unleashClient={client}>
@@ -293,7 +250,6 @@ test('should register error when error event is sent', () => {
293250
test('should not start client if startClient is false', () => {
294251
const localMock = vi.fn();
295252
const stopMock = vi.fn();
296-
// @ts-expect-error
297253
UnleashClientSpy.mockReturnValue({
298254
getVariant: getVariantMock,
299255
updateContext: updateContextMock,
@@ -304,11 +260,7 @@ test('should not start client if startClient is false', () => {
304260
off: offMock,
305261
});
306262

307-
const providerProps = {
308-
config: givenConfig,
309-
};
310-
311-
const client = new UnleashClientModule.UnleashClient(providerProps.config);
263+
const client = new UnleashClient(givenConfig);
312264

313265
render(
314266
<FlagProvider unleashClient={client} startClient={false}>

testSetup.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { expect, afterEach } from 'vitest';
22
import { cleanup } from '@testing-library/react';
3-
import matchers from '@testing-library/jest-dom/matchers';
4-
5-
// extends Vitest's expect method with methods from react-testing-library
6-
expect.extend(matchers);
3+
import '@testing-library/jest-dom/vitest'
74

85
// runs a cleanup after each test case (e.g. clearing jsdom)
96
afterEach(() => {

0 commit comments

Comments
 (0)