Revamp of the Settings State System along with TypeScript support (#1560)

* Add vue-rx, rxjs and lodash as dependencies

* Added vue-rx plugin integration to nuxt config

* Initial settings store implementation

* Add babel plugin for private class properties to for Jest

* Add DispatchingStore test spec

* Initial settings code

* Reactive Streams for fb current user and id token

* Fix typo

* Migrate index and graphql pages to the new store

* Migrate network strategy to the new store

* Fixed Section.vue errors

* Fix getSettingSubject issue

* Migrate fb settings reference in components to the new state system

* Add typings for lodash as dev dependency

* Load setting

* Load initial sync setting values

* Update proxy url

* Add typescript support

* Rewrite Settings store to TypeScript

* Port Settings page to TypeScript as reference

* Move all store migrations to a separate file

* Delete test file for fb.js

* Add ts-jest as dev dependency

* Remove firebase-mock as dependency

* Remove FRAME_COLORS_ENABLED settings value
This commit is contained in:
Andrew Bastin
2021-03-23 11:18:14 -04:00
committed by GitHub
parent 64f64b9e31
commit 5fce1118f6
47 changed files with 32426 additions and 9143 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,83 @@
import { cancelRunningRequest, sendNetworkRequest } from "../network"
import AxiosStrategy, { cancelRunningAxiosRequest } from "../strategies/AxiosStrategy"
import ExtensionStrategy, {
cancelRunningExtensionRequest,
hasExtensionInstalled,
} from "../strategies/ExtensionStrategy"
jest.mock("../strategies/AxiosStrategy", () => ({
__esModule: true,
default: jest.fn(() => Promise.resolve()),
cancelRunningAxiosRequest: jest.fn(() => Promise.resolve()),
}))
jest.mock("../strategies/ExtensionStrategy", () => ({
__esModule: true,
default: jest.fn(() => Promise.resolve()),
cancelRunningExtensionRequest: jest.fn(() => Promise.resolve()),
hasExtensionInstalled: jest.fn(),
}))
jest.mock("~/newstore/settings", () => {
return {
settingsStore: {
value: {
EXTENSIONS_ENABLED: false
}
}
}
})
global.$nuxt = {
$loading: {
finish: jest.fn(() => Promise.resolve()),
},
}
beforeEach(() => {
jest.clearAllMocks() // Reset the call count for the mock functions
})
describe("cancelRunningRequest", () => {
test("cancels only axios request if extension not allowed in settings and extension is installed", () => {
hasExtensionInstalled.mockReturnValue(true)
cancelRunningRequest()
expect(cancelRunningExtensionRequest).not.toHaveBeenCalled()
expect(cancelRunningAxiosRequest).toHaveBeenCalled()
})
test("cancels only axios request if extension is not allowed and not installed", () => {
hasExtensionInstalled.mockReturnValue(false)
cancelRunningRequest()
expect(cancelRunningExtensionRequest).not.toHaveBeenCalled()
expect(cancelRunningAxiosRequest).toHaveBeenCalled()
})
})
describe("sendNetworkRequest", () => {
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({})
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({})
expect(ExtensionStrategy).not.toHaveBeenCalled()
expect(AxiosStrategy).toHaveBeenCalled()
expect(global.$nuxt.$loading.finish).toHaveBeenCalled()
})
})

View File

@@ -0,0 +1,82 @@
import { cancelRunningRequest, sendNetworkRequest } from "../network"
import AxiosStrategy, { cancelRunningAxiosRequest } from "../strategies/AxiosStrategy"
import ExtensionStrategy, {
cancelRunningExtensionRequest,
hasExtensionInstalled,
} from "../strategies/ExtensionStrategy"
jest.mock("../strategies/AxiosStrategy", () => ({
__esModule: true,
default: jest.fn(() => Promise.resolve()),
cancelRunningAxiosRequest: jest.fn(() => Promise.resolve()),
}))
jest.mock("../strategies/ExtensionStrategy", () => ({
__esModule: true,
default: jest.fn(() => Promise.resolve()),
cancelRunningExtensionRequest: jest.fn(() => Promise.resolve()),
hasExtensionInstalled: jest.fn(),
}))
jest.mock("~/newstore/settings", () => {
return {
settingsStore: {
value: {
EXTENSIONS_ENABLED: true
}
}
}
})
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()
expect(cancelRunningAxiosRequest).not.toHaveBeenCalled()
expect(cancelRunningExtensionRequest).toHaveBeenCalled()
})
test("cancels only axios request if extension is allowed but not installed", () => {
hasExtensionInstalled.mockReturnValue(false)
cancelRunningRequest()
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({})
expect(AxiosStrategy).not.toHaveBeenCalled()
expect(ExtensionStrategy).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({})
expect(ExtensionStrategy).not.toHaveBeenCalled()
expect(AxiosStrategy).toHaveBeenCalled()
expect(global.$nuxt.$loading.finish).toHaveBeenCalled()
})
})

View File

@@ -1,176 +0,0 @@
import { cancelRunningRequest, sendNetworkRequest } from "../network"
import AxiosStrategy, { cancelRunningAxiosRequest } from "../strategies/AxiosStrategy"
import ExtensionStrategy, {
cancelRunningExtensionRequest,
hasExtensionInstalled,
} from "../strategies/ExtensionStrategy"
jest.mock("../strategies/AxiosStrategy", () => ({
__esModule: true,
default: jest.fn(() => Promise.resolve()),
cancelRunningAxiosRequest: jest.fn(() => Promise.resolve()),
}))
jest.mock("../strategies/ExtensionStrategy", () => ({
__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()
})
})

View File

@@ -1,6 +1,8 @@
import firebase from "firebase/app"
import "firebase/firestore"
import "firebase/auth"
import { ReplaySubject } from "rxjs"
import { getSettingSubject, applySetting } from "~/newstore/settings"
// Initialize Firebase, copied from cloud console
const firebaseConfig = {
@@ -38,6 +40,41 @@ export class FirebaseInstance {
this.currentGraphqlCollections = []
this.currentEnvironments = []
this.currentUser$ = new ReplaySubject(1)
this.idToken$ = new ReplaySubject(1)
let loadedSettings = false
getSettingSubject("syncCollections").subscribe((status) => {
if (this.currentUser && loadedSettings) {
this.writeSettings("syncCollections", status)
}
})
getSettingSubject("syncHistory").subscribe((status) => {
if (this.currentUser && loadedSettings) {
this.writeSettings("syncHistory", status)
}
})
getSettingSubject("syncEnvironments").subscribe((status) => {
if (this.currentUser && loadedSettings) {
this.writeSettings("syncEnvironments", status)
}
})
this.app.auth().onIdTokenChanged((user) => {
if (user) {
user.getIdToken().then((token) => {
this.idToken = token
this.idToken$.next(token)
})
} else {
this.idToken = null
this.idToken$.next(null)
}
})
this.app.auth().onAuthStateChanged((user) => {
if (user) {
this.currentUser = user
@@ -87,6 +124,14 @@ export class FirebaseInstance {
settings.push(setting)
})
this.currentSettings = settings
settings.forEach((e) => {
if (e && e.name && e.value != null) {
applySetting(e.name, e.value)
}
})
loadedSettings = true
})
this.usersCollection

17
helpers/migrations.ts Normal file
View File

@@ -0,0 +1,17 @@
import { settingsStore, applySetting } from "~/newstore/settings"
/*
* This file contains all the migrations we have to perform overtime in various (persisted)
* state/store entries
*/
export function performMigrations(): void {
// Migrate old default proxy URL to the new proxy URL (if not set / overridden)
if (
settingsStore.value.PROXY_URL === "https://hoppscotch.apollosoftware.xyz/"
) {
applySetting("PROXY_URL", "https://proxy.hoppscotch.io/")
}
}

View File

@@ -3,26 +3,25 @@ import ExtensionStrategy, {
cancelRunningExtensionRequest,
hasExtensionInstalled,
} from "./strategies/ExtensionStrategy"
import { settingsStore } from "~/newstore/settings"
export const cancelRunningRequest = (store) => {
if (isExtensionsAllowed(store) && hasExtensionInstalled()) {
export const cancelRunningRequest = () => {
if (isExtensionsAllowed() && hasExtensionInstalled()) {
cancelRunningExtensionRequest()
} else {
cancelRunningAxiosRequest()
}
}
const isExtensionsAllowed = ({ state }) =>
typeof state.postwoman.settings.EXTENSIONS_ENABLED === "undefined" ||
state.postwoman.settings.EXTENSIONS_ENABLED
const isExtensionsAllowed = () => settingsStore.value.EXTENSIONS_ENABLED
const runAppropriateStrategy = (req, store) => {
if (isExtensionsAllowed(store) && hasExtensionInstalled()) {
return ExtensionStrategy(req, store)
const runAppropriateStrategy = (req) => {
if (isExtensionsAllowed() && hasExtensionInstalled()) {
return ExtensionStrategy(req)
}
return AxiosStrategy(req, store)
return AxiosStrategy(req)
}
export const sendNetworkRequest = (req, store) =>
runAppropriateStrategy(req, store).finally(() => window.$nuxt.$loading.finish())
export const sendNetworkRequest = (req) =>
runAppropriateStrategy(req).finally(() => window.$nuxt.$loading.finish())

View File

@@ -1,5 +1,6 @@
import axios from "axios"
import { decodeB64StringToArrayBuffer } from "../utils/b64"
import { settingsStore } from "~/newstore/settings"
let cancelSource = axios.CancelToken.source()
@@ -10,10 +11,10 @@ export const cancelRunningAxiosRequest = () => {
cancelSource = axios.CancelToken.source()
}
const axiosWithProxy = async (req, { state }) => {
const axiosWithProxy = async (req) => {
try {
const { data } = await axios.post(
state.postwoman.settings.PROXY_URL || "https://proxy.hoppscotch.io",
settingsStore.value.PROXY_URL || "https://proxy.hoppscotch.io",
{
...req,
wantsBinary: true,
@@ -60,11 +61,11 @@ const axiosWithoutProxy = async (req, _store) => {
}
}
const axiosStrategy = (req, store) => {
if (store.state.postwoman.settings.PROXY_ENABLED) {
return axiosWithProxy(req, store)
const axiosStrategy = (req) => {
if (settingsStore.value.PROXY_ENABLED) {
return axiosWithProxy(req)
}
return axiosWithoutProxy(req, store)
return axiosWithoutProxy(req)
}
export const testables = {

View File

@@ -1,4 +1,5 @@
import { decodeB64StringToArrayBuffer } from "../utils/b64"
import { settingsStore } from "~/newstore/settings"
export const hasExtensionInstalled = () =>
typeof window.__POSTWOMAN_EXTENSION_HOOK__ !== "undefined"
@@ -15,12 +16,12 @@ export const cancelRunningExtensionRequest = () => {
}
}
const extensionWithProxy = async (req, { state }) => {
const extensionWithProxy = async (req) => {
const backupTimeDataStart = new Date().getTime()
const res = await window.__POSTWOMAN_EXTENSION_HOOK__.sendRequest({
method: "post",
url: state.postwoman.settings.PROXY_URL || "https://proxy.hoppscotch.io",
url: settingsStore.value.PROXY_URL || "https://proxy.hoppscotch.io/",
data: {
...req,
wantsBinary: true,
@@ -53,7 +54,7 @@ const extensionWithProxy = async (req, { state }) => {
return parsedData
}
const extensionWithoutProxy = async (req, _store) => {
const extensionWithoutProxy = async (req) => {
const backupTimeDataStart = new Date().getTime()
const res = await window.__POSTWOMAN_EXTENSION_HOOK__.sendRequest({
@@ -74,11 +75,11 @@ const extensionWithoutProxy = async (req, _store) => {
return res
}
const extensionStrategy = (req, store) => {
if (store.state.postwoman.settings.PROXY_ENABLED) {
return extensionWithProxy(req, store)
const extensionStrategy = (req) => {
if (settingsStore.value.PROXY_ENABLED) {
return extensionWithProxy(req)
}
return extensionWithoutProxy(req, store)
return extensionWithoutProxy(req)
}
export default extensionStrategy

View File

@@ -2,24 +2,25 @@ import axios from "axios"
import axiosStrategy from "../AxiosStrategy"
jest.mock("axios")
jest.mock("~/newstore/settings", () => {
return {
__esModule: true,
settingsStore: {
value: {
PROXY_ENABLED: false
}
}
}
})
axios.CancelToken.source.mockReturnValue({ token: "test" })
axios.mockResolvedValue({})
describe("axiosStrategy", () => {
describe("No-Proxy Requests", () => {
const store = {
state: {
postwoman: {
settings: {
PROXY_ENABLED: false,
PROXY_URL: "test",
},
},
},
}
test("sends request to the actual sender if proxy disabled", async () => {
await axiosStrategy({ url: "test" }, store)
await axiosStrategy({ url: "test" })
expect(axios).toBeCalledWith(
expect.objectContaining({
@@ -29,7 +30,7 @@ describe("axiosStrategy", () => {
})
test("asks axios to return data as arraybuffer", async () => {
await axiosStrategy({ url: "test" }, store)
await axiosStrategy({ url: "test" })
expect(axios).toBeCalledWith(
expect.objectContaining({
@@ -39,21 +40,21 @@ describe("axiosStrategy", () => {
})
test("resolves successful requests", async () => {
await expect(axiosStrategy({}, store)).resolves.toBeDefined()
await expect(axiosStrategy({})).resolves.toBeDefined()
})
test("rejects cancel errors with text 'cancellation'", async () => {
axios.isCancel.mockReturnValueOnce(true)
axios.mockRejectedValue("err")
expect(axiosStrategy({}, store)).rejects.toBe("cancellation")
expect(axiosStrategy({})).rejects.toBe("cancellation")
})
test("rejects non-cancellation errors as-is", async () => {
axios.isCancel.mockReturnValueOnce(false)
axios.mockRejectedValue("err")
expect(axiosStrategy({}, store)).rejects.toBe("err")
expect(axiosStrategy({})).rejects.toBe("err")
})
})
})

View File

@@ -5,6 +5,17 @@ jest.mock("../../utils/b64", () => ({
__esModule: true,
decodeB64StringToArrayBuffer: jest.fn((data) => `${data}-converted`),
}))
jest.mock("~/newstore/settings", () => {
return {
__esModule: true,
settingsStore: {
value: {
PROXY_ENABLED: true,
PROXY_URL: "test"
}
}
}
})
describe("cancelRunningAxiosRequest", () => {
test("cancels axios request and does that only 1 time", () => {
@@ -17,16 +28,6 @@ describe("cancelRunningAxiosRequest", () => {
describe("axiosStrategy", () => {
describe("Proxy Requests", () => {
const store = {
state: {
postwoman: {
settings: {
PROXY_ENABLED: true,
PROXY_URL: "test",
},
},
},
}
test("sends POST request to proxy if proxy is enabled", async () => {
let passedURL
@@ -36,7 +37,7 @@ describe("axiosStrategy", () => {
return Promise.resolve({ data: { success: true, isBinary: false } })
})
await axiosStrategy({}, store)
await axiosStrategy({})
expect(passedURL).toEqual("test")
})
@@ -55,7 +56,7 @@ describe("axiosStrategy", () => {
return Promise.resolve({ data: { success: true, isBinary: false } })
})
await axiosStrategy(reqFields, store)
await axiosStrategy(reqFields)
expect(passedFields).toMatchObject(reqFields)
})
@@ -68,7 +69,7 @@ describe("axiosStrategy", () => {
return Promise.resolve({ data: { success: true, isBinary: false } })
})
await axiosStrategy({}, store)
await axiosStrategy({})
expect(passedFields).toHaveProperty("wantsBinary")
})
@@ -83,7 +84,7 @@ describe("axiosStrategy", () => {
},
})
await expect(axiosStrategy({}, store)).rejects.toThrow("test message")
await expect(axiosStrategy({})).rejects.toThrow("test message")
})
test("checks for proxy response success field and throws error 'Proxy Error' for non-success", async () => {
@@ -94,7 +95,7 @@ describe("axiosStrategy", () => {
},
})
await expect(axiosStrategy({}, store)).rejects.toThrow("Proxy Error")
await expect(axiosStrategy({})).rejects.toThrow("Proxy Error")
})
test("checks for proxy response success and doesn't throw for success", async () => {
@@ -105,7 +106,7 @@ describe("axiosStrategy", () => {
},
})
await expect(axiosStrategy({}, store)).resolves.toBeDefined()
await expect(axiosStrategy({})).resolves.toBeDefined()
})
test("checks isBinary response field and resolve with the converted value if so", async () => {
@@ -117,7 +118,7 @@ describe("axiosStrategy", () => {
},
})
await expect(axiosStrategy({}, store)).resolves.toMatchObject({
await expect(axiosStrategy({})).resolves.toMatchObject({
data: "testdata-converted",
})
})
@@ -131,7 +132,7 @@ describe("axiosStrategy", () => {
},
})
await expect(axiosStrategy({}, store)).resolves.toMatchObject({
await expect(axiosStrategy({})).resolves.toMatchObject({
data: "testdata",
})
})
@@ -140,14 +141,14 @@ describe("axiosStrategy", () => {
jest.spyOn(axios, "post").mockRejectedValue("errr")
jest.spyOn(axios, "isCancel").mockReturnValueOnce(true)
await expect(axiosStrategy({}, store)).rejects.toBe("cancellation")
await expect(axiosStrategy({})).rejects.toBe("cancellation")
})
test("non-cancellation errors are thrown", async () => {
jest.spyOn(axios, "post").mockRejectedValue("errr")
jest.spyOn(axios, "isCancel").mockReturnValueOnce(false)
await expect(axiosStrategy({}, store)).rejects.toBe("errr")
await expect(axiosStrategy({})).rejects.toBe("errr")
})
})
})

View File

@@ -0,0 +1,220 @@
import extensionStrategy, {
hasExtensionInstalled,
hasChromeExtensionInstalled,
hasFirefoxExtensionInstalled,
cancelRunningExtensionRequest,
} from "../ExtensionStrategy"
jest.mock("../../utils/b64", () => ({
__esModule: true,
decodeB64StringToArrayBuffer: jest.fn((data) => `${data}-converted`),
}))
jest.mock("~/newstore/settings", () => {
return {
__esModule: true,
settingsStore: {
value: {
EXTENSIONS_ENABLED: true,
PROXY_ENABLED: false
}
}
}
})
describe("hasExtensionInstalled", () => {
test("returns true if extension is present and hooked", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
expect(hasExtensionInstalled()).toEqual(true)
})
test("returns false if extension not present or not hooked", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
expect(hasExtensionInstalled()).toEqual(false)
})
})
describe("hasChromeExtensionInstalled", () => {
test("returns true if extension is hooked and browser is chrome", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
expect(hasChromeExtensionInstalled()).toEqual(true)
})
test("returns false if extension is hooked and browser is not chrome", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
expect(hasChromeExtensionInstalled()).toEqual(false)
})
test("returns false if extension not installed and browser is chrome", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
expect(hasChromeExtensionInstalled()).toEqual(false)
})
test("returns false if extension not installed and browser is not chrome", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
expect(hasChromeExtensionInstalled()).toEqual(false)
})
})
describe("hasFirefoxExtensionInstalled", () => {
test("returns true if extension is hooked and browser is firefox", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
expect(hasFirefoxExtensionInstalled()).toEqual(true)
})
test("returns false if extension is hooked and browser is not firefox", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
expect(hasFirefoxExtensionInstalled()).toEqual(false)
})
test("returns false if extension not installed and browser is firefox", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
expect(hasFirefoxExtensionInstalled()).toEqual(false)
})
test("returns false if extension not installed and browser is not firefox", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
expect(hasFirefoxExtensionInstalled()).toEqual(false)
})
})
describe("cancelRunningExtensionRequest", () => {
const cancelFunc = jest.fn()
beforeEach(() => {
cancelFunc.mockClear()
})
test("cancels request if extension installed and function present in hook", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
cancelRunningRequest: cancelFunc,
}
cancelRunningExtensionRequest()
expect(cancelFunc).toHaveBeenCalledTimes(1)
})
test("does not cancel request if extension not installed", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
cancelRunningExtensionRequest()
expect(cancelFunc).not.toHaveBeenCalled()
})
test("does not cancel request if extension installed but function not present", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
cancelRunningExtensionRequest()
expect(cancelFunc).not.toHaveBeenCalled()
})
})
describe("extensionStrategy", () => {
const sendReqFunc = jest.fn()
beforeEach(() => {
sendReqFunc.mockClear()
})
describe("Non-Proxy Requests", () => {
test("ask extension to send request", async () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockResolvedValue({
data: '{"success":true,"data":""}',
})
await extensionStrategy({})
expect(sendReqFunc).toHaveBeenCalledTimes(1)
})
test("sends request to the actual sender if proxy disabled", async () => {
let passedUrl
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockImplementation(({ method, url }) => {
passedUrl = url
return Promise.resolve({
data: '{"success":true,"data":""}',
})
})
await extensionStrategy({ url: "test" })
expect(passedUrl).toEqual("test")
})
test("asks extension to get binary data", async () => {
let passedFields
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockImplementation((fields) => {
passedFields = fields
return Promise.resolve({
data: '{"success":true,"data":""}',
})
})
await extensionStrategy({})
expect(passedFields).toHaveProperty("wantsBinary")
})
test("resolves successful requests", async () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockResolvedValue({
data: '{"success":true,"data":""}',
})
await expect(extensionStrategy({})).resolves.toBeDefined()
})
test("rejects errors as-is", async () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockRejectedValue("err")
await expect(extensionStrategy({})).rejects.toBe("err")
})
})
})

View File

@@ -10,6 +10,19 @@ jest.mock("../../utils/b64", () => ({
decodeB64StringToArrayBuffer: jest.fn((data) => `${data}-converted`),
}))
jest.mock("~/newstore/settings", () => {
return {
__esModule: true,
settingsStore: {
value: {
EXTENSIONS_ENABLED: true,
PROXY_ENABLED: true,
PROXY_URL: "test"
}
}
}
})
describe("hasExtensionInstalled", () => {
test("returns true if extension is present and hooked", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
@@ -127,16 +140,6 @@ describe("extensionStrategy", () => {
})
describe("Proxy Requests", () => {
const store = {
state: {
postwoman: {
settings: {
PROXY_ENABLED: true,
PROXY_URL: "testURL",
},
},
},
}
test("asks extension to send request", async () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
@@ -147,7 +150,7 @@ describe("extensionStrategy", () => {
data: '{"success":true,"data":""}',
})
await extensionStrategy({}, store)
await extensionStrategy({})
expect(sendReqFunc).toHaveBeenCalledTimes(1)
})
@@ -169,9 +172,9 @@ describe("extensionStrategy", () => {
})
})
await extensionStrategy({}, store)
await extensionStrategy({})
expect(passedUrl).toEqual(store.state.postwoman.settings.PROXY_URL)
expect(passedUrl).toEqual("test")
expect(passedMethod).toEqual("post")
})
@@ -196,7 +199,7 @@ describe("extensionStrategy", () => {
})
})
await extensionStrategy(reqFields, store)
await extensionStrategy(reqFields)
expect(passedFields).toMatchObject(reqFields)
})
@@ -216,7 +219,7 @@ describe("extensionStrategy", () => {
})
})
await extensionStrategy({}, store)
await extensionStrategy({})
expect(passedFields).toHaveProperty("wantsBinary")
})
@@ -230,7 +233,7 @@ describe("extensionStrategy", () => {
data: '{"success":false,"data": { "message": "testerr" } }',
})
await expect(extensionStrategy({}, store)).rejects.toThrow("testerr")
await expect(extensionStrategy({})).rejects.toThrow("testerr")
})
test("checks for proxy response success field and throws error 'Proxy Error' for non-success", async () => {
@@ -242,7 +245,7 @@ describe("extensionStrategy", () => {
data: '{"success":false,"data": {} }',
})
await expect(extensionStrategy({}, store)).rejects.toThrow("Proxy Error")
await expect(extensionStrategy({})).rejects.toThrow("Proxy Error")
})
test("checks for proxy response success and doesn't throw for success", async () => {
@@ -254,7 +257,7 @@ describe("extensionStrategy", () => {
data: '{"success":true,"data": {} }',
})
await expect(extensionStrategy({}, store)).resolves.toBeDefined()
await expect(extensionStrategy({})).resolves.toBeDefined()
})
test("checks isBinary response field and resolve with the converted value if so", async () => {
@@ -266,7 +269,7 @@ describe("extensionStrategy", () => {
data: '{"success": true, "isBinary": true, "data": "testdata" }',
})
await expect(extensionStrategy({}, store)).resolves.toMatchObject({
await expect(extensionStrategy({})).resolves.toMatchObject({
data: "testdata-converted",
})
})
@@ -280,7 +283,7 @@ describe("extensionStrategy", () => {
data: '{"success": true, "isBinary": false, "data": "testdata" }',
})
await expect(extensionStrategy({}, store)).resolves.toMatchObject({
await expect(extensionStrategy({})).resolves.toMatchObject({
data: "testdata",
})
})
@@ -292,96 +295,7 @@ describe("extensionStrategy", () => {
sendReqFunc.mockRejectedValue("err")
await expect(extensionStrategy({}, store)).rejects.toBe("err")
})
})
describe("Non-Proxy Requests", () => {
const store = {
state: {
postwoman: {
settings: {
PROXY_ENABLED: false,
PROXY_URL: "testURL",
},
},
},
}
test("ask extension to send request", async () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockResolvedValue({
data: '{"success":true,"data":""}',
})
await extensionStrategy({}, store)
expect(sendReqFunc).toHaveBeenCalledTimes(1)
})
test("sends request to the actual sender if proxy disabled", async () => {
let passedUrl
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockImplementation(({ method, url }) => {
passedUrl = url
return Promise.resolve({
data: '{"success":true,"data":""}',
})
})
await extensionStrategy({ url: "test" }, store)
expect(passedUrl).toEqual("test")
})
test("asks extension to get binary data", async () => {
let passedFields
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockImplementation((fields) => {
passedFields = fields
return Promise.resolve({
data: '{"success":true,"data":""}',
})
})
await extensionStrategy({}, store)
expect(passedFields).toHaveProperty("wantsBinary")
})
test("resolves successful requests", async () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockResolvedValue({
data: '{"success":true,"data":""}',
})
await expect(extensionStrategy({}, store)).resolves.toBeDefined()
})
test("rejects errors as-is", async () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockRejectedValue("err")
await expect(extensionStrategy({}, store)).rejects.toBe("err")
await expect(extensionStrategy({})).rejects.toBe("err")
})
})
})