useCompleteOnboarding.spec.js
1 month ago
useDateRangePicker.spec.js
2 months ago
useEmail.spec.js
2 months ago
useEngagementChartData.spec.js
2 months ago
useLicenseSettings.spec.js
2 months ago
useLink.spec.js
2 months ago
useMediaDetail.spec.js
2 months ago
useMediaLibrary.spec.js
2 months ago
useMediaList.spec.js
1 month ago
usePerformanceSettings.spec.js
2 months ago
useRegisterActivePage.spec.js
2 months ago
useSettingOption.spec.js
2 months ago
useSimpleSettingsPage.spec.js
2 months ago
useTopPerforming.spec.js
2 months ago
useTopVideosPaginated.spec.js
2 months ago
useUpgradeCTA.spec.js
2 months ago
useUserDetail.spec.js
2 months ago
useSimpleSettingsPage.spec.js
145 lines
| 1 | import { renderHook, act } from "@testing-library/react-hooks"; |
| 2 | |
| 3 | const mockUseSettingOption = jest.fn(); |
| 4 | const mockUseRegisterActivePage = jest.fn(); |
| 5 | const mockToastSuccess = jest.fn(); |
| 6 | const mockToastError = jest.fn(); |
| 7 | |
| 8 | jest.mock("../useSettingOption", () => ({ |
| 9 | __esModule: true, |
| 10 | default: (...args) => mockUseSettingOption(...args), |
| 11 | })); |
| 12 | |
| 13 | jest.mock("../useRegisterActivePage", () => ({ |
| 14 | __esModule: true, |
| 15 | default: (...args) => mockUseRegisterActivePage(...args), |
| 16 | })); |
| 17 | |
| 18 | jest.mock("@bsf/force-ui", () => ({ |
| 19 | toast: { |
| 20 | success: (...args) => mockToastSuccess(...args), |
| 21 | error: (...args) => mockToastError(...args), |
| 22 | }, |
| 23 | })); |
| 24 | |
| 25 | import useSimpleSettingsPage from "../useSimpleSettingsPage"; |
| 26 | |
| 27 | const stub = (overrides = {}) => ({ |
| 28 | data: null, |
| 29 | setData: jest.fn(), |
| 30 | save: jest.fn().mockResolvedValue(undefined), |
| 31 | reset: jest.fn(), |
| 32 | isDirty: false, |
| 33 | isSaving: false, |
| 34 | isLoading: false, |
| 35 | ...overrides, |
| 36 | }); |
| 37 | |
| 38 | beforeEach(() => { |
| 39 | mockUseSettingOption.mockReset(); |
| 40 | mockUseRegisterActivePage.mockReset(); |
| 41 | mockToastSuccess.mockReset(); |
| 42 | mockToastError.mockReset(); |
| 43 | }); |
| 44 | |
| 45 | describe("useSimpleSettingsPage", () => { |
| 46 | describe("update", () => { |
| 47 | it("merges a patch on top of the previous value", () => { |
| 48 | const setData = jest.fn(); |
| 49 | mockUseSettingOption.mockReturnValue(stub({ data: { a: 1, b: 2 }, setData })); |
| 50 | |
| 51 | const { result } = renderHook(() => |
| 52 | useSimpleSettingsPage("foo", { a: 0, b: 0 }, jest.fn()) |
| 53 | ); |
| 54 | |
| 55 | act(() => { |
| 56 | result.current.update({ b: 9 }); |
| 57 | }); |
| 58 | |
| 59 | // setData receives an updater; invoke it to verify the merge. |
| 60 | const updater = setData.mock.calls[0][0]; |
| 61 | expect(updater({ a: 1, b: 2 })).toEqual({ a: 1, b: 9 }); |
| 62 | }); |
| 63 | |
| 64 | it("falls back to defaults when prev is null/undefined", () => { |
| 65 | const setData = jest.fn(); |
| 66 | mockUseSettingOption.mockReturnValue(stub({ data: null, setData })); |
| 67 | |
| 68 | const { result } = renderHook(() => |
| 69 | useSimpleSettingsPage("foo", { a: 0, b: 0 }, jest.fn()) |
| 70 | ); |
| 71 | |
| 72 | act(() => { |
| 73 | result.current.update({ b: 9 }); |
| 74 | }); |
| 75 | |
| 76 | const updater = setData.mock.calls[0][0]; |
| 77 | expect(updater(null)).toEqual({ a: 0, b: 9 }); |
| 78 | expect(updater(undefined)).toEqual({ a: 0, b: 9 }); |
| 79 | }); |
| 80 | }); |
| 81 | |
| 82 | describe("handleSave", () => { |
| 83 | it("calls save() and shows a success toast", async () => { |
| 84 | const save = jest.fn().mockResolvedValue(undefined); |
| 85 | mockUseSettingOption.mockReturnValue(stub({ save })); |
| 86 | |
| 87 | const { result } = renderHook(() => |
| 88 | useSimpleSettingsPage("foo", {}, jest.fn()) |
| 89 | ); |
| 90 | |
| 91 | await act(async () => { |
| 92 | await result.current.handleSave(); |
| 93 | }); |
| 94 | |
| 95 | expect(save).toHaveBeenCalledTimes(1); |
| 96 | expect(mockToastSuccess).toHaveBeenCalledWith( |
| 97 | "Settings saved.", |
| 98 | expect.objectContaining({ autoDismiss: 3000 }) |
| 99 | ); |
| 100 | expect(mockToastError).not.toHaveBeenCalled(); |
| 101 | }); |
| 102 | |
| 103 | it("swallows save() failures and shows an error toast", async () => { |
| 104 | const save = jest.fn().mockRejectedValue(new Error("server")); |
| 105 | mockUseSettingOption.mockReturnValue(stub({ save })); |
| 106 | |
| 107 | const { result } = renderHook(() => |
| 108 | useSimpleSettingsPage("foo", {}, jest.fn()) |
| 109 | ); |
| 110 | |
| 111 | // handleSave should NOT throw — it converts failures to a toast. |
| 112 | await act(async () => { |
| 113 | await expect(result.current.handleSave()).resolves.toBeUndefined(); |
| 114 | }); |
| 115 | |
| 116 | expect(mockToastError).toHaveBeenCalledWith( |
| 117 | "Could not save settings.", |
| 118 | expect.objectContaining({ autoDismiss: 5000 }) |
| 119 | ); |
| 120 | expect(mockToastSuccess).not.toHaveBeenCalled(); |
| 121 | }); |
| 122 | }); |
| 123 | |
| 124 | describe("nav-guard registration", () => { |
| 125 | it("hands isDirty + handleSave + reset to useRegisterActivePage", () => { |
| 126 | const reset = jest.fn(); |
| 127 | mockUseSettingOption.mockReturnValue(stub({ isDirty: true, reset })); |
| 128 | const registerActivePage = jest.fn(); |
| 129 | |
| 130 | renderHook(() => |
| 131 | useSimpleSettingsPage("foo", {}, registerActivePage) |
| 132 | ); |
| 133 | |
| 134 | expect(mockUseRegisterActivePage).toHaveBeenCalledWith( |
| 135 | registerActivePage, |
| 136 | expect.objectContaining({ |
| 137 | isDirty: true, |
| 138 | save: expect.any(Function), |
| 139 | discard: reset, |
| 140 | }) |
| 141 | ); |
| 142 | }); |
| 143 | }); |
| 144 | }); |
| 145 |