Skip to content

Commit 66e8652

Browse files
committed
Add tests for on step change
1 parent 9f75a7a commit 66e8652

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

test/useWizard.test.tsx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,58 @@ describe('useWizard', () => {
276276
expect(result.current.isFirstStep).toBe(false);
277277
expect(result.current.isLastStep).toBe(true);
278278
});
279+
280+
describe('onStepChange()', () => {
281+
const renderUseWizardHook = (
282+
onStepChange: (index: number) => void,
283+
startIndex = 0,
284+
) => {
285+
return renderHook(() => useWizard(), {
286+
initialProps: {
287+
startIndex,
288+
onStepChange,
289+
},
290+
wrapper: ({ children, startIndex, onStepChange }) => (
291+
<Wizard startIndex={startIndex} onStepChange={onStepChange}>
292+
<p>step 1 {children}</p>
293+
<p>step 2 {children}</p>
294+
<p>step 3 {children}</p>
295+
</Wizard>
296+
),
297+
});
298+
};
299+
300+
test('should invoke onStepChange when nextStep is called', async () => {
301+
const onStepChange = jest.fn();
302+
const { result, waitForNextUpdate } = renderUseWizardHook(onStepChange);
303+
304+
result.current.nextStep();
305+
306+
await waitForNextUpdate();
307+
308+
expect(onStepChange).toHaveBeenCalledWith(1);
309+
});
310+
311+
test('should invoke onStepChange when previousStep is called', async () => {
312+
const onStepChange = jest.fn();
313+
const { result } = renderUseWizardHook(onStepChange, 1);
314+
315+
act(() => {
316+
result.current.previousStep();
317+
});
318+
319+
expect(onStepChange).toHaveBeenCalledWith(0);
320+
});
321+
322+
test('should invoke onStepChange when goToStep is called', async () => {
323+
const onStepChange = jest.fn();
324+
const { result } = renderUseWizardHook(onStepChange);
325+
326+
act(() => {
327+
result.current.goToStep(1);
328+
});
329+
330+
expect(onStepChange).toHaveBeenCalledWith(1);
331+
});
332+
});
279333
});

0 commit comments

Comments
 (0)