|
| 1 | +import { screen, waitFor } from "@testing-library/react-native" |
| 2 | +import { App } from "./App" |
| 3 | +import { renderWithProviders } from "./src/utils/test-utils" |
| 4 | + |
| 5 | +test("App should have correct initial render", () => { |
| 6 | + renderWithProviders(<App />) |
| 7 | + |
| 8 | + // The app should be rendered correctly |
| 9 | + expect(screen.getByText(/learn more redux/i)).toBeOnTheScreen() |
| 10 | + |
| 11 | + // Initial state: count should be 0, incrementValue should be 2 |
| 12 | + expect(screen.getByLabelText("Count")).toHaveTextContent("0") |
| 13 | + expect(screen.getByLabelText("Set increment amount")).toHaveDisplayValue("2") |
| 14 | +}) |
| 15 | + |
| 16 | +test("Increment value and Decrement value should work as expected", async () => { |
| 17 | + const { user } = renderWithProviders(<App />) |
| 18 | + |
| 19 | + // Click on "+" => Count should be 1 |
| 20 | + await user.press(screen.getByLabelText("Increment value")) |
| 21 | + expect(screen.getByLabelText("Count")).toHaveTextContent("1") |
| 22 | + |
| 23 | + // Click on "-" => Count should be 0 |
| 24 | + await user.press(screen.getByLabelText("Decrement value")) |
| 25 | + expect(screen.getByLabelText("Count")).toHaveTextContent("0") |
| 26 | +}) |
| 27 | + |
| 28 | +test("Add Amount should work as expected", async () => { |
| 29 | + const { user } = renderWithProviders(<App />) |
| 30 | + |
| 31 | + // "Add Amount" button is clicked => Count should be 2 |
| 32 | + await user.press(screen.getByText("Add Amount")) |
| 33 | + expect(screen.getByLabelText("Count")).toHaveTextContent("2") |
| 34 | + |
| 35 | + const incrementValueInput = screen.getByLabelText("Set increment amount") |
| 36 | + // incrementValue is 2, click on "Add Amount" => Count should be 4 |
| 37 | + await user.clear(incrementValueInput) |
| 38 | + await user.type(incrementValueInput, "2") |
| 39 | + await user.press(screen.getByText("Add Amount")) |
| 40 | + expect(screen.getByLabelText("Count")).toHaveTextContent("4") |
| 41 | + |
| 42 | + // [Negative number] incrementValue is -1, click on "Add Amount" => Count should be 3 |
| 43 | + await user.clear(incrementValueInput) |
| 44 | + await user.type(incrementValueInput, "-1") |
| 45 | + await user.press(screen.getByText("Add Amount")) |
| 46 | + expect(screen.getByLabelText("Count")).toHaveTextContent("3") |
| 47 | +}) |
| 48 | + |
| 49 | +it("Add Async should work as expected", async () => { |
| 50 | + const { user } = renderWithProviders(<App />) |
| 51 | + |
| 52 | + // "Add Async" button is clicked => Count should be 2 |
| 53 | + await user.press(screen.getByText("Add Async")) |
| 54 | + |
| 55 | + await waitFor(() => { |
| 56 | + expect(screen.getByLabelText("Count")).toHaveTextContent("2") |
| 57 | + }) |
| 58 | + |
| 59 | + const incrementValueInput = screen.getByLabelText("Set increment amount") |
| 60 | + // incrementValue is 2, click on "Add Async" => Count should be 4 |
| 61 | + await user.clear(incrementValueInput) |
| 62 | + await user.type(incrementValueInput, "2") |
| 63 | + |
| 64 | + await user.press(screen.getByText("Add Async")) |
| 65 | + await waitFor(() => { |
| 66 | + expect(screen.getByLabelText("Count")).toHaveTextContent("4") |
| 67 | + }) |
| 68 | + |
| 69 | + // [Negative number] incrementValue is -1, click on "Add Async" => Count should be 3 |
| 70 | + await user.clear(incrementValueInput) |
| 71 | + await user.type(incrementValueInput, "-1") |
| 72 | + await user.press(screen.getByText("Add Async")) |
| 73 | + await waitFor(() => { |
| 74 | + expect(screen.getByLabelText("Count")).toHaveTextContent("3") |
| 75 | + }) |
| 76 | +}) |
| 77 | + |
| 78 | +test("Add If Odd should work as expected", async () => { |
| 79 | + const { user } = renderWithProviders(<App />) |
| 80 | + |
| 81 | + // "Add If Odd" button is clicked => Count should stay 0 |
| 82 | + await user.press(screen.getByText("Add If Odd")) |
| 83 | + expect(screen.getByLabelText("Count")).toHaveTextContent("0") |
| 84 | + |
| 85 | + // Click on "+" => Count should be updated to 1 |
| 86 | + await user.press(screen.getByLabelText("Increment value")) |
| 87 | + expect(screen.getByLabelText("Count")).toHaveTextContent("1") |
| 88 | + |
| 89 | + // "Add If Odd" button is clicked => Count should be updated to 3 |
| 90 | + await user.press(screen.getByText("Add If Odd")) |
| 91 | + expect(screen.getByLabelText("Count")).toHaveTextContent("3") |
| 92 | + |
| 93 | + const incrementValueInput = screen.getByLabelText("Set increment amount") |
| 94 | + // incrementValue is 1, click on "Add If Odd" => Count should be updated to 4 |
| 95 | + await user.clear(incrementValueInput) |
| 96 | + await user.type(incrementValueInput, "1") |
| 97 | + await user.press(screen.getByText("Add If Odd")) |
| 98 | + expect(screen.getByLabelText("Count")).toHaveTextContent("4") |
| 99 | + |
| 100 | + // click on "Add If Odd" => Count should stay 4 |
| 101 | + await user.clear(incrementValueInput) |
| 102 | + await user.type(incrementValueInput, "-1") |
| 103 | + await user.press(screen.getByText("Add If Odd")) |
| 104 | + expect(screen.getByLabelText("Count")).toHaveTextContent("4") |
| 105 | +}) |
0 commit comments