Skip to content

Commit 8b8602c

Browse files
Bruno Oliveiraboliveira
authored andcommitted
fix: fix recentleViewedProducts reducer to sort descending
This fixes recentlyViewedProducts reducer to sort the products by descending order instead of ascending.
1 parent 98818d6 commit 8b8602c

5 files changed

Lines changed: 50 additions & 41 deletions

File tree

packages/redux/src/products/actions/__tests__/__snapshots__/fetchRecentlyViewedProducts.test.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ exports[`fetchRecentlyViewedProducts() action creator should create the correct
44
Object {
55
"payload": Object {
66
"entries": Array [
7-
Object {
8-
"lastVisitDate": "2020-02-02T15:57:30.238Z",
9-
"productId": 11111111,
10-
},
117
Object {
128
"lastVisitDate": "2020-02-01T15:57:30.238Z",
139
"productId": 22222222,
1410
},
11+
Object {
12+
"lastVisitDate": "2020-02-02T15:57:30.238Z",
13+
"productId": 11111111,
14+
},
1515
],
1616
"number": 1,
1717
"totalItems": 1,

packages/redux/src/products/reducer/__tests__/recentlyViewed.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ describe('Recently Viewed reducer', () => {
194194
result: {
195195
pagination: initialState.result?.pagination,
196196
remote: expectedRecentlyViewedRemotePayload,
197-
computed: expectedRecentlyViewedLocalPayload,
197+
computed: expectRecentlyViewedLocalPayloadSorted,
198198
},
199199
};
200200
const productId = 33333333;

packages/redux/src/products/reducer/recentlyViewedProducts.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as actionTypes from '../actionTypes/index.js';
22
import * as authenticationActionTypes from '../../users/authentication/actionTypes.js';
33
import { type AnyAction, combineReducers, type Reducer } from 'redux';
4-
import { omit, sortBy, uniqBy } from 'lodash-es';
4+
import { omit, orderBy, uniqBy } from 'lodash-es';
55
import type { RecentlyViewedProducts } from '@farfetch/blackout-client';
66
import type { RecentlyViewedState } from '../types/index.js';
77

@@ -55,9 +55,10 @@ const result = (
5555
remote: action.payload,
5656
pagination: omit(action.payload, 'entries') as RecentlyViewedProducts,
5757
computed: uniqBy(
58-
sortBy(
58+
orderBy(
5959
[...action.payload.entries, ...computed],
6060
entry => new Date(entry.lastVisitDate),
61+
'desc',
6162
),
6263
'productId',
6364
),
@@ -71,9 +72,10 @@ const result = (
7172
remote: state?.remote,
7273
pagination: state?.pagination,
7374
computed: uniqBy(
74-
sortBy(
75+
orderBy(
7576
[...action.payload, ...computed],
7677
entry => new Date(entry.lastVisitDate),
78+
'desc',
7779
),
7880
'productId',
7981
),

packages/redux/src/products/selectors/__tests__/recentlyViewed.test.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as recentlyViewedReducer from '../../reducer/recentlyViewedProducts.js';
22
import * as selectors from '../recentlyViewedProducts.js';
33
import {
4-
expectedRecentlyViewedLocalPayload,
54
expectedRecentlyViewedRemotePayload,
5+
expectRecentlyViewedLocalPayloadSorted,
66
} from 'tests/__fixtures__/products/index.mjs';
7-
import { omit, uniqBy } from 'lodash-es';
7+
import { omit } from 'lodash-es';
88
import type { StoreState } from '../../../types/index.js';
99

1010
describe('RecentlyViewed redux selectors', () => {
@@ -15,13 +15,7 @@ describe('RecentlyViewed redux selectors', () => {
1515
error: null,
1616
result: {
1717
remote: expectedRecentlyViewedRemotePayload,
18-
computed: uniqBy(
19-
[
20-
...expectedRecentlyViewedLocalPayload,
21-
...expectedRecentlyViewedRemotePayload.entries,
22-
],
23-
'productId',
24-
),
18+
computed: expectRecentlyViewedLocalPayloadSorted,
2519
pagination: omit(expectedRecentlyViewedRemotePayload, 'entries'),
2620
},
2721
},
@@ -52,15 +46,9 @@ describe('RecentlyViewed redux selectors', () => {
5246
it('should get the result', () => {
5347
const spy = jest.spyOn(recentlyViewedReducer, 'getResult');
5448

55-
expect(selectors.getRecentlyViewedProducts(mockState)).toEqual([
56-
...expectedRecentlyViewedLocalPayload,
57-
// mimic the lodash `uniqBy` method effect for the given payload
58-
...expectedRecentlyViewedRemotePayload.entries.filter(
59-
item =>
60-
item.productId !==
61-
expectedRecentlyViewedLocalPayload?.[0]?.productId,
62-
),
63-
]);
49+
expect(selectors.getRecentlyViewedProducts(mockState)).toEqual(
50+
expectRecentlyViewedLocalPayloadSorted,
51+
);
6452
expect(spy).toHaveBeenCalledTimes(1);
6553
});
6654

tests/__fixtures__/products/recentlyViewed.fixtures.mts

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1-
import { sortBy } from 'lodash-es';
2-
31
export const id = 1345678;
42
export const expectedRecentlyViewedRemotePayload = {
3+
number: 1,
4+
totalPages: 1,
5+
totalItems: 1,
6+
entries: [
7+
{
8+
productId: 22222222,
9+
lastVisitDate: '2020-02-01T15:57:30.238Z',
10+
},
11+
{
12+
productId: 11111111,
13+
lastVisitDate: '2020-02-02T15:57:30.238Z',
14+
},
15+
],
16+
};
17+
18+
export const expectedRecentlyViewedRemotePayloadSorted = {
519
number: 1,
620
totalPages: 1,
721
totalItems: 1,
@@ -17,15 +31,11 @@ export const expectedRecentlyViewedRemotePayload = {
1731
],
1832
};
1933

20-
export const expectedRecentlyViewedRemotePayloadSorted = {
21-
...expectedRecentlyViewedRemotePayload,
22-
entries: sortBy(
23-
expectedRecentlyViewedRemotePayload.entries,
24-
entry => new Date(entry.lastVisitDate),
25-
),
26-
};
27-
2834
export const expectedRecentlyViewedLocalPayload = [
35+
{
36+
productId: 44444444,
37+
lastVisitDate: '2020-02-03T10:08:50.010Z',
38+
},
2939
{
3040
productId: 22222222,
3141
lastVisitDate: '2020-02-03T11:08:50.010Z',
@@ -34,13 +44,22 @@ export const expectedRecentlyViewedLocalPayload = [
3444
productId: 33333333,
3545
lastVisitDate: '2020-02-03T12:08:50.010Z',
3646
},
37-
{ productId: 44444444, lastVisitDate: '2020-02-03T10:08:50.010Z' },
3847
];
3948

40-
export const expectRecentlyViewedLocalPayloadSorted = sortBy(
41-
expectedRecentlyViewedLocalPayload,
42-
entry => new Date(entry.lastVisitDate),
43-
);
49+
export const expectRecentlyViewedLocalPayloadSorted = [
50+
{
51+
productId: 33333333,
52+
lastVisitDate: '2020-02-03T12:08:50.010Z',
53+
},
54+
{
55+
productId: 22222222,
56+
lastVisitDate: '2020-02-03T11:08:50.010Z',
57+
},
58+
{
59+
productId: 44444444,
60+
lastVisitDate: '2020-02-03T10:08:50.010Z',
61+
},
62+
];
4463

4564
export const mockRecentlyViewedState = {
4665
recentlyViewed: {

0 commit comments

Comments
 (0)