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

Commit

Permalink
feat(:rocket:): switch to new api for shopware v6.2 (#720)
Browse files Browse the repository at this point in the history
* feat(composables): update customer specific areas for new api

* feat(client): adjustments for SW v6.2 (store-api)

* feat(nuxt-module): remove api suffix after API domain URL
  • Loading branch information
mkucmus authored May 13, 2020
1 parent 0b6f3d6 commit 28bac89
Show file tree
Hide file tree
Showing 65 changed files with 684 additions and 399 deletions.
3 changes: 2 additions & 1 deletion api/composables.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { NavigationElement } from '@shopware-pwa/commons/interfaces/models/conte
import { Order } from '@shopware-pwa/commons/interfaces/models/checkout/order/Order';
import { PaymentMethod } from '@shopware-pwa/commons/interfaces/models/checkout/payment/PaymentMethod';
import { Product } from '@shopware-pwa/commons/interfaces/models/content/product/Product';
import { ProductListingResult } from '@shopware-pwa/commons/interfaces/response/ProductListingResult';
import { Ref } from '@vue/composition-api';
import { Salutation } from '@shopware-pwa/commons/interfaces/models/system/salutation/Salutation';
import { SessionContext } from '@shopware-pwa/commons/interfaces/response/SessionContext';
Expand Down Expand Up @@ -201,7 +202,7 @@ export interface UseProductListing {
}

// @alpha (undocumented)
export const useProductListing: (initialProducts?: Product[]) => UseProductListing;
export const useProductListing: (initialListing?: ProductListingResult | undefined) => UseProductListing;

// @alpha (undocumented)
export interface UseProductSearch {
Expand Down
6 changes: 6 additions & 0 deletions api/shopware-6-client.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { NotFilter } from '@shopware-pwa/commons/interfaces/search/SearchFilter'
import { Order } from '@shopware-pwa/commons/interfaces/models/checkout/order/Order';
import { PaymentMethod } from '@shopware-pwa/commons/interfaces/models/checkout/payment/PaymentMethod';
import { Product } from '@shopware-pwa/commons/interfaces/models/content/product/Product';
import { ProductListingResult } from '@shopware-pwa/commons/interfaces/response/ProductListingResult';
import { RangeFilter } from '@shopware-pwa/commons/interfaces/search/SearchFilter';
import { Salutation } from '@shopware-pwa/commons/interfaces/models/system/salutation/Salutation';
import { SearchCriteria } from '@shopware-pwa/commons/interfaces/search/SearchCriteria';
Expand Down Expand Up @@ -151,6 +152,9 @@ export function getCategories(searchCriteria?: SearchCriteria): Promise<SearchRe
// @alpha (undocumented)
export function getCategory(categoryId: string): Promise<Category>;

// @alpha
export const getCategoryProductsListing: (categoryId: string, searchCriteria?: SearchCriteria | undefined) => Promise<ProductListingResult>;

// @beta
export function getCustomer(): Promise<Customer | null>;

Expand Down Expand Up @@ -285,6 +289,8 @@ export interface ShopwareParams {
// (undocumented)
limit?: number;
// (undocumented)
p?: number;
// (undocumented)
page?: number;
// (undocumented)
sort?: string;
Expand Down
25 changes: 25 additions & 0 deletions packages/commons/interfaces/response/ProductListingResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Product } from "@shopware-pwa/commons/interfaces/models/content/product/Product";
import { Sort } from "@shopware-pwa/commons/interfaces/search/SearchCriteria";
import { Aggregation } from "@shopware-pwa/commons/interfaces/search/Aggregation";
import {
EqualsFilter,
RangeFilter,
MultiFilter,
EqualsAnyFilter,
} from "@shopware-pwa/commons/interfaces/search/SearchFilter";
export interface ProductListingResult {
/**
* apiAlias - determines the entity name that can be used within "includes" functionality (added in store-api)
*/
apiAlias: string;
total: number;
elements: Product[];
sorting: string;
page: number;
limit: number;
sortings: Sort[];
aggregations: Aggregation[];
currentFilters: Array<
EqualsFilter | EqualsAnyFilter | RangeFilter | MultiFilter
>;
}
10 changes: 5 additions & 5 deletions packages/commons/interfaces/search/SearchCriteria.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Pagination } from "./Pagination";
import { Pagination } from "@shopware-pwa/commons/interfaces/search/Pagination";
import {
EqualsFilter,
RangeFilter,
MultiFilter,
EqualsAnyFilter,
} from "./SearchFilter";
import { Association } from "./Association";
import { Aggregation } from "./Aggregation";
import { TotalCountMode } from "./TotalCountMode";
} from "@shopware-pwa/commons/interfaces/search/SearchFilter";
import { Association } from "@shopware-pwa/commons/interfaces/search/Association";
import { Aggregation } from "@shopware-pwa/commons/interfaces/search/Aggregation";
import { TotalCountMode } from "@shopware-pwa/commons/interfaces/search/TotalCountMode";

/**
* @alpha
Expand Down
13 changes: 13 additions & 0 deletions packages/composables/__tests__/useCms.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ describe("Shopware composables", () => {
expect(error.value).toStrictEqual({ message: "Something went wrong..." });
});

it("should performs search default pagination limit if not provided", async () => {
const { search, page } = useCms();
mockedGetPage.getPage.mockResolvedValueOnce({} as any);
expect(page.value).toEqual(null);
await search("");
expect(mockedGetPage.getPage).toBeCalledWith("", {
configuration: {
associations: [{ associations: [{ name: "group" }], name: "options" }],
},
pagination: { limit: 10 },
});
});

it("should return activeCategoryId if it's included within the page object", async () => {
const { categoryId, search } = useCms();
const response: shopwareClient.PageResolverResult<any> = {
Expand Down
88 changes: 76 additions & 12 deletions packages/composables/__tests__/useProductListing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("Composables - useProductListing", () => {
expect(products.value).toHaveLength(0);
});
it("should have empty array if no products passed", async () => {
const { products } = useProductListing([]);
const { products } = useProductListing({ elements: [] } as any);
expect(products.value).toHaveLength(0);
});
});
Expand Down Expand Up @@ -99,30 +99,57 @@ describe("Composables - useProductListing", () => {
expect(selectedFilters.value).toHaveProperty("color");
expect(selectedFilters.value.color).toStrictEqual([]);
});

it("selectedFilters should append the filters array on force", async () => {
const {
selectedFilters,
toggleFilter,
resetFilters,
} = useProductListing();
resetFilters();

toggleFilter({
type: SearchFilterType.EQUALS,
value: "white",
field: "color",
} as EqualsFilter);

toggleFilter({
type: SearchFilterType.EQUALS,
value: "black",
field: "color",
} as EqualsFilter),
true;

expect(selectedFilters.value).toHaveProperty("color");
expect(selectedFilters.value.color).toStrictEqual(["white", "black"]);
});
});

describe("search", () => {
it("should reset search criteria on category change event", async () => {
const { products, selectedFilters } = useProductListing([
{ product: "1" } as any,
]);
const { products, selectedFilters } = useProductListing({
elements: [{ product: "1" }],
} as any);
expect(selectedFilters.value).toStrictEqual({});

expect(products.value).toHaveLength(1);
});

it("should set loading property to false when search is done", async () => {
const { loading, search } = useProductListing([{ product: "1" } as any]);
const { loading, search } = useProductListing({
elements: [{ product: "1" }],
} as any);
await search();
expect(loading.value).toBe(false);
});

//
it("should return default total and empty product listing when page resolver fails", async () => {
mockedGetPage.getProducts.mockResolvedValueOnce({} as any);
mockedGetPage.getCategoryProductsListing.mockResolvedValueOnce({} as any);

const { pagination, products, search } = useProductListing();
await search();
const { products, search, pagination } = useProductListing();
search();
expect(pagination.value).toStrictEqual({
currentPage: 1,
perPage: 10,
Expand All @@ -132,8 +159,8 @@ describe("Composables - useProductListing", () => {
});

it("should return products if exist", async () => {
mockedGetPage.getProducts.mockResolvedValueOnce({
data: [
mockedGetPage.getCategoryProductsListing.mockResolvedValueOnce({
elements: [
{
id: "123456",
},
Expand Down Expand Up @@ -175,18 +202,55 @@ describe("Composables - useProductListing", () => {
it("should perform no search and leave default pagination if no change performed", async () => {
const { pagination, changePagination } = useProductListing();

changePagination(undefined as any);
await changePagination(undefined as any);
expect(pagination.value).toStrictEqual({
currentPage: 1,
perPage: 10,
total: 0,
});
});

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
} as any);

expect(pagination.value).toStrictEqual({
currentPage: 1,
perPage: 10,
total: 0,
});
});

it("should not change pagination state to privided one once a useProductListing argument is passed has no pagination data", async () => {
const { pagination } = useProductListing(undefined as any);

expect(pagination.value).toStrictEqual({
currentPage: 1,
perPage: 10,
total: 0,
});
});

it("should change pagination state to privided one as a useProductListing argument is passed", async () => {
const { pagination } = useProductListing({
total: 6,
page: 2,
limit: 10,
elements: [{ id: "123456" }],
} as any);

expect(pagination.value).toStrictEqual({
currentPage: 2,
perPage: 10,
total: 6,
});
});

it("should perform change the shared pagination object if change succeeds", async () => {
const { pagination, changePagination } = useProductListing();

changePagination(10);
await changePagination(10);
expect(pagination.value).toStrictEqual({
currentPage: 10,
perPage: 10,
Expand Down
Loading

1 comment on commit 28bac89

@vercel
Copy link

@vercel vercel bot commented on 28bac89 May 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.