|
1 | 1 | /* eslint import/first: 0 */ |
2 | 2 | import { act, renderHook } from "@testing-library/react"; |
3 | | -import { ScreenContextProvider } from "@ensembleui/react-framework"; |
| 3 | +import { |
| 4 | + ScreenContextProvider, |
| 5 | + ApplicationContextProvider, |
| 6 | +} from "@ensembleui/react-framework"; |
4 | 7 | import { useEnsembleAction } from "../useEnsembleAction"; |
| 8 | +import { useNavigateScreen } from "../useNavigateScreen"; |
5 | 9 |
|
6 | 10 | jest.mock("react-markdown", jest.fn()); |
7 | | -jest.mock("react-router-dom"); |
| 11 | + |
| 12 | +const mockNavigate = jest.fn(); |
| 13 | +const mockLocation = jest.fn(); |
| 14 | + |
| 15 | +jest.mock("react-router-dom", () => ({ |
| 16 | + useNavigate: () => mockNavigate, |
| 17 | + useLocation: () => mockLocation, |
| 18 | +})); |
8 | 19 |
|
9 | 20 | const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => ( |
10 | 21 | <ScreenContextProvider |
@@ -61,3 +72,143 @@ describe("Test cases for useEnsembleAction Hook", () => { |
61 | 72 | expect(execResult).toBe(2); |
62 | 73 | }); |
63 | 74 | }); |
| 75 | + |
| 76 | +const navigateScreenWrapper: React.FC<React.PropsWithChildren> = ({ |
| 77 | + children, |
| 78 | +}) => ( |
| 79 | + <ApplicationContextProvider |
| 80 | + app={{ |
| 81 | + screens: [ |
| 82 | + { name: "Home", id: "home", body: { name: "Button", properties: {} } }, |
| 83 | + { |
| 84 | + name: "Details", |
| 85 | + id: "details", |
| 86 | + body: { name: "Button", properties: {} }, |
| 87 | + }, |
| 88 | + ], |
| 89 | + customWidgets: [], |
| 90 | + home: { |
| 91 | + name: "Home", |
| 92 | + id: "home", |
| 93 | + body: { name: "Button", properties: {} }, |
| 94 | + }, |
| 95 | + themes: {}, |
| 96 | + id: "testApp", |
| 97 | + scripts: [], |
| 98 | + }} |
| 99 | + > |
| 100 | + <ScreenContextProvider |
| 101 | + context={{ |
| 102 | + widgets: { |
| 103 | + myWidget: { |
| 104 | + values: { |
| 105 | + value: "home", |
| 106 | + }, |
| 107 | + invokable: { |
| 108 | + id: "myWidget", |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + data: {}, |
| 113 | + storage: {}, |
| 114 | + }} |
| 115 | + screen={{ |
| 116 | + id: "home", |
| 117 | + name: "Home", |
| 118 | + body: { name: "Button", properties: {} }, |
| 119 | + }} |
| 120 | + > |
| 121 | + {children} |
| 122 | + </ScreenContextProvider> |
| 123 | + </ApplicationContextProvider> |
| 124 | +); |
| 125 | + |
| 126 | +describe("Test cases for useNavigateScreen Hook", () => { |
| 127 | + beforeEach(() => { |
| 128 | + mockNavigate.mockClear(); |
| 129 | + }); |
| 130 | + |
| 131 | + it("should not navigate when no action is provided", () => { |
| 132 | + const { result } = renderHook(() => useNavigateScreen(undefined), { |
| 133 | + wrapper: navigateScreenWrapper, |
| 134 | + }); |
| 135 | + |
| 136 | + act(() => { |
| 137 | + result.current?.callback(); |
| 138 | + }); |
| 139 | + |
| 140 | + expect(mockNavigate).not.toHaveBeenCalled(); |
| 141 | + }); |
| 142 | + |
| 143 | + it("should navigate when action is a string", () => { |
| 144 | + const { result } = renderHook( |
| 145 | + // eslint-disable-next-line no-template-curly-in-string |
| 146 | + () => useNavigateScreen("home"), |
| 147 | + { |
| 148 | + wrapper: navigateScreenWrapper, |
| 149 | + }, |
| 150 | + ); |
| 151 | + |
| 152 | + act(() => { |
| 153 | + result.current?.callback(); |
| 154 | + }); |
| 155 | + |
| 156 | + expect(mockNavigate).toHaveBeenCalledWith("/home", { state: undefined }); |
| 157 | + }); |
| 158 | + |
| 159 | + it("should navigate when action name is a variable", () => { |
| 160 | + const { result } = renderHook( |
| 161 | + // eslint-disable-next-line no-template-curly-in-string |
| 162 | + () => useNavigateScreen("${myWidget.value}"), |
| 163 | + { |
| 164 | + wrapper: navigateScreenWrapper, |
| 165 | + }, |
| 166 | + ); |
| 167 | + |
| 168 | + act(() => { |
| 169 | + result.current?.callback(); |
| 170 | + }); |
| 171 | + |
| 172 | + expect(mockNavigate).toHaveBeenCalledWith("/home", { state: undefined }); |
| 173 | + }); |
| 174 | + |
| 175 | + it("should navigate with inputs when action is an object", () => { |
| 176 | + const action = { |
| 177 | + name: "Details", |
| 178 | + inputs: { |
| 179 | + id: 123, |
| 180 | + category: "test", |
| 181 | + }, |
| 182 | + }; |
| 183 | + |
| 184 | + const { result } = renderHook(() => useNavigateScreen(action), { |
| 185 | + wrapper: navigateScreenWrapper, |
| 186 | + }); |
| 187 | + |
| 188 | + act(() => { |
| 189 | + result.current?.callback(); |
| 190 | + }); |
| 191 | + |
| 192 | + expect(mockNavigate).toHaveBeenCalledWith("/details", { |
| 193 | + state: { |
| 194 | + id: 123, |
| 195 | + category: "test", |
| 196 | + }, |
| 197 | + }); |
| 198 | + }); |
| 199 | + |
| 200 | + it("should not navigate when screen is not found", () => { |
| 201 | + const { result } = renderHook( |
| 202 | + () => useNavigateScreen("NonExistentScreen"), |
| 203 | + { |
| 204 | + wrapper: navigateScreenWrapper, |
| 205 | + }, |
| 206 | + ); |
| 207 | + |
| 208 | + act(() => { |
| 209 | + result.current?.callback(); |
| 210 | + }); |
| 211 | + |
| 212 | + expect(mockNavigate).not.toHaveBeenCalled(); |
| 213 | + }); |
| 214 | +}); |
0 commit comments