Skip to content

Commit

Permalink
fix(composables): loading currencies
Browse files Browse the repository at this point in the history
  • Loading branch information
mkucmus committed Jul 15, 2022
1 parent 78821a7 commit 4b0e764
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 33 deletions.
2 changes: 1 addition & 1 deletion api/shopware-6-client.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export const getAddWishlistProductEndpoint: (productId: string) => string;
export function getAvailableCountries(contextInstance?: ShopwareApiInstance): Promise<EntityResult<"country", Country[]>>;

// @public (undocumented)
export function getAvailableCurrencies(contextInstance?: ShopwareApiInstance): Promise<EntityResult<"currency", Currency[]>>;
export function getAvailableCurrencies(contextInstance?: ShopwareApiInstance): Promise<Currency[]>;

// @public (undocumented)
export function getAvailableLanguages(contextInstance?: ShopwareApiInstance): Promise<EntityResult<"language", Language[]>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<b>Signature:</b>

```typescript
export declare function getAvailableCurrencies(contextInstance?: ShopwareApiInstance): Promise<EntityResult<"currency", Currency[]>>;
export declare function getAvailableCurrencies(contextInstance?: ShopwareApiInstance): Promise<Currency[]>;
```

## Parameters
Expand All @@ -18,7 +18,7 @@ export declare function getAvailableCurrencies(contextInstance?: ShopwareApiInst

<b>Returns:</b>

Promise&lt;EntityResult&lt;"currency", Currency\[\]&gt;&gt;
Promise&lt;Currency\[\]&gt;

## Exceptions

Expand Down
48 changes: 20 additions & 28 deletions packages/composables/__tests__/useCurrency.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,11 @@ describe("Composables - useCurrency", () => {
});

it("should return fetched array of currencies", async () => {
mockedApiClient.getAvailableCurrencies.mockResolvedValueOnce({
elements: [
{
iso: "EUR",
},
],
} as any);
mockedApiClient.getAvailableCurrencies.mockResolvedValueOnce([
{
iso: "EUR",
},
] as any);

const { loadAvailableCurrencies, availableCurrencies } = useCurrency();
await loadAvailableCurrencies();
Expand All @@ -112,13 +110,11 @@ describe("Composables - useCurrency", () => {
});

it("should not override an empty currencies if the response does not have any", async () => {
mockedApiClient.getAvailableCurrencies.mockResolvedValueOnce({
elements: [
{
iso: "EUR",
},
],
} as any);
mockedApiClient.getAvailableCurrencies.mockResolvedValueOnce([
{
iso: "EUR",
},
] as any);

const { loadAvailableCurrencies, availableCurrencies } = useCurrency();
await loadAvailableCurrencies();
Expand Down Expand Up @@ -149,13 +145,11 @@ describe("Composables - useCurrency", () => {
});

it("should not call apiClient:getAvailableCurrencies second time if values are fetched", async () => {
mockedApiClient.getAvailableCurrencies.mockResolvedValueOnce({
elements: [
{
iso: "EUR",
},
],
} as any);
mockedApiClient.getAvailableCurrencies.mockResolvedValueOnce([
{
iso: "EUR",
},
] as any);

const { loadAvailableCurrencies } = useCurrency();
await loadAvailableCurrencies();
Expand All @@ -164,13 +158,11 @@ describe("Composables - useCurrency", () => {
});

it("should call apiClient:getAvailableCurrencies second if forceReload flag is used", async () => {
mockedApiClient.getAvailableCurrencies.mockResolvedValueOnce({
elements: [
{
iso: "EUR",
},
],
} as any);
mockedApiClient.getAvailableCurrencies.mockResolvedValueOnce([
{
iso: "EUR",
},
] as any);

const { loadAvailableCurrencies } = useCurrency();
await loadAvailableCurrencies();
Expand Down
2 changes: 1 addition & 1 deletion packages/composables/src/hooks/useCurrency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function useCurrency(): IUseCurrency {
}): Promise<void> => {
if (!options?.forceReload && _availableCurrencies.value?.length) return;
const response = await getAvailableCurrencies(apiInstance);
_availableCurrencies.value = response?.elements;
_availableCurrencies.value = response;
};

const setCurrency = async (currency: Partial<Currency>): Promise<void> => {
Expand Down
2 changes: 1 addition & 1 deletion packages/shopware-6-client/src/services/contextService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function setCurrentBillingAddress(
*/
export async function getAvailableCurrencies(
contextInstance: ShopwareApiInstance = defaultInstance
): Promise<EntityResult<"currency", Currency[]>> {
): Promise<Currency[]> {
const { data } = await contextInstance.invoke.get(
getContextCurrencyEndpoint()
);
Expand Down

0 comments on commit 4b0e764

Please sign in to comment.