From f75d11f260a9850c3659ab698fa8f6b8fb8d8e17 Mon Sep 17 00:00:00 2001 From: CD Cabrera Date: Mon, 28 Nov 2022 16:15:14 -0500 Subject: [PATCH] fix(inventoryCard): sw-703 reset columns, rows (#1010) * inventoryCard, allow empty cols, rows * inventoryCardContext, SubsContext, consistent response --- .../inventoryCardContext.test.js.snap | 60 ++++++++++++++----- src/components/inventoryCard/inventoryCard.js | 16 ++--- .../inventoryCard/inventoryCardContext.js | 10 ++-- ...ntoryCardSubscriptionsContext.test.js.snap | 30 +++++++--- .../inventoryCardSubscriptionsContext.js | 5 +- tests/__snapshots__/code.test.js.snap | 6 +- 6 files changed, 84 insertions(+), 43 deletions(-) diff --git a/src/components/inventoryCard/__tests__/__snapshots__/inventoryCardContext.test.js.snap b/src/components/inventoryCard/__tests__/__snapshots__/inventoryCardContext.test.js.snap index d7f16c638..30cb4656b 100644 --- a/src/components/inventoryCard/__tests__/__snapshots__/inventoryCardContext.test.js.snap +++ b/src/components/inventoryCard/__tests__/__snapshots__/inventoryCardContext.test.js.snap @@ -32,7 +32,29 @@ exports[`InventoryCardContext should handle a store response with useGetHostsInv ], "error": false, "fulfilled": true, + "message": null, "pending": false, + "responses": { + "id": {}, + "list": [ + { + "data": [ + { + "data": [ + { + "lorem": "ipsum", + }, + { + "dolor": "sit", + }, + ], + "meta": {}, + }, + ], + "fulfilled": true, + }, + ], + }, } `; @@ -53,7 +75,29 @@ exports[`InventoryCardContext should handle a store response with useGetInstance ], "error": false, "fulfilled": true, + "message": null, "pending": false, + "responses": { + "id": {}, + "list": [ + { + "data": [ + { + "data": [ + { + "lorem": "ipsum", + }, + { + "dolor": "sit", + }, + ], + "meta": {}, + }, + ], + "fulfilled": true, + }, + ], + }, } `; @@ -145,8 +189,6 @@ exports[`InventoryCardContext should handle an onPage event: onPage event, dispa exports[`InventoryCardContext should handle variations in hosts inventory API responses: inventory, cancelled 1`] = ` { "data": {}, - "error": undefined, - "fulfilled": undefined, "pending": true, } `; @@ -154,8 +196,6 @@ exports[`InventoryCardContext should handle variations in hosts inventory API re exports[`InventoryCardContext should handle variations in hosts inventory API responses: inventory, disabled 1`] = ` { "data": {}, - "error": undefined, - "fulfilled": undefined, "pending": false, } `; @@ -164,7 +204,6 @@ exports[`InventoryCardContext should handle variations in hosts inventory API re { "data": {}, "error": true, - "fulfilled": undefined, "pending": false, } `; @@ -172,7 +211,6 @@ exports[`InventoryCardContext should handle variations in hosts inventory API re exports[`InventoryCardContext should handle variations in hosts inventory API responses: inventory, fulfilled 1`] = ` { "data": {}, - "error": undefined, "fulfilled": true, "pending": false, } @@ -181,8 +219,6 @@ exports[`InventoryCardContext should handle variations in hosts inventory API re exports[`InventoryCardContext should handle variations in hosts inventory API responses: inventory, pending 1`] = ` { "data": {}, - "error": undefined, - "fulfilled": undefined, "pending": true, } `; @@ -190,8 +226,6 @@ exports[`InventoryCardContext should handle variations in hosts inventory API re exports[`InventoryCardContext should handle variations in instances inventory API responses: inventory, cancelled 1`] = ` { "data": {}, - "error": undefined, - "fulfilled": undefined, "pending": true, } `; @@ -199,8 +233,6 @@ exports[`InventoryCardContext should handle variations in instances inventory AP exports[`InventoryCardContext should handle variations in instances inventory API responses: inventory, disabled 1`] = ` { "data": {}, - "error": undefined, - "fulfilled": undefined, "pending": false, } `; @@ -209,7 +241,6 @@ exports[`InventoryCardContext should handle variations in instances inventory AP { "data": {}, "error": true, - "fulfilled": undefined, "pending": false, } `; @@ -217,7 +248,6 @@ exports[`InventoryCardContext should handle variations in instances inventory AP exports[`InventoryCardContext should handle variations in instances inventory API responses: inventory, fulfilled 1`] = ` { "data": {}, - "error": undefined, "fulfilled": true, "pending": false, } @@ -226,8 +256,6 @@ exports[`InventoryCardContext should handle variations in instances inventory AP exports[`InventoryCardContext should handle variations in instances inventory API responses: inventory, pending 1`] = ` { "data": {}, - "error": undefined, - "fulfilled": undefined, "pending": true, } `; diff --git a/src/components/inventoryCard/inventoryCard.js b/src/components/inventoryCard/inventoryCard.js index 371a5ce32..ad19b3b26 100644 --- a/src/components/inventoryCard/inventoryCard.js +++ b/src/components/inventoryCard/inventoryCard.js @@ -72,9 +72,11 @@ const InventoryCard = ({ const { data: listData = [], meta = {} } = data; useDeepCompareEffect(() => { + let updatedColumnHeaders = []; + let updatedRows = []; + if (fulfilled && listData.length) { - let updatedColumnHeaders = []; - const updatedRows = listData.map(({ ...cellData }) => { + updatedRows = listData.map(({ ...cellData }) => { const { columnHeaders, cells } = inventoryCardHelpers.parseRowCellsListData({ filters: inventoryCardHelpers.parseInventoryFilters({ filters: filterInventoryData, @@ -112,12 +114,12 @@ const InventoryCard = ({ undefined }; }); - - setUpdatedColumnsRows(() => ({ - columnHeaders: updatedColumnHeaders, - rows: updatedRows - })); } + + setUpdatedColumnsRows(() => ({ + columnHeaders: updatedColumnHeaders, + rows: updatedRows + })); }, [filterInventoryData, fulfilled, listData]); if (isDisabled) { diff --git a/src/components/inventoryCard/inventoryCardContext.js b/src/components/inventoryCard/inventoryCardContext.js index d43436656..7e0ea4e62 100644 --- a/src/components/inventoryCard/inventoryCardContext.js +++ b/src/components/inventoryCard/inventoryCardContext.js @@ -34,7 +34,7 @@ const useGetHostsInventory = ({ const { productId } = useAliasProduct(); const query = useAliasProductInventoryQuery(); const dispatch = useAliasDispatch(); - const { error, cancelled, fulfilled, pending, data } = useAliasSelectorsResponse( + const { cancelled, pending, data, ...response } = useAliasSelectorsResponse( ({ inventory }) => inventory?.hostsInventory?.[productId] ); @@ -45,8 +45,7 @@ const useGetHostsInventory = ({ }, [dispatch, isDisabled, productId, query]); return { - error, - fulfilled, + ...response, pending: pending || cancelled || false, data: (data?.length === 1 && data[0]) || data || {} }; @@ -75,7 +74,7 @@ const useGetInstancesInventory = ({ const { productId } = useAliasProduct(); const query = useAliasProductInventoryQuery(); const dispatch = useAliasDispatch(); - const { error, cancelled, fulfilled, pending, data } = useAliasSelectorsResponse( + const { cancelled, pending, data, ...response } = useAliasSelectorsResponse( ({ inventory }) => inventory?.instancesInventory?.[productId] ); @@ -86,8 +85,7 @@ const useGetInstancesInventory = ({ }, [dispatch, isDisabled, productId, query]); return { - error, - fulfilled, + ...response, pending: pending || cancelled || false, data: (data?.length === 1 && data[0]) || data || {} }; diff --git a/src/components/inventoryCardSubscriptions/__tests__/__snapshots__/inventoryCardSubscriptionsContext.test.js.snap b/src/components/inventoryCardSubscriptions/__tests__/__snapshots__/inventoryCardSubscriptionsContext.test.js.snap index 4b9ee3773..c3d364828 100644 --- a/src/components/inventoryCardSubscriptions/__tests__/__snapshots__/inventoryCardSubscriptionsContext.test.js.snap +++ b/src/components/inventoryCardSubscriptions/__tests__/__snapshots__/inventoryCardSubscriptionsContext.test.js.snap @@ -30,7 +30,29 @@ exports[`InventoryCardSubscriptionsContext should handle a store response with u ], "error": false, "fulfilled": true, + "message": null, "pending": false, + "responses": { + "id": {}, + "list": [ + { + "data": [ + { + "data": [ + { + "lorem": "ipsum", + }, + { + "dolor": "sit", + }, + ], + "meta": {}, + }, + ], + "fulfilled": true, + }, + ], + }, } `; @@ -89,8 +111,6 @@ exports[`InventoryCardSubscriptionsContext should handle an onPage event: onPage exports[`InventoryCardSubscriptionsContext should handle variations in instances inventory API responses: inventory, cancelled 1`] = ` { "data": {}, - "error": undefined, - "fulfilled": undefined, "pending": true, } `; @@ -98,8 +118,6 @@ exports[`InventoryCardSubscriptionsContext should handle variations in instances exports[`InventoryCardSubscriptionsContext should handle variations in instances inventory API responses: inventory, disabled 1`] = ` { "data": {}, - "error": undefined, - "fulfilled": undefined, "pending": false, } `; @@ -108,7 +126,6 @@ exports[`InventoryCardSubscriptionsContext should handle variations in instances { "data": {}, "error": true, - "fulfilled": undefined, "pending": false, } `; @@ -116,7 +133,6 @@ exports[`InventoryCardSubscriptionsContext should handle variations in instances exports[`InventoryCardSubscriptionsContext should handle variations in instances inventory API responses: inventory, fulfilled 1`] = ` { "data": {}, - "error": undefined, "fulfilled": true, "pending": false, } @@ -125,8 +141,6 @@ exports[`InventoryCardSubscriptionsContext should handle variations in instances exports[`InventoryCardSubscriptionsContext should handle variations in instances inventory API responses: inventory, pending 1`] = ` { "data": {}, - "error": undefined, - "fulfilled": undefined, "pending": true, } `; diff --git a/src/components/inventoryCardSubscriptions/inventoryCardSubscriptionsContext.js b/src/components/inventoryCardSubscriptions/inventoryCardSubscriptionsContext.js index d8a1888b8..dd28e33d0 100644 --- a/src/components/inventoryCardSubscriptions/inventoryCardSubscriptionsContext.js +++ b/src/components/inventoryCardSubscriptions/inventoryCardSubscriptionsContext.js @@ -33,7 +33,7 @@ const useGetSubscriptionsInventory = ({ const { productId } = useAliasProduct(); const query = useAliasProductInventoryQuery(); const dispatch = useAliasDispatch(); - const { error, cancelled, fulfilled, pending, data } = useAliasSelectorsResponse( + const { cancelled, pending, data, ...response } = useAliasSelectorsResponse( ({ inventory }) => inventory?.subscriptionsInventory?.[productId] ); @@ -44,8 +44,7 @@ const useGetSubscriptionsInventory = ({ }, [dispatch, isDisabled, productId, query]); return { - error, - fulfilled, + ...response, pending: pending || cancelled || false, data: (data?.length === 1 && data[0]) || data || {} }; diff --git a/tests/__snapshots__/code.test.js.snap b/tests/__snapshots__/code.test.js.snap index da653fc1b..3f4a1bb3a 100644 --- a/tests/__snapshots__/code.test.js.snap +++ b/tests/__snapshots__/code.test.js.snap @@ -2,11 +2,11 @@ exports[`General code checks should only have specific console.[warn|log|info|error] methods: console methods 1`] = ` [ - "components/inventoryCard/inventoryCardContext.js:169: console.warn(\`Sorting can only be performed on select fields, confirm field \${id} is allowed.\`);", - "components/inventoryCard/inventoryCardContext.js:231: console.warn(\`Sorting can only be performed on select fields, confirm field \${id} is allowed.\`);", + "components/inventoryCard/inventoryCardContext.js:167: console.warn(\`Sorting can only be performed on select fields, confirm field \${id} is allowed.\`);", + "components/inventoryCard/inventoryCardContext.js:229: console.warn(\`Sorting can only be performed on select fields, confirm field \${id} is allowed.\`);", "components/inventoryCard/inventoryCardHelpers.js:87: console.warn(\`Warning: Filter "\${id}" not found in "table row" response data.\`, cellData);", "components/inventoryCard/inventoryList.deprecated.js:62: console.warn(\`Sorting can only be performed on select fields, confirm field \${id} is allowed.\`);", - "components/inventoryCardSubscriptions/inventoryCardSubscriptionsContext.js:127: console.warn(\`Sorting can only be performed on select fields, confirm field \${id} is allowed.\`);", + "components/inventoryCardSubscriptions/inventoryCardSubscriptionsContext.js:126: console.warn(\`Sorting can only be performed on select fields, confirm field \${id} is allowed.\`);", "index.js:12: console.log(\`Emulated appNavClick: \${JSON.stringify({ id, ...rest })}\`);", "redux/common/reduxHelpers.js:282: console.error(\`Error: Property \${prop} does not exist within the passed state.\`, state);", "redux/common/reduxHelpers.js:286: console.warn(\`Warning: Property \${prop} does not exist within the passed initialState.\`, initialState);",