From 9734e4d859e46453fcd3ca3366c4f54a81354417 Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Wed, 19 Aug 2020 16:16:49 -0400 Subject: [PATCH] Add test spec for helpers/network.js --- helpers/__tests__/network.spec.js | 180 ++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 helpers/__tests__/network.spec.js diff --git a/helpers/__tests__/network.spec.js b/helpers/__tests__/network.spec.js new file mode 100644 index 000000000..a6cce5570 --- /dev/null +++ b/helpers/__tests__/network.spec.js @@ -0,0 +1,180 @@ +import { cancelRunningRequest, sendNetworkRequest } from "../network" + +import AxiosStrategy, { cancelRunningAxiosRequest } from "../strategies/AxiosStrategy" +import ExtensionStrategy, { + cancelRunningExtensionRequest, + hasExtensionInstalled, +} from "../strategies/ExtensionStrategy" + +jest.mock("../strategies/AxiosStrategy", () => { + return { + __esModule: true, + default: jest.fn(() => Promise.resolve()), + cancelRunningAxiosRequest: jest.fn(() => Promise.resolve()), + } +}) + +jest.mock("../strategies/ExtensionStrategy", () => { + return { + __esModule: true, + default: jest.fn(() => Promise.resolve()), + cancelRunningExtensionRequest: jest.fn(() => Promise.resolve()), + hasExtensionInstalled: jest.fn(), + } +}) + +const extensionAllowedStore = { + state: { + postwoman: { + settings: { + EXTENSIONS_ENABLED: true, + }, + }, + }, +} + +const extensionNotAllowedStore = { + state: { + postwoman: { + settings: { + EXTENSIONS_ENABLED: false, + }, + }, + }, +} + +const extensionUndefinedStore = { + state: { + postwoman: { + settings: {}, + }, + }, +} + +global.$nuxt = { + $loading: { + finish: jest.fn(() => Promise.resolve()), + }, +} + +beforeEach(() => { + jest.clearAllMocks() // Reset the call count for the mock functions +}) + +describe("cancelRunningRequest", () => { + test("cancels only extension request if extension allowed in settings and is installed", () => { + hasExtensionInstalled.mockReturnValue(true) + + cancelRunningRequest(extensionAllowedStore) + + expect(cancelRunningAxiosRequest).not.toHaveBeenCalled() + expect(cancelRunningExtensionRequest).toHaveBeenCalled() + }) + + test("cancels only extension request if extension setting is undefined and extension is installed", () => { + hasExtensionInstalled.mockReturnValue(true) + + cancelRunningRequest(extensionUndefinedStore) + + expect(cancelRunningAxiosRequest).not.toHaveBeenCalled() + expect(cancelRunningExtensionRequest).toHaveBeenCalled() + }) + + test("cancels only axios request if extension not allowed in settings and extension is installed", () => { + hasExtensionInstalled.mockReturnValue(true) + + cancelRunningRequest(extensionNotAllowedStore) + + expect(cancelRunningExtensionRequest).not.toHaveBeenCalled() + expect(cancelRunningAxiosRequest).toHaveBeenCalled() + }) + + test("cancels only axios request if extension is allowed but not installed", () => { + hasExtensionInstalled.mockReturnValue(false) + + cancelRunningRequest(extensionAllowedStore) + + expect(cancelRunningExtensionRequest).not.toHaveBeenCalled() + expect(cancelRunningAxiosRequest).toHaveBeenCalled() + }) + + test("cancels only axios request if extension is not allowed and not installed", () => { + hasExtensionInstalled.mockReturnValue(false) + + cancelRunningRequest(extensionNotAllowedStore) + + expect(cancelRunningExtensionRequest).not.toHaveBeenCalled() + expect(cancelRunningAxiosRequest).toHaveBeenCalled() + }) + + test("cancels only axios request if extension setting is undefined and not installed", () => { + hasExtensionInstalled.mockReturnValue(false) + + cancelRunningRequest(extensionUndefinedStore) + + expect(cancelRunningExtensionRequest).not.toHaveBeenCalled() + expect(cancelRunningAxiosRequest).toHaveBeenCalled() + }) +}) + +describe("sendNetworkRequest", () => { + test("runs only extension request if extension allowed in settings and is installed and clears the progress bar", async () => { + hasExtensionInstalled.mockReturnValue(true) + + await sendNetworkRequest({}, extensionAllowedStore) + + expect(AxiosStrategy).not.toHaveBeenCalled() + expect(ExtensionStrategy).toHaveBeenCalled() + expect(global.$nuxt.$loading.finish).toHaveBeenCalled() + }) + + test("runs only extension request if extension setting is undefined and extension is installed and clears the progress bar", async () => { + hasExtensionInstalled.mockReturnValue(true) + + await sendNetworkRequest({}, extensionUndefinedStore) + + expect(AxiosStrategy).not.toHaveBeenCalled() + expect(ExtensionStrategy).toHaveBeenCalled() + expect(global.$nuxt.$loading.finish).toHaveBeenCalled() + }) + + test("runs only axios request if extension not allowed in settings and extension is installed and clears the progress bar", async () => { + hasExtensionInstalled.mockReturnValue(true) + + await sendNetworkRequest({}, extensionNotAllowedStore) + + expect(ExtensionStrategy).not.toHaveBeenCalled() + expect(AxiosStrategy).toHaveBeenCalled() + expect(global.$nuxt.$loading.finish).toHaveBeenCalled() + }) + + test("runs only axios request if extension is allowed but not installed and clears the progress bar", async () => { + hasExtensionInstalled.mockReturnValue(false) + + await sendNetworkRequest({}, extensionAllowedStore) + + expect(ExtensionStrategy).not.toHaveBeenCalled() + expect(AxiosStrategy).toHaveBeenCalled() + expect(global.$nuxt.$loading.finish).toHaveBeenCalled() + }) + + test("runs only axios request if extension is not allowed and not installed and clears the progress bar", async () => { + hasExtensionInstalled.mockReturnValue(false) + + await sendNetworkRequest({}, extensionNotAllowedStore) + + expect(ExtensionStrategy).not.toHaveBeenCalled() + expect(AxiosStrategy).toHaveBeenCalled() + expect(global.$nuxt.$loading.finish).toHaveBeenCalled() + }) + + test("runs only axios request if extension setting is undefined and not installed and clears the progress bar", async () => { + hasExtensionInstalled.mockReturnValue(false) + + await sendNetworkRequest({}, extensionUndefinedStore) + + expect(ExtensionStrategy).not.toHaveBeenCalled() + expect(AxiosStrategy).toHaveBeenCalled() + expect(global.$nuxt.$loading.finish).toHaveBeenCalled() + }) +})