Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Feat/store api for context related areas #551 #750

Merged
merged 4 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions api/shopware-6-client.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,19 @@ export function deleteCustomerAddress(addressId: string): Promise<void>;
export function getAvailableCountries(): Promise<SearchResult<Country[]>>;

// @alpha (undocumented)
export function getAvailableCurrencies(): Promise<SearchResult<Currency[]>>;
export function getAvailableCurrencies(): Promise<Currency[]>;

// @alpha (undocumented)
export function getAvailableLanguages(): Promise<SearchResult<Language[]>>;

// @alpha (undocumented)
export function getAvailablePaymentMethods(): Promise<SearchResult<PaymentMethod[]>>;
export function getAvailablePaymentMethods(): Promise<PaymentMethod[]>;

// @alpha
export function getAvailableSalutations(): Promise<SearchResult<Salutation[]>>;

// @alpha (undocumented)
export function getAvailableShippingMethods(): Promise<SearchResult<ShippingMethod[]>>;
export function getAvailableShippingMethods(): Promise<ShippingMethod[]>;

// @alpha
export function getCart(): Promise<Cart>;
Expand Down
91 changes: 48 additions & 43 deletions packages/composables/__tests__/useCheckout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,12 @@ describe("Composables - useCheckout", () => {
await getShippingMethods({ forceReload: true });
});
it("should return Shipping methods from API", async () => {
mockedApiClient.getAvailableShippingMethods.mockResolvedValueOnce({
total: 2,
data: [
{
name: "Shipping method 1",
},
{ name: "Shipping method 2" },
],
} as any);
mockedApiClient.getAvailableShippingMethods.mockResolvedValueOnce([
{
name: "Shipping method 1",
},
{ name: "Shipping method 2" },
] as any);
const { getShippingMethods } = useCheckout();
const result = await getShippingMethods();
expect(result.value).toEqual([
Expand All @@ -202,10 +199,10 @@ describe("Composables - useCheckout", () => {
]);
});

it("should return an empty array if response data is not defined", async () => {
mockedApiClient.getAvailableShippingMethods.mockResolvedValueOnce({
total: 2,
} as any);
it("should return an empty array if response data is an empty array", async () => {
mockedApiClient.getAvailableShippingMethods.mockResolvedValueOnce(
[] as any
);
const { getShippingMethods } = useCheckout();
const result = await getShippingMethods();
expect(result.value).toEqual([]);
Expand All @@ -222,15 +219,12 @@ describe("Composables - useCheckout", () => {
});

it("should not call api if Shipping methods are already in cache", async () => {
mockedApiClient.getAvailableShippingMethods.mockResolvedValueOnce({
total: 2,
data: [
{
name: "Shipping method 1",
},
{ name: "Shipping method 2" },
],
} as any);
mockedApiClient.getAvailableShippingMethods.mockResolvedValueOnce([
{
name: "Shipping method 1",
},
{ name: "Shipping method 2" },
] as any);
const { getShippingMethods } = useCheckout();

const result = await getShippingMethods();
Expand All @@ -246,6 +240,14 @@ describe("Composables - useCheckout", () => {

expect(mockedApiClient.getAvailableShippingMethods).toBeCalledTimes(1);
});
it("should return an empty array if response data is not defined", async () => {
mockedApiClient.getAvailableShippingMethods.mockResolvedValueOnce(
undefined as any
);
const { getShippingMethods } = useCheckout();
const result = await getShippingMethods();
expect(result.value).toEqual([]);
});
});

describe("getPaymentMethods", () => {
Expand All @@ -258,15 +260,12 @@ describe("Composables - useCheckout", () => {
await getPaymentMethods({ forceReload: true });
});
it("should return Payment methods from API", async () => {
mockedApiClient.getAvailablePaymentMethods.mockResolvedValueOnce({
total: 2,
data: [
{
name: "Payment method 1",
},
{ name: "Payment method 2" },
],
} as any);
mockedApiClient.getAvailablePaymentMethods.mockResolvedValueOnce([
{
name: "Payment method 1",
},
{ name: "Payment method 2" },
] as any);
const { getPaymentMethods } = useCheckout();
const result = await getPaymentMethods();
expect(result.value).toEqual([
Expand All @@ -275,10 +274,19 @@ describe("Composables - useCheckout", () => {
]);
});

it("should return an empty array if response data is an empty array", async () => {
mockedApiClient.getAvailablePaymentMethods.mockResolvedValueOnce(
[] as any
);
const { getPaymentMethods } = useCheckout();
const result = await getPaymentMethods();
expect(result.value).toEqual([]);
});

it("should return an empty array if response data is not defined", async () => {
mockedApiClient.getAvailablePaymentMethods.mockResolvedValueOnce({
total: 2,
} as any);
mockedApiClient.getAvailablePaymentMethods.mockResolvedValueOnce(
undefined as any
);
const { getPaymentMethods } = useCheckout();
const result = await getPaymentMethods();
expect(result.value).toEqual([]);
Expand All @@ -295,15 +303,12 @@ describe("Composables - useCheckout", () => {
});

it("should not call api if Payment methods are already in cache", async () => {
mockedApiClient.getAvailablePaymentMethods.mockResolvedValueOnce({
total: 2,
data: [
{
name: "Payment method 1",
},
{ name: "Payment method 2" },
],
} as any);
mockedApiClient.getAvailablePaymentMethods.mockResolvedValueOnce([
{
name: "Payment method 1",
},
{ name: "Payment method 2" },
] as any);
const { getPaymentMethods } = useCheckout();

const result = await getPaymentMethods();
Expand Down
2 changes: 1 addition & 1 deletion packages/composables/__tests__/useProductListing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ describe("Composables - useProductListing", () => {

it("should not change pagination state to privided one once a useProductListing argument is passed hasn't any required fields", async () => {
const { pagination } = useProductListing({
page: undefined
page: undefined,
} as any);

expect(pagination.value).toStrictEqual({
Expand Down
4 changes: 2 additions & 2 deletions packages/composables/src/logic/useCheckout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const useCheckout = (): UseCheckout => {
) => {
if (shippingMethods.value.length && !forceReload) return shippingMethods;
const shippingMethodsResponse = await getAvailableShippingMethods();
orderData.shippingMethods = shippingMethodsResponse.data || [];
orderData.shippingMethods = shippingMethodsResponse || [];
return shippingMethods;
};

Expand All @@ -77,7 +77,7 @@ export const useCheckout = (): UseCheckout => {
) => {
if (paymentMethods.value.length && !forceReload) return paymentMethods;
const paymentMethodsResponse = await getAvailablePaymentMethods();
orderData.paymentMethods = paymentMethodsResponse.data || [];
orderData.paymentMethods = paymentMethodsResponse || [];
return paymentMethods;
};

Expand Down
8 changes: 4 additions & 4 deletions packages/shopware-6-client/__tests__/endpoints.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,14 @@ describe("endpoints", () => {
describe("getContextCurrencyEndpoint", () => {
it("should return Shopware currency endpoint", async () => {
const result = getContextCurrencyEndpoint();
expect(result).toEqual("/sales-channel-api/v1/currency");
expect(result).toEqual("/store-api/v1/currency");
});
});

describe("getContextLanguageEndpoint", () => {
it("should return Shopware language endpoint", async () => {
const result = getContextLanguageEndpoint();
expect(result).toEqual("/sales-channel-api/v1/language");
expect(result).toEqual("/store-api/v1/language");
});
});

Expand All @@ -251,13 +251,13 @@ describe("endpoints", () => {
describe("getContextPaymentMethodEndpoint", () => {
it("should return Shopware payment method endpoint", async () => {
const result = getContextPaymentMethodEndpoint();
expect(result).toEqual("/sales-channel-api/v1/payment-method");
expect(result).toEqual("/store-api/v1/payment-method");
});
});
describe("getContextShippingMethodEndpoint", () => {
it("should return Shopware shipping method endpoint", async () => {
const result = getContextShippingMethodEndpoint();
expect(result).toEqual("/sales-channel-api/v1/shipping-method");
expect(result).toEqual("/store-api/v1/shipping-method");
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ describe("ContextService - getAvailableCurrencies", () => {
jest.resetAllMocks();
});
it("should return array with currencies", async () => {
mockedAxios.get.mockResolvedValueOnce({ data: { data: [{ iso: "EUR" }] } });
mockedAxios.get.mockResolvedValueOnce({ data: [{ iso: "EUR" }] });
const result = await getAvailableCurrencies();
expect(mockedAxios.get).toBeCalledTimes(1);
expect(mockedAxios.get).toBeCalledWith("/sales-channel-api/v1/currency");
expect(mockedAxios.get).toBeCalledWith("/store-api/v1/currency");
expect(result).toEqual([{ iso: "EUR" }]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("ContextService - getAvailableLanguages", () => {

const result = await getAvailableLanguages();
expect(mockedAxios.get).toBeCalledTimes(1);
expect(mockedAxios.get).toBeCalledWith("/sales-channel-api/v1/language");
expect(mockedAxios.get).toBeCalledWith("/store-api/v1/language");
expect(result.total).toEqual(2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ describe("ContextService - getAvailablePaymentMethods", () => {
jest.resetAllMocks();
});
it("should return array with payment methods", async () => {
mockedAxios.get.mockResolvedValueOnce({ data: { total: 2 } });
mockedAxios.get.mockResolvedValueOnce({ data: [{ id: 1 }, { id: 2 }] });

const result = await getAvailablePaymentMethods();
expect(mockedAxios.get).toBeCalledTimes(1);
expect(mockedAxios.get).toBeCalledWith(
"/sales-channel-api/v1/payment-method"
);
expect(result.total).toEqual(2);
expect(mockedAxios.get).toBeCalledWith("/store-api/v1/payment-method");
expect(result).toHaveLength(2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ describe("ContextService - getAvailableShippingMethods", () => {
jest.resetAllMocks();
});
it("should return array with shipping methods", async () => {
mockedAxios.get.mockResolvedValueOnce({ data: { total: 2 } });
mockedAxios.get.mockResolvedValueOnce({ data: [{ id: 1 }, { id: 2 }] });

const result = await getAvailableShippingMethods();
expect(mockedAxios.get).toBeCalledTimes(1);
expect(mockedAxios.get).toBeCalledWith(
"/sales-channel-api/v1/shipping-method"
);
expect(result.total).toEqual(2);
expect(mockedAxios.get).toBeCalledWith("/store-api/v1/shipping-method");
expect(result).toHaveLength(2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("ContextService - getSessionContext", () => {

const result: SessionContext = await getSessionContext();
expect(mockedAxios.get).toBeCalledTimes(1);
expect(mockedAxios.get).toBeCalledWith("/sales-channel-api/v1/context");
expect(mockedAxios.get).toBeCalledWith("/store-api/v1/context");
expect(result.token).toEqual("qwerty");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("ContextService - setCurrentBillingAddress with contextToken given", ()
const result = await setCurrentBillingAddress(newBillingAddressId);

expect(mockedAxios.patch).toBeCalledTimes(1);
expect(mockedAxios.patch).toBeCalledWith("/sales-channel-api/v1/context", {
expect(mockedAxios.patch).toBeCalledWith("/store-api/v1/context", {
billingAddressId: "45f96f681f9d4834b29e9e15df3a7149",
});

Expand All @@ -46,7 +46,7 @@ describe("ContextService - setCurrentBillingAddress without contextToken given",

const result = await setCurrentBillingAddress(newBillingAddressId);
expect(mockedAxios.patch).toBeCalledTimes(1);
expect(mockedAxios.patch).toBeCalledWith("/sales-channel-api/v1/context", {
expect(mockedAxios.patch).toBeCalledWith("/store-api/v1/context", {
billingAddressId: "45f96f681f9d4834b29e9e15df3a7149",
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe("ContextService - setCurrentCurrency with contextToken given", () => {
const result = await setCurrentCurrency(newCurrencyId);

expect(mockedAxios.patch).toBeCalledTimes(1);
expect(mockedAxios.patch).toBeCalledWith("/sales-channel-api/v1/context", {
expect(mockedAxios.patch).toBeCalledWith("/store-api/v1/context", {
currencyId: "45f96f681f9d4834b29e9e15df3a7149",
});

Expand All @@ -49,7 +49,7 @@ describe("ContextService - setCurrentCurrency without contextToken given", () =>
const result = await setCurrentCurrency(newCurrencyId);

expect(mockedAxios.patch).toBeCalledTimes(1);
expect(mockedAxios.patch).toBeCalledWith("/sales-channel-api/v1/context", {
expect(mockedAxios.patch).toBeCalledWith("/store-api/v1/context", {
currencyId: "45f96f681f9d4834b29e9e15df3a7149",
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("ContextService - setCurrentLanguage with contextToken given", () => {
const result = await setCurrentLanguage(newLanguageId);

expect(mockedAxios.patch).toBeCalledTimes(1);
expect(mockedAxios.patch).toBeCalledWith("/sales-channel-api/v1/context", {
expect(mockedAxios.patch).toBeCalledWith("/store-api/v1/context", {
languageId: "45f96f681f9d4834b29e9e15df3a7149",
});

Expand All @@ -44,7 +44,7 @@ describe("ContextService - setCurrentLanguage without contextToken given", () =>
const result = await setCurrentLanguage(newLanguageId);

expect(mockedAxios.patch).toBeCalledTimes(1);
expect(mockedAxios.patch).toBeCalledWith("/sales-channel-api/v1/context", {
expect(mockedAxios.patch).toBeCalledWith("/store-api/v1/context", {
languageId: "45f96f681f9d4834b29e9e15df3a7149",
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("ContextService - setCurrentPaymentMethod with contextToken given", ()
const result = await setCurrentPaymentMethod(newPaymentMethodId);

expect(mockedAxios.patch).toBeCalledTimes(1);
expect(mockedAxios.patch).toBeCalledWith("/sales-channel-api/v1/context", {
expect(mockedAxios.patch).toBeCalledWith("/store-api/v1/context", {
paymentMethodId: "45f96f681f9d4834b29e9e15df3a7149",
});

Expand All @@ -47,7 +47,7 @@ describe("ContextService - setCurrentPaymentMethod without contextToken given",
const result = await setCurrentPaymentMethod(newPaymentMethodId);

expect(mockedAxios.patch).toBeCalledTimes(1);
expect(mockedAxios.patch).toBeCalledWith("/sales-channel-api/v1/context", {
expect(mockedAxios.patch).toBeCalledWith("/store-api/v1/context", {
paymentMethodId: "45f96f681f9d4834b29e9e15df3a7149",
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("ContextService - setCurrentShippingAddress with contextToken given", (
const result = await setCurrentShippingAddress(newShippingAddressId);

expect(mockedAxios.patch).toBeCalledTimes(1);
expect(mockedAxios.patch).toBeCalledWith("/sales-channel-api/v1/context", {
expect(mockedAxios.patch).toBeCalledWith("/store-api/v1/context", {
shippingAddressId: "45f96f681f9d4834b29e9e15df3a7149",
});

Expand All @@ -46,7 +46,7 @@ describe("ContextService - setCurrentShippingAddress without contextToken given"

const result = await setCurrentShippingAddress(newShippingAddressId);
expect(mockedAxios.patch).toBeCalledTimes(1);
expect(mockedAxios.patch).toBeCalledWith("/sales-channel-api/v1/context", {
expect(mockedAxios.patch).toBeCalledWith("/store-api/v1/context", {
shippingAddressId: "45f96f681f9d4834b29e9e15df3a7149",
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("ContextService - setCurrentShippingMethod with contextToken given", ()
const result = await setCurrentShippingMethod(newShippingMethodId);

expect(mockedAxios.patch).toBeCalledTimes(1);
expect(mockedAxios.patch).toBeCalledWith("/sales-channel-api/v1/context", {
expect(mockedAxios.patch).toBeCalledWith("/store-api/v1/context", {
shippingMethodId: "45f96f681f9d4834b29e9e15df3a7149",
});

Expand All @@ -47,7 +47,7 @@ describe("ContextService - setCurrentShippingMethod without contextToken given",
const result = await setCurrentShippingMethod(newShippingMethodId);

expect(mockedAxios.patch).toBeCalledTimes(1);
expect(mockedAxios.patch).toBeCalledWith("/sales-channel-api/v1/context", {
expect(mockedAxios.patch).toBeCalledWith("/store-api/v1/context", {
shippingMethodId: "45f96f681f9d4834b29e9e15df3a7149",
});

Expand Down
Loading