From c29bb00cdd0e30bb755177fb8c884955cdd1867c Mon Sep 17 00:00:00 2001 From: Jaskaran Sarkaria Date: Wed, 13 Nov 2024 21:34:16 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20sort=20by=20nests=20and?= =?UTF-8?q?=20fix=20sort=20tests=20(#49)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/ProductView/ProductView.svelte | 2 +- .../ProductView/ProductView.test.ts | 196 ++++++++++++------ src/lib/types/product.ts | 1 + src/lib/utils/requests.ts | 12 +- 4 files changed, 139 insertions(+), 72 deletions(-) diff --git a/src/lib/components/ProductView/ProductView.svelte b/src/lib/components/ProductView/ProductView.svelte index 0099544..f215155 100644 --- a/src/lib/components/ProductView/ProductView.svelte +++ b/src/lib/components/ProductView/ProductView.svelte @@ -68,7 +68,7 @@ return collatedArray .slice() .sort((a, b) => - new Date(a.updated_at) < new Date(b.updated_at) ? 1 : -1 + new Date(a.created_at) < new Date(b.created_at) ? 1 : -1 ); default: browser && window.sessionStorage.setItem("filter", "in-stock"); diff --git a/src/lib/components/ProductView/ProductView.test.ts b/src/lib/components/ProductView/ProductView.test.ts index efc2dd8..277ade4 100644 --- a/src/lib/components/ProductView/ProductView.test.ts +++ b/src/lib/components/ProductView/ProductView.test.ts @@ -9,6 +9,7 @@ const mockData: readonly SquareProduct[] = [ { id: "123", updated_at: "", + created_at: "2024-11-12T10:49:06.237Z", custom_attribute_values: {}, item_data: { name: "Elephant", @@ -40,6 +41,7 @@ const mockData: readonly SquareProduct[] = [ { id: "789", updated_at: "", + created_at: "2024-11-13T10:49:06.237Z", custom_attribute_values: {}, item_data: { name: "Dog", @@ -313,25 +315,41 @@ describe("GIVEN ProductView", () => { expect(sortedProducts[3]).toHaveTextContent("90"); }); - it.skip("THEN sort the products by stock", async () => { + it("THEN sort the products by stock", async () => { render(ProductView, { productArr: [ ...mockData, { - Id: 789, - Name: "Cat", - CategoryId: 999, - Description: "small animal", - SalePrice: 66, - ProductImages: [], - ProductTags: [], - VariantGroupId: null, - CurrentStock: 999, - ProductDetails: { - DetailedDescription: "agile", + id: "888", + updated_at: "", + created_at: "2024-11-15T10:49:06.237Z", + custom_attribute_values: {}, + item_data: { + name: "Cat", + product_type: "", + categories: [{ id: "999" }], + is_archived: false, + ecom_visibility: "VISIBLE", + description_html: "big animal", + variations: [ + { + id: "7777", + custom_attribute_values: { + image_arr: { + string_value: "foobar-0-9sjs9s", + }, + }, + item_variation_data: { + item_id: "888", + name: "", + price_money: { + amount: 900, + }, + quantity: "999", + }, + }, + ], }, - SellOnWeb: true, - IsArchived: false, }, ], }); @@ -343,7 +361,7 @@ describe("GIVEN ProductView", () => { }); expect(products[0]).toHaveTextContent("Cat"); - expect(products[1]).toHaveTextContent("66"); + expect(products[1]).toHaveTextContent("9.00"); expect(products[2]).toHaveTextContent("Elephant"); expect(products[3]).toHaveTextContent("90"); expect(products[4]).toHaveTextContent("Dog"); @@ -374,32 +392,48 @@ describe("GIVEN ProductView", () => { const sortedProducts = screen.getAllByRole("heading", { level: 3 }); expect(sortedProducts[0]).toHaveTextContent("Cat"); - expect(sortedProducts[1]).toHaveTextContent("66"); + expect(sortedProducts[1]).toHaveTextContent("9.00"); expect(sortedProducts[2]).toHaveTextContent("Elephant"); expect(sortedProducts[3]).toHaveTextContent("90"); expect(sortedProducts[4]).toHaveTextContent("Dog"); expect(sortedProducts[5]).toHaveTextContent("10"); }); - it.skip("THEN sort the products by newest to oldest", async () => { + it("THEN sort the products by newest to oldest", async () => { render(ProductView, { productArr: [ ...mockData, { - Id: 789, - Name: "Cat", - CategoryId: 999, - Description: "small animal", - SalePrice: 66, - ProductImages: [], - ProductTags: [], - VariantGroupId: null, - CurrentStock: 999, - ProductDetails: { - DetailedDescription: "agile", + id: "888", + updated_at: "", + created_at: "2024-11-15T10:49:06.237Z", + custom_attribute_values: {}, + item_data: { + name: "Newest", + product_type: "", + categories: [{ id: "999" }], + is_archived: false, + ecom_visibility: "VISIBLE", + description_html: "big animal", + variations: [ + { + id: "7777", + custom_attribute_values: { + image_arr: { + string_value: "foobar-0-9sjs9s", + }, + }, + item_variation_data: { + item_id: "888", + name: "", + price_money: { + amount: 900, + }, + quantity: "9", + }, + }, + ], }, - SellOnWeb: true, - IsArchived: false, }, ], }); @@ -410,12 +444,12 @@ describe("GIVEN ProductView", () => { level: 3, }); - expect(products[0]).toHaveTextContent("Cat"); - expect(products[1]).toHaveTextContent("66"); - expect(products[2]).toHaveTextContent("Elephant"); - expect(products[3]).toHaveTextContent("90"); + expect(products[0]).toHaveTextContent("Elephant"); + expect(products[1]).toHaveTextContent("0.90"); + expect(products[2]).toHaveTextContent("Newest"); + expect(products[3]).toHaveTextContent("9.00"); expect(products[4]).toHaveTextContent("Dog"); - expect(products[5]).toHaveTextContent("10"); + expect(products[5]).toHaveTextContent("0.10"); expect( (screen.getByRole("option", { name: "in stock" }) as HTMLOptionElement) @@ -438,12 +472,12 @@ describe("GIVEN ProductView", () => { const sortedProducts = screen.getAllByRole("heading", { level: 3 }); - expect(sortedProducts[0]).toHaveTextContent("Cat"); - expect(sortedProducts[1]).toHaveTextContent("66"); + expect(sortedProducts[0]).toHaveTextContent("Newest"); + expect(sortedProducts[1]).toHaveTextContent("9.00"); expect(sortedProducts[2]).toHaveTextContent("Dog"); - expect(sortedProducts[3]).toHaveTextContent("10"); + expect(sortedProducts[3]).toHaveTextContent("0.10"); expect(sortedProducts[4]).toHaveTextContent("Elephant"); - expect(sortedProducts[5]).toHaveTextContent("90"); + expect(sortedProducts[5]).toHaveTextContent("0.90"); }); it.skip("AND the products are already sorted THEN don't sort the products again", async () => { @@ -452,36 +486,68 @@ describe("GIVEN ProductView", () => { productArr: [ ...mockData, { - Id: 789, - Name: "Cat", - CategoryId: 999, - Description: "small animal", - SalePrice: 66, - ProductImages: [], - ProductTags: [], - VariantGroupId: null, - CurrentStock: 999, - ProductDetails: { - DetailedDescription: "agile", + id: "888", + updated_at: "", + created_at: "2024-11-15T10:49:06.237Z", + custom_attribute_values: {}, + item_data: { + name: "Cat", + product_type: "", + categories: [{ id: "999" }], + is_archived: false, + ecom_visibility: "VISIBLE", + description_html: "big animal", + variations: [ + { + id: "7777", + custom_attribute_values: { + image_arr: { + string_value: "foobar-0-9sjs9s", + }, + }, + item_variation_data: { + item_id: "888", + name: "", + price_money: { + amount: 900, + }, + quantity: "999", + }, + }, + ], }, - SellOnWeb: true, - IsArchived: false, }, { - Id: 790, - Name: "Fish", - CategoryId: 999, - Description: "scaley", - SalePrice: 99, - ProductImages: [], - ProductTags: [], - VariantGroupId: null, - CurrentStock: 1000, - ProductDetails: { - DetailedDescription: "lives in water", + id: "889", + updated_at: "", + created_at: "2024-11-15T10:49:06.237Z", + custom_attribute_values: {}, + item_data: { + name: "Fish", + product_type: "", + categories: [{ id: "999" }], + is_archived: false, + ecom_visibility: "VISIBLE", + description_html: "big animal", + variations: [ + { + id: "7777", + custom_attribute_values: { + image_arr: { + string_value: "foobar-0-9sjs9s", + }, + }, + item_variation_data: { + item_id: "888", + name: "", + price_money: { + amount: 9900, + }, + quantity: "1000", + }, + }, + ], }, - SellOnWeb: true, - IsArchived: false, }, ], }); diff --git a/src/lib/types/product.ts b/src/lib/types/product.ts index eeb114b..846858a 100644 --- a/src/lib/types/product.ts +++ b/src/lib/types/product.ts @@ -61,4 +61,5 @@ export interface SquareProduct { item_data: ItemData; custom_attribute_values: CustomItemAttributeVals; updated_at: string; + created_at: string; } diff --git a/src/lib/utils/requests.ts b/src/lib/utils/requests.ts index b57281a..75af7cb 100644 --- a/src/lib/utils/requests.ts +++ b/src/lib/utils/requests.ts @@ -47,15 +47,15 @@ const getCategoryFromServer: ( fW: (a: string) => Promise ) => GetFn> = (fetchWrapper: (a: string) => Promise) => - (url: string): Promise> => - fetchWrapper(url).then((res) => res.json()); + (url: string): Promise> => + fetchWrapper(url).then((res) => res.json()); const getProductArrayFromServer: ( fW: (a: string) => Promise ) => GetFn> = (fetchWrapper: (a: string) => Promise) => - (url: string): Promise> => - fetchWrapper(url).then((res) => res.json()); + (url: string): Promise> => + fetchWrapper(url).then((res) => res.json()); const getCategoriesArray: GetFn> = ( url: string @@ -66,5 +66,5 @@ const getCategoriesArrayServer: ( fW: (a: string) => Promise ) => GetFn> = (fetchWrapper: (a: string) => Promise) => - (url: string): Promise> => - fetchWrapper(url).then((res) => res.json()); + (url: string): Promise> => + fetchWrapper(url).then((res) => res.json());