Skip to content

Commit

Permalink
fix: fix recentleViewedProducts reducer to sort descending
Browse files Browse the repository at this point in the history
This fixes recentlyViewedProducts reducer to sort the products
by descending order instead of ascending.
  • Loading branch information
Bruno Oliveira committed Jan 17, 2024
1 parent 98818d6 commit 625b159
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ describe('Recently Viewed reducer', () => {
result: {
pagination: initialState.result?.pagination,
remote: expectedRecentlyViewedRemotePayload,
computed: expectedRecentlyViewedLocalPayload,
computed: expectRecentlyViewedLocalPayloadSorted,
},
};
const productId = 33333333;
Expand Down
8 changes: 5 additions & 3 deletions packages/redux/src/products/reducer/recentlyViewedProducts.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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',
),
Expand All @@ -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',
),
Expand Down
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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'),
},
},
Expand Down Expand Up @@ -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);
});

Expand Down
49 changes: 34 additions & 15 deletions tests/__fixtures__/products/recentlyViewed.fixtures.mts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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',
Expand All @@ -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: {
Expand Down

0 comments on commit 625b159

Please sign in to comment.