From 625b159c34f92f5e1d96c5e94eb66ed6b64882ad Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 17 Jan 2024 09:00:59 +0000 Subject: [PATCH] fix: fix recentleViewedProducts reducer to sort descending This fixes recentlyViewedProducts reducer to sort the products by descending order instead of ascending. --- .../fetchRecentlyViewedProducts.test.ts.snap | 8 +-- .../reducer/__tests__/recentlyViewed.test.ts | 2 +- .../reducer/recentlyViewedProducts.ts | 8 +-- .../__tests__/recentlyViewed.test.ts | 24 +++------ .../products/recentlyViewed.fixtures.mts | 49 +++++++++++++------ 5 files changed, 50 insertions(+), 41 deletions(-) diff --git a/packages/redux/src/products/actions/__tests__/__snapshots__/fetchRecentlyViewedProducts.test.ts.snap b/packages/redux/src/products/actions/__tests__/__snapshots__/fetchRecentlyViewedProducts.test.ts.snap index 3f059390b..846cad024 100644 --- a/packages/redux/src/products/actions/__tests__/__snapshots__/fetchRecentlyViewedProducts.test.ts.snap +++ b/packages/redux/src/products/actions/__tests__/__snapshots__/fetchRecentlyViewedProducts.test.ts.snap @@ -4,14 +4,14 @@ exports[`fetchRecentlyViewedProducts() action creator should create the correct Object { "payload": Object { "entries": Array [ - Object { - "lastVisitDate": "2020-02-02T15:57:30.238Z", - "productId": 11111111, - }, Object { "lastVisitDate": "2020-02-01T15:57:30.238Z", "productId": 22222222, }, + Object { + "lastVisitDate": "2020-02-02T15:57:30.238Z", + "productId": 11111111, + }, ], "number": 1, "totalItems": 1, diff --git a/packages/redux/src/products/reducer/__tests__/recentlyViewed.test.ts b/packages/redux/src/products/reducer/__tests__/recentlyViewed.test.ts index 4aca32ba9..c1f76aa93 100644 --- a/packages/redux/src/products/reducer/__tests__/recentlyViewed.test.ts +++ b/packages/redux/src/products/reducer/__tests__/recentlyViewed.test.ts @@ -194,7 +194,7 @@ describe('Recently Viewed reducer', () => { result: { pagination: initialState.result?.pagination, remote: expectedRecentlyViewedRemotePayload, - computed: expectedRecentlyViewedLocalPayload, + computed: expectRecentlyViewedLocalPayloadSorted, }, }; const productId = 33333333; diff --git a/packages/redux/src/products/reducer/recentlyViewedProducts.ts b/packages/redux/src/products/reducer/recentlyViewedProducts.ts index 1e21ade0b..60e895ddb 100644 --- a/packages/redux/src/products/reducer/recentlyViewedProducts.ts +++ b/packages/redux/src/products/reducer/recentlyViewedProducts.ts @@ -1,7 +1,7 @@ import * as actionTypes from '../actionTypes/index.js'; import * as authenticationActionTypes from '../../users/authentication/actionTypes.js'; import { type AnyAction, combineReducers, type Reducer } from 'redux'; -import { omit, sortBy, uniqBy } from 'lodash-es'; +import { omit, orderBy, uniqBy } from 'lodash-es'; import type { RecentlyViewedProducts } from '@farfetch/blackout-client'; import type { RecentlyViewedState } from '../types/index.js'; @@ -55,9 +55,10 @@ const result = ( remote: action.payload, pagination: omit(action.payload, 'entries') as RecentlyViewedProducts, computed: uniqBy( - sortBy( + orderBy( [...action.payload.entries, ...computed], entry => new Date(entry.lastVisitDate), + 'desc', ), 'productId', ), @@ -71,9 +72,10 @@ const result = ( remote: state?.remote, pagination: state?.pagination, computed: uniqBy( - sortBy( + orderBy( [...action.payload, ...computed], entry => new Date(entry.lastVisitDate), + 'desc', ), 'productId', ), diff --git a/packages/redux/src/products/selectors/__tests__/recentlyViewed.test.ts b/packages/redux/src/products/selectors/__tests__/recentlyViewed.test.ts index 0858a5a7d..68c12f7cf 100644 --- a/packages/redux/src/products/selectors/__tests__/recentlyViewed.test.ts +++ b/packages/redux/src/products/selectors/__tests__/recentlyViewed.test.ts @@ -1,10 +1,10 @@ import * as recentlyViewedReducer from '../../reducer/recentlyViewedProducts.js'; import * as selectors from '../recentlyViewedProducts.js'; import { - expectedRecentlyViewedLocalPayload, expectedRecentlyViewedRemotePayload, + expectRecentlyViewedLocalPayloadSorted, } from 'tests/__fixtures__/products/index.mjs'; -import { omit, uniqBy } from 'lodash-es'; +import { omit } from 'lodash-es'; import type { StoreState } from '../../../types/index.js'; describe('RecentlyViewed redux selectors', () => { @@ -15,13 +15,7 @@ describe('RecentlyViewed redux selectors', () => { error: null, result: { remote: expectedRecentlyViewedRemotePayload, - computed: uniqBy( - [ - ...expectedRecentlyViewedLocalPayload, - ...expectedRecentlyViewedRemotePayload.entries, - ], - 'productId', - ), + computed: expectRecentlyViewedLocalPayloadSorted, pagination: omit(expectedRecentlyViewedRemotePayload, 'entries'), }, }, @@ -52,15 +46,9 @@ describe('RecentlyViewed redux selectors', () => { it('should get the result', () => { const spy = jest.spyOn(recentlyViewedReducer, 'getResult'); - expect(selectors.getRecentlyViewedProducts(mockState)).toEqual([ - ...expectedRecentlyViewedLocalPayload, - // mimic the lodash `uniqBy` method effect for the given payload - ...expectedRecentlyViewedRemotePayload.entries.filter( - item => - item.productId !== - expectedRecentlyViewedLocalPayload?.[0]?.productId, - ), - ]); + expect(selectors.getRecentlyViewedProducts(mockState)).toEqual( + expectRecentlyViewedLocalPayloadSorted, + ); expect(spy).toHaveBeenCalledTimes(1); }); diff --git a/tests/__fixtures__/products/recentlyViewed.fixtures.mts b/tests/__fixtures__/products/recentlyViewed.fixtures.mts index 46618d93f..39c8a9f87 100644 --- a/tests/__fixtures__/products/recentlyViewed.fixtures.mts +++ b/tests/__fixtures__/products/recentlyViewed.fixtures.mts @@ -1,7 +1,21 @@ -import { sortBy } from 'lodash-es'; - export const id = 1345678; export const expectedRecentlyViewedRemotePayload = { + number: 1, + totalPages: 1, + totalItems: 1, + entries: [ + { + productId: 22222222, + lastVisitDate: '2020-02-01T15:57:30.238Z', + }, + { + productId: 11111111, + lastVisitDate: '2020-02-02T15:57:30.238Z', + }, + ], +}; + +export const expectedRecentlyViewedRemotePayloadSorted = { number: 1, totalPages: 1, totalItems: 1, @@ -17,15 +31,11 @@ export const expectedRecentlyViewedRemotePayload = { ], }; -export const expectedRecentlyViewedRemotePayloadSorted = { - ...expectedRecentlyViewedRemotePayload, - entries: sortBy( - expectedRecentlyViewedRemotePayload.entries, - entry => new Date(entry.lastVisitDate), - ), -}; - export const expectedRecentlyViewedLocalPayload = [ + { + productId: 44444444, + lastVisitDate: '2020-02-03T10:08:50.010Z', + }, { productId: 22222222, lastVisitDate: '2020-02-03T11:08:50.010Z', @@ -34,13 +44,22 @@ export const expectedRecentlyViewedLocalPayload = [ productId: 33333333, lastVisitDate: '2020-02-03T12:08:50.010Z', }, - { productId: 44444444, lastVisitDate: '2020-02-03T10:08:50.010Z' }, ]; -export const expectRecentlyViewedLocalPayloadSorted = sortBy( - expectedRecentlyViewedLocalPayload, - entry => new Date(entry.lastVisitDate), -); +export const expectRecentlyViewedLocalPayloadSorted = [ + { + productId: 33333333, + lastVisitDate: '2020-02-03T12:08:50.010Z', + }, + { + productId: 22222222, + lastVisitDate: '2020-02-03T11:08:50.010Z', + }, + { + productId: 44444444, + lastVisitDate: '2020-02-03T10:08:50.010Z', + }, +]; export const mockRecentlyViewedState = { recentlyViewed: {