Skip to content

Commit 2020625

Browse files
committed
fix: make all tests work again
1 parent 9d36156 commit 2020625

File tree

1 file changed

+75
-100
lines changed

1 file changed

+75
-100
lines changed

test/useWizard.test.tsx

Lines changed: 75 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
import { waitFor } from '@testing-library/react';
21
import { act, renderHook } from '@testing-library/react-hooks';
32
import * as React from 'react';
43

54
import { useWizard, Wizard } from '../src';
65

76
// Common render use wizard hook
8-
const renderUseWizardHook = () => {
7+
const renderUseWizardHook = (initialStartIndex = 0) => {
98
return renderHook(() => useWizard(), {
10-
wrapper: ({ children }: { children: React.ReactNode }) => {
11-
return <Wizard>{children}</Wizard>;
12-
},
139
initialProps: {
14-
children: (
15-
<>
16-
<p>step 1</p>
17-
<p>step 2</p>
18-
</>
19-
),
10+
startIndex: initialStartIndex,
2011
},
12+
wrapper: ({ children, startIndex }) => (
13+
<Wizard startIndex={startIndex}>
14+
<p>step 1 {children}</p>
15+
<p>step 2 {children}</p>
16+
</Wizard>
17+
),
2118
});
2219
};
2320

@@ -34,150 +31,128 @@ describe('useWizard', () => {
3431
expect(result.current.activeStep).toBe(0);
3532
});
3633

34+
test('should set active step to one', () => {
35+
const { result } = renderUseWizardHook(1);
36+
37+
expect(result.current.activeStep).toBe(1);
38+
});
39+
3740
test('should be first step', () => {
3841
const { result } = renderUseWizardHook();
3942

4043
expect(result.current.isFirstStep).toBe(true);
4144
});
4245

43-
test('should be second step after next step', () => {
44-
const { result } = renderUseWizardHook();
46+
test('should be second step after next step', async () => {
47+
const { result, waitForNextUpdate } = renderUseWizardHook();
4548

49+
// According to https://react-hooks-testing-library.com/usage/advanced-hooks#async it's
50+
// not necessary to wrap this call in `act`, however this doesn't seem to be the case
4651
act(() => {
4752
result.current.nextStep();
4853
});
4954

50-
// Wait for an element to appear
51-
waitFor(() => {
52-
expect(result.current.isFirstStep).toBe(false);
53-
expect(result.current.isLastStep).toBe(true);
54-
});
55+
await waitForNextUpdate();
56+
57+
expect(result.current.isFirstStep).toBe(false);
58+
expect(result.current.isLastStep).toBe(true);
5559
});
5660

57-
test('should call callback on next step', () => {
61+
test('should invoke `handleStep` handler on next step', async () => {
5862
const callback = jest.fn();
5963

60-
const { result } = renderUseWizardHook();
64+
const { result, waitForNextUpdate } = renderUseWizardHook();
6165

6266
act(() => {
6367
result.current.handleStep(callback);
64-
});
65-
66-
// Wait for an element to appear
67-
waitFor(() => {
6868
result.current.nextStep();
69-
expect(callback).toBeCalled();
7069
});
70+
71+
await waitForNextUpdate();
72+
73+
expect(callback).toBeCalled();
7174
});
7275

73-
test('should set active step to one on next step', () => {
74-
const { result } = renderUseWizardHook();
76+
test('should set active step to one on next step', async () => {
77+
const { result, waitForNextUpdate } = renderUseWizardHook();
7578

76-
// Wait for an element to appear
77-
waitFor(() => {
79+
act(() => {
7880
result.current.nextStep();
79-
expect(result.current.activeStep).toBe(1);
8081
});
82+
83+
await waitForNextUpdate();
84+
85+
expect(result.current.activeStep).toBe(1);
8186
});
8287

83-
test('should set is last stap on next step', () => {
84-
const { result } = renderUseWizardHook();
88+
test('should set `isLastStap` on next step', async () => {
89+
const { result, waitForNextUpdate } = renderUseWizardHook();
8590

8691
// Wait for an element to appear
87-
waitFor(() => {
92+
act(() => {
8893
result.current.nextStep();
89-
expect(result.current.isFirstStep).toBe(false);
90-
expect(result.current.isLastStep).toBe(true);
9194
});
95+
96+
await waitForNextUpdate();
97+
98+
expect(result.current.isFirstStep).toBe(false);
99+
expect(result.current.isLastStep).toBe(true);
92100
});
93101

94-
test('should go to passed step index on next step', () => {
95-
const { result } = renderHook(() => useWizard(), {
96-
wrapper: ({ children }: { children: React.ReactNode }) => {
97-
return <Wizard>{children}</Wizard>;
98-
},
99-
initialProps: {
100-
children: (
101-
<>
102-
<p>step 1</p>
103-
<p>step 2</p>
104-
<p>step 3</p>
105-
</>
106-
),
107-
},
102+
test('should go to passed step index on next step', async () => {
103+
const { result, waitForNextUpdate } = renderHook(() => useWizard(), {
104+
wrapper: ({ children }) => (
105+
<Wizard>
106+
<p>step 1 {children}</p>
107+
<p>step 2 {children}</p>
108+
<p>step 3 {children}</p>
109+
</Wizard>
110+
),
108111
});
109-
// Wait for an element to appear
110-
waitFor(() => {
112+
113+
act(() => {
111114
result.current.nextStep(2);
112-
expect(result.current.isFirstStep).toBe(false);
113-
expect(result.current.isLastStep).toBe(true);
114115
});
116+
117+
await waitForNextUpdate();
118+
119+
expect(result.current.isFirstStep).toBe(false);
120+
expect(result.current.isLastStep).toBe(true);
115121
});
116122

117-
test('should go to passed step index on next step with handler', () => {
123+
test('should go to passed step index on next step with handler', async () => {
118124
const callback = jest.fn();
119125

120-
const { result } = renderHook(() => useWizard(), {
121-
wrapper: ({ children }: { children: React.ReactNode }) => {
122-
return <Wizard>{children}</Wizard>;
123-
},
124-
initialProps: {
125-
children: (
126-
<>
127-
<p>step 1</p>
128-
<p>step 2</p>
129-
<p>step 3</p>
130-
</>
131-
),
132-
},
126+
const { result, waitForNextUpdate } = renderHook(() => useWizard(), {
127+
wrapper: ({ children }) => (
128+
<Wizard>
129+
<p>step 1 {children}</p>
130+
<p>step 2 {children}</p>
131+
<p>step 3 {children}</p>
132+
</Wizard>
133+
),
133134
});
134135

135136
act(() => {
136137
result.current.handleStep(callback);
137-
});
138-
// Wait for an element to appear
139-
waitFor(() => {
140138
result.current.nextStep(2);
141-
expect(result.current.isFirstStep).toBe(false);
142-
expect(result.current.isLastStep).toBe(true);
143-
expect(callback).toBeCalled();
144139
});
145-
});
146140

147-
test('should go to passed step index on previous step', () => {
148-
const { result } = renderHook(() => useWizard(), {
149-
wrapper: ({ children }: { children: React.ReactNode }) => {
150-
return <Wizard>{children}</Wizard>;
151-
},
152-
initialProps: {
153-
children: (
154-
<>
155-
<p>step 1</p>
156-
<p>step 2</p>
157-
<p>step 3</p>
158-
</>
159-
),
160-
},
161-
});
162-
// Wait for an element to appear
163-
waitFor(() => {
164-
result.current.previousStep(2);
165-
expect(result.current.isFirstStep).toBe(false);
166-
expect(result.current.isLastStep).toBe(true);
167-
});
141+
await waitForNextUpdate();
142+
143+
expect(result.current.isFirstStep).toBe(false);
144+
expect(result.current.isLastStep).toBe(true);
145+
expect(callback).toBeCalled();
168146
});
169147

170-
test('should not go to previous step if first step', () => {
148+
test('should not go to previous step if first step', async () => {
171149
const { result } = renderUseWizardHook();
172150

173151
act(() => {
174152
result.current.previousStep();
175153
});
176154

177155
// Wait for an element to appear
178-
waitFor(() => {
179-
result.current.nextStep();
180-
expect(result.current.activeStep).toBe(0);
181-
});
156+
expect(result.current.activeStep).toBe(0);
182157
});
183158
});

0 commit comments

Comments
 (0)