|
1 | 1 | import { afterEach, describe, expect, mock, test } from "bun:test"; |
2 | 2 | import { cleanup, fireEvent, render } from "@testing-library/react"; |
| 3 | +import type { UpdateStatus } from "../../shared/rpc-types.ts"; |
3 | 4 | import { setupMockRPC } from "../test-helpers/mock-rpc.ts"; |
4 | 5 | import { UpdateNotification } from "./UpdateNotification.tsx"; |
5 | 6 |
|
6 | 7 | const VERSION_READY_PATTERN = /v2\.0\.0 is ready/; |
7 | 8 |
|
| 9 | +function defaultProps() { |
| 10 | + return { |
| 11 | + status: { status: "up-to-date", currentVersion: "1.0.0" } as UpdateStatus, |
| 12 | + dismissed: false, |
| 13 | + onDismiss: mock(), |
| 14 | + manualCheckResult: null as UpdateStatus | null, |
| 15 | + onDismissManualCheck: mock(), |
| 16 | + }; |
| 17 | +} |
| 18 | + |
8 | 19 | describe("UpdateNotification", () => { |
9 | 20 | afterEach(cleanup); |
10 | 21 |
|
11 | 22 | test("renders nothing when status is up-to-date", () => { |
12 | | - const { container } = render( |
13 | | - <UpdateNotification |
14 | | - status={{ status: "up-to-date", currentVersion: "1.0.0" }} |
15 | | - dismissed={false} |
16 | | - onDismiss={() => {}} |
17 | | - />, |
18 | | - ); |
| 23 | + const { container } = render(<UpdateNotification {...defaultProps()} />); |
19 | 24 | expect(container.innerHTML).toBe(""); |
20 | 25 | }); |
21 | 26 |
|
22 | 27 | test("renders nothing when dismissed", () => { |
23 | 28 | setupMockRPC(); |
24 | | - const { container } = render( |
25 | | - <UpdateNotification |
26 | | - status={{ status: "ready", currentVersion: "1.0.0", latestVersion: "2.0.0" }} |
27 | | - dismissed={true} |
28 | | - onDismiss={() => {}} |
29 | | - />, |
30 | | - ); |
| 29 | + const props = defaultProps(); |
| 30 | + props.status = { status: "ready", currentVersion: "1.0.0", latestVersion: "2.0.0" }; |
| 31 | + props.dismissed = true; |
| 32 | + const { container } = render(<UpdateNotification {...props} />); |
31 | 33 | expect(container.innerHTML).toBe(""); |
32 | 34 | }); |
33 | 35 |
|
34 | 36 | test("renders notification when status is ready", () => { |
35 | 37 | setupMockRPC(); |
36 | | - const { getByText } = render( |
37 | | - <UpdateNotification |
38 | | - status={{ status: "ready", currentVersion: "1.0.0", latestVersion: "2.0.0" }} |
39 | | - dismissed={false} |
40 | | - onDismiss={() => {}} |
41 | | - />, |
42 | | - ); |
| 38 | + const props = defaultProps(); |
| 39 | + props.status = { status: "ready", currentVersion: "1.0.0", latestVersion: "2.0.0" }; |
| 40 | + const { getByText } = render(<UpdateNotification {...props} />); |
43 | 41 | expect(getByText(VERSION_READY_PATTERN)).toBeDefined(); |
44 | 42 | }); |
45 | 43 |
|
46 | 44 | test("renders Restart button when ready", () => { |
47 | 45 | setupMockRPC(); |
48 | | - const { getByRole } = render( |
49 | | - <UpdateNotification |
50 | | - status={{ status: "ready", currentVersion: "1.0.0", latestVersion: "2.0.0" }} |
51 | | - dismissed={false} |
52 | | - onDismiss={() => {}} |
53 | | - />, |
54 | | - ); |
| 46 | + const props = defaultProps(); |
| 47 | + props.status = { status: "ready", currentVersion: "1.0.0", latestVersion: "2.0.0" }; |
| 48 | + const { getByRole } = render(<UpdateNotification {...props} />); |
55 | 49 | expect(getByRole("button", { name: "Restart to update" })).toBeDefined(); |
56 | 50 | }); |
57 | 51 |
|
58 | 52 | test("calls onDismiss when dismiss button clicked", () => { |
59 | 53 | setupMockRPC(); |
60 | | - const onDismiss = mock(); |
61 | | - const { getByLabelText } = render( |
62 | | - <UpdateNotification |
63 | | - status={{ status: "ready", currentVersion: "1.0.0", latestVersion: "2.0.0" }} |
64 | | - dismissed={false} |
65 | | - onDismiss={onDismiss} |
66 | | - />, |
67 | | - ); |
| 54 | + const props = defaultProps(); |
| 55 | + props.status = { status: "ready", currentVersion: "1.0.0", latestVersion: "2.0.0" }; |
| 56 | + const { getByLabelText } = render(<UpdateNotification {...props} />); |
68 | 57 | fireEvent.click(getByLabelText("Dismiss")); |
69 | | - expect(onDismiss).toHaveBeenCalled(); |
| 58 | + expect(props.onDismiss).toHaveBeenCalled(); |
70 | 59 | }); |
71 | 60 |
|
72 | 61 | test("calls applyUpdate RPC when Restart clicked", () => { |
73 | 62 | const applyUpdate = mock(() => Promise.resolve({ ok: true })); |
74 | 63 | setupMockRPC({ applyUpdate }); |
75 | | - const { getByRole } = render( |
76 | | - <UpdateNotification |
77 | | - status={{ status: "ready", currentVersion: "1.0.0", latestVersion: "2.0.0" }} |
78 | | - dismissed={false} |
79 | | - onDismiss={() => {}} |
80 | | - />, |
81 | | - ); |
| 64 | + const props = defaultProps(); |
| 65 | + props.status = { status: "ready", currentVersion: "1.0.0", latestVersion: "2.0.0" }; |
| 66 | + const { getByRole } = render(<UpdateNotification {...props} />); |
82 | 67 | fireEvent.click(getByRole("button", { name: "Restart to update" })); |
83 | 68 | expect(applyUpdate).toHaveBeenCalled(); |
84 | 69 | }); |
| 70 | + |
| 71 | + test("shows up-to-date message for manual check result", () => { |
| 72 | + const props = defaultProps(); |
| 73 | + props.manualCheckResult = { status: "up-to-date", currentVersion: "1.0.0" }; |
| 74 | + const { getByText } = render(<UpdateNotification {...props} />); |
| 75 | + expect(getByText("You're up to date")).toBeDefined(); |
| 76 | + }); |
| 77 | + |
| 78 | + test("shows error message for manual check result", () => { |
| 79 | + const props = defaultProps(); |
| 80 | + props.manualCheckResult = { |
| 81 | + status: "error", |
| 82 | + currentVersion: "1.0.0", |
| 83 | + error: "Network timeout", |
| 84 | + }; |
| 85 | + const { getByText } = render(<UpdateNotification {...props} />); |
| 86 | + expect(getByText(/Network timeout/)).toBeDefined(); |
| 87 | + }); |
| 88 | + |
| 89 | + test("calls onDismissManualCheck when dismissing manual check result", () => { |
| 90 | + const props = defaultProps(); |
| 91 | + props.manualCheckResult = { status: "up-to-date", currentVersion: "1.0.0" }; |
| 92 | + const { getByLabelText } = render(<UpdateNotification {...props} />); |
| 93 | + fireEvent.click(getByLabelText("Dismiss")); |
| 94 | + expect(props.onDismissManualCheck).toHaveBeenCalled(); |
| 95 | + }); |
| 96 | + |
| 97 | + test("manual check result with ready status falls through to normal notification", () => { |
| 98 | + setupMockRPC(); |
| 99 | + const props = defaultProps(); |
| 100 | + props.status = { status: "ready", currentVersion: "1.0.0", latestVersion: "2.0.0" }; |
| 101 | + props.manualCheckResult = { |
| 102 | + status: "ready", |
| 103 | + currentVersion: "1.0.0", |
| 104 | + latestVersion: "2.0.0", |
| 105 | + }; |
| 106 | + const { getByText, getByRole } = render(<UpdateNotification {...props} />); |
| 107 | + expect(getByText(VERSION_READY_PATTERN)).toBeDefined(); |
| 108 | + expect(getByRole("button", { name: "Restart to update" })).toBeDefined(); |
| 109 | + }); |
85 | 110 | }); |
0 commit comments