Skip to content

Commit

Permalink
Fix tests, add dedup test
Browse files Browse the repository at this point in the history
  • Loading branch information
orbiteleven committed Jun 9, 2022
1 parent d6e50e9 commit 207d674
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { OrderedMap } from 'immutable'

import { getPaginatedResultsReducer } from './paginated-results-reducer'
import {
GetPaginatedResultsReducer,
PaginatedResultsActionKind,
PaginatedResultsState,
ResultsType,
} from './types'

describe('paginatedResultsReducer', () => {
Expand All @@ -24,8 +27,8 @@ describe('paginatedResultsReducer', () => {
})

describe('reducer', () => {
let reducer: GetPaginatedResultsReducer<any, any, any>
let defaultState: PaginatedResultsState<any, any, any>
let reducer: GetPaginatedResultsReducer<unknown, unknown, unknown>
let defaultState: PaginatedResultsState<unknown, unknown, unknown>

beforeEach(() => {
const result = getPaginatedResultsReducer()
Expand All @@ -37,7 +40,7 @@ describe('paginatedResultsReducer', () => {
const state = reducer(
{
...defaultState,
results: [{ id: 1 }, { id: 2 }],
results: OrderedMap([{ id: 1 }, { id: 2 }]),
pagination: {
...defaultState.pagination,
cursor: { id: 1 },
Expand All @@ -47,7 +50,7 @@ describe('paginatedResultsReducer', () => {
{ type: PaginatedResultsActionKind.NEW_SEARCH }
)

expect(state.results).toStrictEqual([])
expect(state.results.toList().toArray()).toStrictEqual([])
expect(state.pagination.cursor).toBe(undefined)
expect(state.pagination.skip).toBe(0)
})
Expand All @@ -61,7 +64,7 @@ describe('paginatedResultsReducer', () => {

describe('new results', () => {
describe('hasMore', () => {
let hasMoreState: PaginatedResultsState<any, any, any>
let hasMoreState: PaginatedResultsState<unknown, unknown, unknown>
beforeEach(() => {
hasMoreState = {
...defaultState,
Expand Down Expand Up @@ -119,25 +122,54 @@ describe('paginatedResultsReducer', () => {
}
)

expect(state.results).toStrictEqual([{ id: 1 }])
expect(state.results.toList().toArray()).toStrictEqual([{ id: 1 }])
})

it('should append new results', () => {
const currentResults = [{ id: 1 }, { id: 2 }]
const newResults = [{ id: 3 }, { id: 4 }]
const state = reducer(
{ ...defaultState, results: currentResults },
{
...defaultState,
results: OrderedMap({
1: { id: 1 },
2: { id: 2 },
}) as ResultsType,
},
{
type: PaginatedResultsActionKind.NEW_RESULTS,
payload: { results: newResults },
}
)

expect(state.results).toStrictEqual([
expect(state.results.toList().toArray()).toStrictEqual([
...currentResults,
...newResults,
])
})

it('should dedupe entries', () => {
const newResults = [{ id: 2 }, { id: 3 }]
const state = reducer(
{
...defaultState,
results: OrderedMap({
1: { id: 1 },
2: { id: 2 },
}) as ResultsType,
},
{
type: PaginatedResultsActionKind.NEW_RESULTS,
payload: { results: newResults },
}
)

expect(state.results.toList().toArray()).toStrictEqual([
{ id: 1 },
{ id: 2 },
{ id: 3 },
])
})
})

describe('loadMore', () => {
Expand All @@ -154,9 +186,9 @@ describe('paginatedResultsReducer', () => {
})

it('should set cursor and skip on pagination', () => {
const currentResults = [{ id: 1 }, { id: 2 }]
const currentResults = OrderedMap({ 1: { id: 1 }, 2: { id: 2 } })
const state = reducer(
{ ...defaultState, results: currentResults },
{ ...defaultState, results: OrderedMap(currentResults) },
{
type: PaginatedResultsActionKind.LOAD_MORE,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ export const getPaginatedResultsReducer = <
const hasMore = results.length > state.pagination.take
const sliceEnd = hasMore ? -1 : undefined
const nextResultsPage = results.slice(0, sliceEnd)
const newResults = nextResultsPage.reduce(
(map, item) => map.set(item.id, item),
state.results
)
const newResults = nextResultsPage.reduce((map, item) => {
const newMap = map.set(item.id.toString(), item)
return newMap
}, state.results)
return {
...state,
hasMore,
Expand Down

0 comments on commit 207d674

Please sign in to comment.