Skip to content

Commit e6df499

Browse files
committed
Update tests to account for previous state dependency.
1 parent 1e60c36 commit e6df499

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

src/provider.test.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,18 @@ describe('LDProvider', () => {
456456
const mockSetState = jest.spyOn(instance, 'setState');
457457

458458
await instance.componentDidMount();
459-
const setStateFunction = mockSetState.mock?.lastCall?.[0] as (p: ProviderState) => ProviderState;
459+
460+
// Each set of the state depends on the previous state, so to re-create the final state, we need to call the
461+
// setState function for each call.
462+
let finalState = previousState;
463+
464+
for(const call of mockSetState.mock.calls) {
465+
const setStateFunction = call[0] as (p: ProviderState) => ProviderState;
466+
finalState = setStateFunction(finalState);
467+
}
460468

461469
expect(mockLDClient.on).toHaveBeenCalledWith('change', expect.any(Function));
462-
expect(setStateFunction(previousState)).toEqual({
470+
expect(finalState).toMatchObject({
463471
flags: { anotherTestFlag: true, testFlag: false },
464472
unproxiedFlags: { 'another-test-flag': true, 'test-flag': false },
465473
flagKeyMap: { anotherTestFlag: 'another-test-flag', testFlag: 'test-flag' },
@@ -480,10 +488,18 @@ describe('LDProvider', () => {
480488
const mockSetState = jest.spyOn(instance, 'setState');
481489

482490
await instance.componentDidMount();
483-
const setStateFunction = mockSetState.mock?.lastCall?.[0] as (p: ProviderState) => ProviderState;
491+
492+
// Each set of the state depends on the previous state, so to re-create the final state, we need to call the
493+
// setState function for each call.
494+
let finalState = previousState;
495+
496+
for (const call of mockSetState.mock.calls) {
497+
const setStateFunction = call[0] as (p: ProviderState) => ProviderState;
498+
finalState = setStateFunction(finalState);
499+
}
484500

485501
expect(mockLDClient.on).toHaveBeenCalledWith('change', expect.any(Function));
486-
expect(setStateFunction(previousState)).toEqual({
502+
expect(finalState).toMatchObject({
487503
flagKeyMap: {},
488504
unproxiedFlags: { 'another-test-flag': false, 'test-flag': false },
489505
flags: { 'another-test-flag': false, 'test-flag': false },

src/provider.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class LDProvider extends Component<PropsWithChildren<ProviderConfig>, ProviderSt
4848
};
4949
}
5050
}
51-
this.setState(this.state);
5251
}
5352

5453
getReactOptions = () => ({ ...defaultReactOptions, ...this.props.reactOptions });

src/withLDProvider.test.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,17 @@ describe('withLDProvider', () => {
130130
const mockSetState = jest.spyOn(instance, 'setState');
131131

132132
await instance.componentDidMount();
133-
const setStateFunction = mockSetState.mock?.lastCall?.[0] as (p: ProviderState) => ProviderState;
133+
// Each set of the state depends on the previous state, so to re-create the final state, we need to call the
134+
// setState function for each call.
135+
let finalState = previousState;
136+
137+
for (const call of mockSetState.mock.calls) {
138+
const setStateFunction = call[0] as (p: ProviderState) => ProviderState;
139+
finalState = setStateFunction(finalState);
140+
}
134141

135142
expect(mockLDClient.on).toHaveBeenCalledWith('change', expect.any(Function));
136-
expect(setStateFunction(previousState)).toEqual({
143+
expect(finalState).toMatchObject({
137144
flags: { anotherTestFlag: true, testFlag: false },
138145
unproxiedFlags: { 'test-flag': false, 'another-test-flag': true },
139146
flagKeyMap: { testFlag: 'test-flag', anotherTestFlag: 'another-test-flag' },
@@ -149,10 +156,17 @@ describe('withLDProvider', () => {
149156
const mockSetState = jest.spyOn(instance, 'setState');
150157

151158
await instance.componentDidMount();
152-
const setStateFunction = mockSetState.mock?.lastCall?.[0] as (p: ProviderState) => ProviderState;
159+
// Each set of the state depends on the previous state, so to re-create the final state, we need to call the
160+
// setState function for each call.
161+
let finalState = previousState;
162+
163+
for (const call of mockSetState.mock.calls) {
164+
const setStateFunction = call[0] as (p: ProviderState) => ProviderState;
165+
finalState = setStateFunction(finalState);
166+
}
153167

154168
expect(mockLDClient.on).toHaveBeenCalledWith('change', expect.any(Function));
155-
expect(setStateFunction(previousState)).toEqual({
169+
expect(finalState).toMatchObject({
156170
flags: { 'test-flag': false, 'another-test-flag': false },
157171
unproxiedFlags: { 'test-flag': false, 'another-test-flag': false },
158172
flagKeyMap: {},

0 commit comments

Comments
 (0)