@@ -4,21 +4,43 @@ import { expect, it, vi } from "vite-plus/test"; |
4 | 4 | const harness = vi.hoisted(() => ({ |
5 | 5 | pair: vi.fn(), |
6 | 6 | goBack: vi.fn(), |
7 | | - currentRoute: "SettingsEnvironmentNew", |
| 7 | + dispatch: vi.fn(), |
| 8 | + columns: false, |
| 9 | + // The root stack the form sits on when pairing succeeds. |
| 10 | + rootState: { |
| 11 | + key: "root", |
| 12 | + index: 2, |
| 13 | + routes: [ |
| 14 | + { key: "home", name: "Home" }, |
| 15 | + { key: "settings", name: "Settings" }, |
| 16 | + { key: "new", name: "SettingsEnvironmentNew" }, |
| 17 | + ], |
| 18 | + }, |
| 19 | + // Navigation that is not ready yet holds callbacks here until the test flushes them. |
| 20 | + deferred: null as Array<() => void> | null, |
8 | 21 | })); |
9 | 22 |
|
10 | 23 | vi.mock("@react-navigation/native", () => ({ StackActions: { replace: vi.fn() } })); |
11 | 24 | vi.mock("../../state/use-remote-environment-registry", () => ({ |
12 | 25 | pairEnvironment: harness.pair, |
13 | 26 | })); |
| 27 | +vi.mock("../workspace/workspace-selection-store", () => ({ |
| 28 | + workspaceHasColumns: () => harness.columns, |
| 29 | +})); |
14 | 30 | vi.mock("../../lib/rootNavigation", () => ({ |
15 | 31 | rootNavigationRef: { |
16 | | - getCurrentRoute: () => ({ name: harness.currentRoute }), |
| 32 | + getCurrentRoute: () => harness.rootState.routes[harness.rootState.index], |
| 33 | + getRootState: () => harness.rootState, |
| 34 | + getState: () => harness.rootState, |
| 35 | + getParent: () => undefined, |
17 | 36 | canGoBack: () => true, |
18 | 37 | goBack: harness.goBack, |
19 | | - dispatch: vi.fn(), |
| 38 | + dispatch: harness.dispatch, |
| 39 | + }, |
| 40 | + whenRootNavigationReady: (navigate: () => void) => { |
| 41 | + if (harness.deferred) harness.deferred.push(navigate); |
| 42 | + else navigate(); |
20 | 43 | }, |
21 | | - whenRootNavigationReady: (navigate: () => void) => navigate(), |
22 | 44 | })); |
23 | 45 |
|
24 | 46 | import { appAtomRegistry } from "../../state/atom-registry"; |
@@ -122,3 +144,50 @@ it("keeps Connect busy while the auto-connect runs, even after the user edits th |
122 | 144 | expect(autoConnectReport(failed, PAIRING_URL, true).error).toBeNull(); |
123 | 145 | expect(autoConnectReport(connecting, "http://other/pair#token=x", false).connecting).toBe(false); |
124 | 146 | }); |
| 147 | + |
| 148 | +async function pairSuccessfully(link: PartialState<NavigationState>) { |
| 149 | + const succeeded = Promise.withResolvers<{ ok: boolean; error: string | null }>(); |
| 150 | + harness.pair.mockReturnValueOnce(succeeded.promise); |
| 151 | + startDevAutoConnect(link); |
| 152 | + succeeded.resolve({ ok: true, error: null }); |
| 153 | + await succeeded.promise; |
| 154 | + await Promise.resolve(); |
| 155 | +} |
| 156 | + |
| 157 | +it("on columns, a successful settings link shows Environments instead of an empty detail", async () => { |
| 158 | + vi.stubGlobal("__DEV__", true); |
| 159 | + harness.goBack.mockClear(); |
| 160 | + harness.dispatch.mockClear(); |
| 161 | + harness.columns = true; |
| 162 | + await pairSuccessfully(settingsLink({ pairingUrl: PAIRING_URL, autoConnect: "1" })); |
| 163 | + harness.columns = false; |
| 164 | + |
| 165 | + expect(harness.goBack).not.toHaveBeenCalled(); |
| 166 | + expect(harness.dispatch).toHaveBeenCalledExactlyOnceWith({ |
| 167 | + type: "RESET", |
| 168 | + target: "root", |
| 169 | + payload: expect.objectContaining({ |
| 170 | + index: 2, |
| 171 | + routes: [ |
| 172 | + { key: "home", name: "Home" }, |
| 173 | + { key: "settings", name: "Settings" }, |
| 174 | + { name: "SettingsEnvironments", params: undefined }, |
| 175 | + ], |
| 176 | + }), |
| 177 | + }); |
| 178 | +}); |
| 179 | + |
| 180 | +it("a success that waited for navigation leaves a newer link's form open", async () => { |
| 181 | + vi.stubGlobal("__DEV__", true); |
| 182 | + harness.goBack.mockClear(); |
| 183 | + harness.dispatch.mockClear(); |
| 184 | + harness.deferred = []; |
| 185 | + await pairSuccessfully(settingsLink({ pairingUrl: PAIRING_URL, autoConnect: "1" })); |
| 186 | + startDevAutoConnect(settingsLink({ pairingUrl: "http://192.168.1.11:3773/pair#token=def" })); |
| 187 | + |
| 188 | + const deferred = harness.deferred; |
| 189 | + harness.deferred = null; |
| 190 | + for (const navigate of deferred) navigate(); |
| 191 | + expect(harness.goBack).not.toHaveBeenCalled(); |
| 192 | + expect(harness.dispatch).not.toHaveBeenCalled(); |
| 193 | +}); |