From 208cfa81096ca6993ac34f393c8e7648350f8fad Mon Sep 17 00:00:00 2001 From: Devlin Junker Date: Tue, 22 Nov 2022 07:58:28 -1000 Subject: add unit tests for admin settings Signed-off-by: Devlin Junker --- .../unit/components/AdminSettings.spec.ts | 23 +++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'tests') diff --git a/tests/javascript/unit/components/AdminSettings.spec.ts b/tests/javascript/unit/components/AdminSettings.spec.ts index 06b1c5784..4562df933 100644 --- a/tests/javascript/unit/components/AdminSettings.spec.ts +++ b/tests/javascript/unit/components/AdminSettings.spec.ts @@ -4,8 +4,10 @@ import { store, localVue } from "../setupStore"; import { showError, showSuccess } from "@nextcloud/dialogs"; import { loadState } from "@nextcloud/initial-state"; +import 'regenerator-runtime/runtime' // NOTE: Required for testing password-confirmation? import AdminSettings from "Components/AdminSettings.vue"; + jest.mock("@nextcloud/axios"); jest.mock("@nextcloud/initial-state"); jest.mock("@nextcloud/router"); @@ -17,40 +19,47 @@ describe("AdminSettings.vue", () => { let wrapper: Wrapper; beforeAll(() => { - wrapper = shallowMount(AdminSettings, { localVue, store }); + jest.useFakeTimers(); + (loadState as any).mockReturnValue(''); + wrapper = shallowMount(AdminSettings, { localVue, store}); }); it("should initialize and fetch settings from state", () => { expect(loadState).toBeCalledTimes(7); }); - it("should send post with updated settings", () => { + it("should send post with updated settings", async () => { jest.spyOn(axios, "post").mockResolvedValue({ data: {} }); + (wrapper.vm as any).handleResponse = jest.fn() - wrapper.vm.$options?.methods?.update.call(wrapper.vm); + await wrapper.vm.$options?.methods?.update.call(wrapper.vm, 'key', 'val'); - expect(axios.post).toBeCalled; + expect(axios.post).toBeCalledTimes(1); }); it("should handle bad response", () => { + (showError as any).mockClear(); console.error = jest.fn(); wrapper.vm.$options?.methods?.handleResponse.call(wrapper.vm, { error: true, errorMessage: "FAIL", }); - expect(showError).toBeCalled; + expect(showError).toBeCalledTimes(1); }); it("should handle success response", () => { wrapper.vm.$options?.methods?.handleResponse.call(wrapper.vm, { - success: "ok", + status: "ok", }); + (global as any).t = jest.fn(); + jest.runAllTimers(); - expect(showSuccess).toBeCalled; + expect(showSuccess).toBeCalledTimes(1); }); afterAll(() => { jest.clearAllMocks(); + jest.useRealTimers(); }); }); -- cgit v1.2.3