|
1 | | -import { render } from "@testing-library/react"; |
2 | | -import React from "react"; |
3 | | -import { act } from "react-dom/test-utils"; |
4 | | -import { useSortedAlphabetically } from "./use-sorted-alphabetically"; |
5 | | - |
6 | | -describe("useSortedAlphabetically", () => { |
7 | | - const testSelector = (s: string) => s; |
8 | | - |
9 | | - test("when initialState is not sorted, then returns sorted array", () => { |
10 | | - // Arrange |
11 | | - const unsortedList = ["C", "A", "D", "B"]; |
12 | | - const sortedList = ["A", "B", "C", "D"]; |
13 | | - |
14 | | - const TestApp = () => { |
15 | | - const [values, setValues] = useSortedAlphabetically( |
16 | | - unsortedList, |
17 | | - testSelector |
18 | | - ); |
19 | | - |
20 | | - return ( |
21 | | - <React.Fragment> |
22 | | - {values.map((value: string) => ( |
23 | | - <p key={value} title="item"> |
24 | | - {value} |
25 | | - </p> |
26 | | - ))} |
27 | | - </React.Fragment> |
28 | | - ); |
29 | | - }; |
30 | | - |
31 | | - // Act |
32 | | - const { getAllByTitle } = render(<TestApp />); |
33 | | - const items = getAllByTitle("item").map( |
34 | | - (el: HTMLElement) => el.innerHTML |
35 | | - ); |
36 | | - |
37 | | - // Assert |
38 | | - expect(items).toStrictEqual(sortedList); |
39 | | - }); |
40 | | - |
41 | | - test("When setValues is called with unsorted array, then values is set to a sorted array", () => { |
42 | | - // Arrange |
43 | | - const unsortedList = ["C", "A", "D", "B"]; |
44 | | - const sortedList = ["A", "B", "C", "D"]; |
45 | | - |
46 | | - const TestApp = () => { |
47 | | - const [values, setValues] = useSortedAlphabetically( |
48 | | - [], |
49 | | - testSelector |
50 | | - ); |
51 | | - |
52 | | - return ( |
53 | | - <React.Fragment> |
54 | | - {values.map((value: string) => ( |
55 | | - <p key={value} title="item"> |
56 | | - {value} |
57 | | - </p> |
58 | | - ))} |
59 | | - <button |
60 | | - onClick={() => setValues(unsortedList)} |
61 | | - title="testButton" |
62 | | - type="button" |
63 | | - /> |
64 | | - </React.Fragment> |
65 | | - ); |
66 | | - }; |
67 | | - |
68 | | - // Act |
69 | | - const { getAllByTitle, getByTitle } = render(<TestApp />); |
70 | | - const button = getByTitle("testButton"); |
71 | | - act(() => button.click()); |
72 | | - const items = getAllByTitle("item").map( |
73 | | - (el: HTMLElement) => el.innerHTML |
74 | | - ); |
75 | | - |
76 | | - // Assert |
77 | | - expect(items).toStrictEqual(sortedList); |
78 | | - }); |
79 | | -}); |
| 1 | +import { render } from "@testing-library/react"; |
| 2 | +import React from "react"; |
| 3 | +import { act } from "react-dom/test-utils"; |
| 4 | +import { useSortedAlphabetically } from "./use-sorted-alphabetically"; |
| 5 | + |
| 6 | +describe("useSortedAlphabetically", () => { |
| 7 | + const testSelector = (s: string) => s; |
| 8 | + |
| 9 | + test("when initialState is not sorted, then returns sorted array", () => { |
| 10 | + // Arrange |
| 11 | + const unsortedList = ["C", "A", "D", "B"]; |
| 12 | + const sortedList = ["A", "B", "C", "D"]; |
| 13 | + |
| 14 | + const TestApp = () => { |
| 15 | + const [values, setValues] = useSortedAlphabetically( |
| 16 | + unsortedList, |
| 17 | + testSelector |
| 18 | + ); |
| 19 | + |
| 20 | + return ( |
| 21 | + <React.Fragment> |
| 22 | + {values.map((value: string) => ( |
| 23 | + <p key={value} title="item"> |
| 24 | + {value} |
| 25 | + </p> |
| 26 | + ))} |
| 27 | + </React.Fragment> |
| 28 | + ); |
| 29 | + }; |
| 30 | + |
| 31 | + // Act |
| 32 | + const { getAllByTitle } = render(<TestApp />); |
| 33 | + const items = getAllByTitle("item").map( |
| 34 | + (el: HTMLElement) => el.innerHTML |
| 35 | + ); |
| 36 | + |
| 37 | + // Assert |
| 38 | + expect(items).toStrictEqual(sortedList); |
| 39 | + }); |
| 40 | + |
| 41 | + test("when setValues is called with unsorted array, then values is set to a sorted array", () => { |
| 42 | + // Arrange |
| 43 | + const unsortedList = ["C", "A", "D", "B"]; |
| 44 | + const sortedList = ["A", "B", "C", "D"]; |
| 45 | + |
| 46 | + const TestApp = () => { |
| 47 | + const [values, setValues] = useSortedAlphabetically( |
| 48 | + [], |
| 49 | + testSelector |
| 50 | + ); |
| 51 | + |
| 52 | + return ( |
| 53 | + <React.Fragment> |
| 54 | + {values.map((value: string) => ( |
| 55 | + <p key={value} title="item"> |
| 56 | + {value} |
| 57 | + </p> |
| 58 | + ))} |
| 59 | + <button |
| 60 | + onClick={() => setValues(unsortedList)} |
| 61 | + title="testButton" |
| 62 | + type="button" |
| 63 | + /> |
| 64 | + </React.Fragment> |
| 65 | + ); |
| 66 | + }; |
| 67 | + |
| 68 | + // Act |
| 69 | + const { getAllByTitle, getByTitle } = render(<TestApp />); |
| 70 | + const button = getByTitle("testButton"); |
| 71 | + act(() => button.click()); |
| 72 | + const items = getAllByTitle("item").map( |
| 73 | + (el: HTMLElement) => el.innerHTML |
| 74 | + ); |
| 75 | + |
| 76 | + // Assert |
| 77 | + expect(items).toStrictEqual(sortedList); |
| 78 | + }); |
| 79 | +}); |
0 commit comments