|
| 1 | +import { configure } from "@testing-library/dom"; |
| 2 | +import { waitFor, render, screen } from "@testing-library/react"; |
| 3 | +import userEvent from "@testing-library/user-event"; |
| 4 | + |
| 5 | +import App from "./App"; |
| 6 | + |
| 7 | +configure({ defaultHidden: true }); |
| 8 | + |
| 9 | +const components = [ |
| 10 | + { |
| 11 | + name: "Simple", |
| 12 | + component: <App iterations={2} />, |
| 13 | + iterations: 2, |
| 14 | + depth: 1, |
| 15 | + }, |
| 16 | + { |
| 17 | + name: "Medium", |
| 18 | + component: <App iterations={5} />, |
| 19 | + iterations: 5, |
| 20 | + depth: 3, |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "Complex", |
| 24 | + component: <App iterations={10} />, |
| 25 | + iterations: 10, |
| 26 | + depth: 5, |
| 27 | + }, |
| 28 | +]; |
| 29 | + |
| 30 | +describe.each(components)( |
| 31 | + "Perf testing $name component with iterations complexity if '$iterations' and depth complexity of '$depth'", |
| 32 | + ({ component, iterations }) => { |
| 33 | + beforeEach(() => { |
| 34 | + render(component); |
| 35 | + }); |
| 36 | + |
| 37 | + it("getByRole selector performance", async () => { |
| 38 | + for (const index of Array.from({ length: iterations }).keys()) { |
| 39 | + const button = screen.getByRole("button", { |
| 40 | + name: `Button label ${index}`, |
| 41 | + }); |
| 42 | + await userEvent.click(button); |
| 43 | + await waitFor(() => |
| 44 | + expect( |
| 45 | + screen.getByRole("paragraph", { |
| 46 | + name: `Service label ${index}`, |
| 47 | + }), |
| 48 | + ), |
| 49 | + ); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + it("getByLabelText selector performance", async () => { |
| 54 | + for (const index of Array.from({ length: iterations }).keys()) { |
| 55 | + const button = screen.getByLabelText(`Button label ${index}`); |
| 56 | + await userEvent.click(button); |
| 57 | + await waitFor(() => |
| 58 | + expect(screen.getByLabelText(`Service label ${index}`)), |
| 59 | + ); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + it("getByPlaceholderText selector performance", async () => { |
| 64 | + for (const index of Array.from({ length: iterations }).keys()) { |
| 65 | + const input = screen.getByPlaceholderText( |
| 66 | + `Button input placeholder ${index}`, |
| 67 | + ); |
| 68 | + await userEvent.click(input); |
| 69 | + await waitFor(() => |
| 70 | + expect( |
| 71 | + screen.getByPlaceholderText( |
| 72 | + `Service textarea placeholder ${index}`, |
| 73 | + ), |
| 74 | + ), |
| 75 | + ); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + it("getByText selector performance", async () => { |
| 80 | + for (const index of Array.from({ length: iterations }).keys()) { |
| 81 | + const button = screen.getByText(`Button text ${index}`); |
| 82 | + await userEvent.click(button); |
| 83 | + await waitFor(() => |
| 84 | + expect(screen.getByText(`Service text ${index}`)).toBeInTheDocument(), |
| 85 | + ); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + it("getByDisplayValue selector performance", async () => { |
| 90 | + for (const index of Array.from({ length: iterations }).keys()) { |
| 91 | + const input = screen.getByDisplayValue(`Button input value ${index}`); |
| 92 | + await userEvent.click(input); |
| 93 | + await waitFor(() => |
| 94 | + expect(screen.getByDisplayValue(`Service textarea value ${index}`)), |
| 95 | + ); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + it("getByAltText selector performance", async () => { |
| 100 | + for (const index of Array.from({ length: iterations }).keys()) { |
| 101 | + const img = screen.getByAltText(`Button image alt ${index}`); |
| 102 | + await userEvent.click(img); |
| 103 | + await waitFor(() => |
| 104 | + expect( |
| 105 | + screen.getByAltText(`Service image alt ${index}`), |
| 106 | + ).toBeInTheDocument(), |
| 107 | + ); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + it("getByTitle selector performance", async () => { |
| 112 | + for (const index of Array.from({ length: iterations }).keys()) { |
| 113 | + const button = screen.getByTitle(`Button title ${index}`); |
| 114 | + await userEvent.click(button); |
| 115 | + await waitFor(() => |
| 116 | + expect( |
| 117 | + screen.getByTitle(`Service title ${index}`), |
| 118 | + ).toBeInTheDocument(), |
| 119 | + ); |
| 120 | + } |
| 121 | + }); |
| 122 | + |
| 123 | + it("getByTestId selector performance", async () => { |
| 124 | + for (const index of Array.from({ length: iterations }).keys()) { |
| 125 | + const button = screen.getByTestId(`button-test-id-${index}`); |
| 126 | + await userEvent.click(button); |
| 127 | + await waitFor(() => |
| 128 | + expect( |
| 129 | + screen.getByTestId(`button-test-id-${index}`), |
| 130 | + ).toBeInTheDocument(), |
| 131 | + ); |
| 132 | + } |
| 133 | + }); |
| 134 | + }, |
| 135 | +); |
0 commit comments