This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: currency switcher * test: useCurrency * feat(nuxt-module): price filter * chore: fix build * fix: problem with resolving url params for search * feat: change route with currency Co-authored-by: patzick <tomczyk.patryk@gmail.com>
- Loading branch information
Showing
35 changed files
with
5,253 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
import Vue from "vue"; | ||
import VueCompostionApi from "@vue/composition-api"; | ||
import { Ref, ref, reactive, computed } from "@vue/composition-api"; | ||
import { SessionContext } from "@shopware-pwa/commons/interfaces/response/SessionContext"; | ||
import * as Composables from "@shopware-pwa/composables"; | ||
import * as shopwareClient from "@shopware-pwa/shopware-6-client"; | ||
import { useCurrency, setStore } from "@shopware-pwa/composables"; | ||
import { Currency } from "@shopware-pwa/commons/interfaces/models/system/currency/Currency"; | ||
|
||
Vue.use(VueCompostionApi); | ||
|
||
jest.mock("@shopware-pwa/shopware-6-client"); | ||
const mockedApiClient = shopwareClient as jest.Mocked<typeof shopwareClient>; | ||
const consoleErrorSpy = jest.spyOn(console, "error"); | ||
consoleErrorSpy.mockImplementation(() => {}); | ||
|
||
describe("Composables - useCurrency", () => { | ||
const stateContext: Ref<Partial<SessionContext> | null> = ref(null); | ||
const mockedCurrentCurrency: Ref<Currency | null> = ref(null); | ||
const refreshSessionContextMock = jest.fn(async () => {}); | ||
const setCurrencyContextMock = jest.fn(async () => {}); | ||
const refreshCartMock = jest.fn(async () => {}); | ||
beforeEach(async () => { | ||
jest.resetAllMocks(); | ||
stateContext.value = null; | ||
mockedCurrentCurrency.value = null; | ||
setStore({ | ||
getters: reactive({ | ||
getSessionContext: computed(() => stateContext.value), | ||
}), | ||
commit: (name: string, value: SessionContext) => { | ||
stateContext.value = value; | ||
}, | ||
}); | ||
jest.spyOn(Composables, "useSessionContext").mockImplementation(() => { | ||
return { | ||
refreshSessionContext: refreshSessionContextMock, | ||
setCurrency: setCurrencyContextMock, | ||
currency: mockedCurrentCurrency, | ||
} as any; | ||
}); | ||
jest.spyOn(Composables, "useCart").mockImplementation(() => { | ||
return { | ||
refreshCart: refreshCartMock, | ||
} as any; | ||
}); | ||
mockedApiClient.getAvailableCurrencies.mockResolvedValue([] as any); | ||
}); | ||
|
||
afterEach(async () => { | ||
// clear shared available currencies array | ||
const { loadAvailableCurrencies } = useCurrency(); | ||
await loadAvailableCurrencies({ forceReload: true }); | ||
}); | ||
|
||
describe("computed", () => { | ||
describe("currency", () => { | ||
it("should return currency from useSessionContext", async () => { | ||
mockedCurrentCurrency.value = { symbol: "$$$" } as any; | ||
const { currency } = useCurrency(); | ||
expect(currency.value).toEqual({ symbol: "$$$" }); | ||
}); | ||
}); | ||
describe("currencySymbol", () => { | ||
it("should return an empty string if currency symbol object is missing", async () => { | ||
mockedCurrentCurrency.value = {} as any; | ||
const { currencySymbol } = useCurrency(); | ||
expect(currencySymbol.value).toBe(""); | ||
}); | ||
it("should return an empty string if currency is null", async () => { | ||
mockedCurrentCurrency.value = null; | ||
const { currencySymbol } = useCurrency(); | ||
expect(currencySymbol.value).toBe(""); | ||
}); | ||
it("should return a symbol of current currency", async () => { | ||
mockedCurrentCurrency.value = { symbol: "$" } as any; | ||
const { currencySymbol } = useCurrency(); | ||
expect(currencySymbol.value).toEqual("$"); | ||
}); | ||
}); | ||
describe("availableCurrencies", () => { | ||
it("should return empty array if there are no currencies loaded", async () => { | ||
const { availableCurrencies } = useCurrency(); | ||
expect(availableCurrencies.value).toStrictEqual([]); | ||
}); | ||
|
||
it("should return fetched array of currencies", async () => { | ||
mockedApiClient.getAvailableCurrencies.mockResolvedValueOnce([ | ||
{ | ||
iso: "EUR", | ||
}, | ||
] as any); | ||
|
||
const { loadAvailableCurrencies, availableCurrencies } = useCurrency(); | ||
await loadAvailableCurrencies(); | ||
expect(availableCurrencies.value).toEqual([ | ||
{ | ||
iso: "EUR", | ||
}, | ||
]); | ||
}); | ||
|
||
it("should return array with current currenry inside, when no currencies loaded", async () => { | ||
mockedCurrentCurrency.value = { symbol: "$$$" } as any; | ||
const { availableCurrencies } = useCurrency(); | ||
expect(availableCurrencies.value).toEqual([{ symbol: "$$$" }]); | ||
}); | ||
}); | ||
}); | ||
describe("methods", () => { | ||
describe("loadAvailableCurrencies", () => { | ||
it("should call apiClient:getAvailableCurrencies to fetch and set available currencies ", async () => { | ||
const { loadAvailableCurrencies } = useCurrency(); | ||
await loadAvailableCurrencies(); | ||
expect(mockedApiClient.getAvailableCurrencies).toBeCalledTimes(1); | ||
}); | ||
|
||
it("should not call apiClient:getAvailableCurrencies second time if values are fetched", async () => { | ||
mockedApiClient.getAvailableCurrencies.mockResolvedValueOnce([ | ||
{ | ||
iso: "EUR", | ||
}, | ||
] as any); | ||
|
||
const { loadAvailableCurrencies } = useCurrency(); | ||
await loadAvailableCurrencies(); | ||
await loadAvailableCurrencies(); | ||
expect(mockedApiClient.getAvailableCurrencies).toBeCalledTimes(1); | ||
}); | ||
|
||
it("should call apiClient:getAvailableCurrencies second if forceReload flag is used", async () => { | ||
mockedApiClient.getAvailableCurrencies.mockResolvedValueOnce([ | ||
{ | ||
iso: "EUR", | ||
}, | ||
] as any); | ||
|
||
const { loadAvailableCurrencies } = useCurrency(); | ||
await loadAvailableCurrencies(); | ||
await loadAvailableCurrencies({ forceReload: true }); | ||
expect(mockedApiClient.getAvailableCurrencies).toBeCalledTimes(2); | ||
}); | ||
}); | ||
|
||
describe("setCurrency", () => { | ||
it("should call setCurrency from useSessionContext", async () => { | ||
const { setCurrency } = useCurrency(); | ||
await setCurrency({ id: "some-currency-id" }); | ||
expect(setCurrencyContextMock).toBeCalledWith({ | ||
id: "some-currency-id", | ||
}); | ||
}); | ||
|
||
it("should call refreshSessionContext from useSessionContext", async () => { | ||
const { setCurrency } = useCurrency(); | ||
await setCurrency({ id: "some-currency-id" }); | ||
expect(refreshSessionContextMock).toBeCalled(); | ||
}); | ||
|
||
it("should call refreshCart from useCart", async () => { | ||
const { setCurrency } = useCurrency(); | ||
await setCurrency({ id: "some-currency-id" }); | ||
expect(refreshCartMock).toBeCalled(); | ||
}); | ||
|
||
it("should sidplay error when one of method throws an error", async () => { | ||
setCurrencyContextMock.mockRejectedValueOnce({ | ||
message: "Some error", | ||
} as any); | ||
const { setCurrency } = useCurrency(); | ||
await setCurrency({ id: "some-currency-id" }); | ||
expect(setCurrencyContextMock).toBeCalled(); | ||
expect(consoleErrorSpy).toBeCalledWith( | ||
"[useCurrency][setCurrency] Problem with currency change", | ||
{ | ||
message: "Some error", | ||
} | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.