|
1 | | -import React from "react"; |
2 | | -import { render, fireEvent } from "@testing-library/react"; |
3 | | -import { Provider } from 'react-redux'; |
4 | | -import StopButton from "./StopButton"; |
5 | | -import store from '../../app/store'; |
| 1 | +import React from "react" |
| 2 | +import { act, render, fireEvent, waitFor } from "@testing-library/react" |
| 3 | +import { Provider } from 'react-redux' |
| 4 | +import StopButton from "./StopButton" |
| 5 | +import store from '../../app/store' |
6 | 6 | import { codeRunHandled, triggerCodeRun } from '../Editor/EditorSlice' |
7 | 7 |
|
| 8 | +beforeEach(() => { |
| 9 | + jest.useFakeTimers() |
| 10 | +}) |
| 11 | + |
| 12 | +afterEach(() => { |
| 13 | + jest.useRealTimers() |
| 14 | +}) |
8 | 15 |
|
9 | 16 | test("Clicking stop button sets codeRunStopped to true", () => { |
10 | 17 | store.dispatch(codeRunHandled()) |
11 | 18 | store.dispatch(triggerCodeRun()) |
12 | 19 |
|
13 | | - const { getByText } = render( |
14 | | - <Provider store={store}> |
15 | | - <StopButton buttonText="Stop Code" /> |
16 | | - </Provider>); |
| 20 | + const component = render( |
| 21 | + <Provider store={store}> |
| 22 | + <StopButton buttonText="Stop Code" /> |
| 23 | + </Provider> |
| 24 | + ) |
17 | 25 |
|
18 | | - const stopButton = getByText(/Stop Code/); |
19 | | - fireEvent.click(stopButton); |
| 26 | + const stopButton = component.getByRole('button') |
| 27 | + fireEvent.click(stopButton) |
20 | 28 |
|
21 | | - expect(store.getState().editor.codeRunStopped).toEqual(true); |
| 29 | + expect(store.getState().editor.codeRunStopped).toEqual(true) |
22 | 30 | }) |
23 | 31 |
|
24 | | -test("Clicking stop button changes it to 'Stopping...' after 100ms", () => { |
| 32 | +test("Clicking stop button changes it to 'Stopping...' after a time out", () => { |
25 | 33 | store.dispatch(codeRunHandled()) |
26 | 34 | store.dispatch(triggerCodeRun()) |
27 | 35 |
|
28 | | - const { getByText } = render( |
29 | | - <Provider store={store}> |
| 36 | + const component = render( |
| 37 | + <Provider store={store}> |
30 | 38 | <StopButton buttonText="Stop Code" /> |
31 | | - </Provider> |
32 | | - ); |
33 | | - const stopButton = getByText(/Stop Code/); |
34 | | - fireEvent.click(stopButton); |
35 | | - expect(stopButton.textContent).toEqual("Stop Code") |
36 | | - new Promise(resolve => setTimeout(resolve, 100)).then( () => |
37 | | - expect(stopButton.textContent).toEqual("Stopping...") |
| 39 | + </Provider> |
38 | 40 | ) |
| 41 | + const stopButton = component.getByRole('button') |
| 42 | + expect(stopButton.textContent).toEqual("Stop Code") |
| 43 | + |
| 44 | + fireEvent.click(stopButton) |
| 45 | + expect(stopButton.textContent).toEqual("Stop Code") |
| 46 | + |
| 47 | + act(() => { jest.runAllTimers(); } ) |
| 48 | + expect(stopButton.textContent).toEqual("Stopping...") |
39 | 49 | }) |
0 commit comments