Skip to content

Commit e86ed60

Browse files
committed
Update ID change tests for both sorted and unsorted adapters
1 parent 6640979 commit e86ed60

File tree

3 files changed

+36
-40
lines changed

3 files changed

+36
-40
lines changed

packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { EntityStateAdapter, EntityState } from '../models'
1+
import type { EntityAdapter, EntityState } from '../models'
22
import { createEntityAdapter } from '../create_adapter'
3-
import { createAction } from '../../createAction'
3+
import { createAction, createSlice, configureStore } from '@reduxjs/toolkit'
44
import type { BookModel } from './fixtures/book'
55
import {
66
TheGreatGatsby,
@@ -11,7 +11,7 @@ import {
1111
import { createNextState } from '../..'
1212

1313
describe('Sorted State Adapter', () => {
14-
let adapter: EntityStateAdapter<BookModel>
14+
let adapter: EntityAdapter<BookModel>
1515
let state: EntityState<BookModel>
1616

1717
beforeAll(() => {
@@ -568,6 +568,22 @@ describe('Sorted State Adapter', () => {
568568
})
569569
})
570570

571+
it("only returns one entry for that id in the id's array", () => {
572+
const book1: BookModel = { id: 'a', title: 'First' }
573+
const book2: BookModel = { id: 'b', title: 'Second' }
574+
const initialState = adapter.getInitialState()
575+
const withItems = adapter.addMany(initialState, [book1, book2])
576+
577+
expect(withItems.ids).toEqual(['a', 'b'])
578+
const withUpdate = adapter.updateOne(withItems, {
579+
id: 'a',
580+
changes: { id: 'b' },
581+
})
582+
583+
expect(withUpdate.ids).toEqual(['b'])
584+
expect(withUpdate.entities['b']!.title).toBe(book1.title)
585+
})
586+
571587
describe('can be used mutably when wrapped in createNextState', () => {
572588
test('removeAll', () => {
573589
const withTwo = adapter.addMany(state, [TheGreatGatsby, AnimalFarm])

packages/toolkit/src/entities/tests/state_adapter.test.ts

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -58,39 +58,4 @@ describe('createStateOperator', () => {
5858
expect(state2.books.ids.length).toBe(2)
5959
expect(state2.books.entities['b']).toBe(book2)
6060
})
61-
62-
describe("when an id is updated to an existing id's value", () => {
63-
it("only returns one entry for that id in the id's array", () => {
64-
const booksSlice = createSlice({
65-
name: 'books',
66-
initialState: adapter.getInitialState(),
67-
reducers: {
68-
addOne: adapter.addOne,
69-
updateOne: adapter.updateOne,
70-
},
71-
})
72-
73-
const { addOne, updateOne } = booksSlice.actions
74-
75-
const store = configureStore({
76-
reducer: {
77-
books: booksSlice.reducer,
78-
},
79-
})
80-
81-
const book1: BookModel = { id: 'a', title: 'First' }
82-
const book2: BookModel = { id: 'b', title: 'Second' }
83-
store.dispatch(addOne(book1))
84-
store.dispatch(addOne(book2))
85-
86-
const state = store.getState()
87-
expect(state.books.ids).toEqual(['a', 'b'])
88-
89-
store.dispatch(updateOne({ id: 'a', changes: { id: 'b' } }))
90-
91-
const updatedState = store.getState()
92-
expect(updatedState.books.ids).toEqual(['b'])
93-
expect(updatedState.books.entities['b'].title).toBe(book1.title)
94-
})
95-
})
9661
})

packages/toolkit/src/entities/tests/unsorted_state_adapter.test.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { EntityStateAdapter, EntityState } from '../models'
1+
import type { EntityAdapter, EntityState } from '../models'
22
import { createEntityAdapter } from '../create_adapter'
33
import type { BookModel } from './fixtures/book'
44
import {
@@ -10,7 +10,7 @@ import {
1010
import { createNextState } from '../..'
1111

1212
describe('Unsorted State Adapter', () => {
13-
let adapter: EntityStateAdapter<BookModel>
13+
let adapter: EntityAdapter<BookModel>
1414
let state: EntityState<BookModel>
1515

1616
beforeAll(() => {
@@ -413,6 +413,21 @@ describe('Unsorted State Adapter', () => {
413413
},
414414
})
415415
})
416+
it("only returns one entry for that id in the id's array", () => {
417+
const book1: BookModel = { id: 'a', title: 'First' }
418+
const book2: BookModel = { id: 'b', title: 'Second' }
419+
const initialState = adapter.getInitialState()
420+
const withItems = adapter.addMany(initialState, [book1, book2])
421+
422+
expect(withItems.ids).toEqual(['a', 'b'])
423+
const withUpdate = adapter.updateOne(withItems, {
424+
id: 'a',
425+
changes: { id: 'b' },
426+
})
427+
428+
expect(withUpdate.ids).toEqual(['b'])
429+
expect(withUpdate.entities['b']!.title).toBe(book1.title)
430+
})
416431

417432
describe('can be used mutably when wrapped in createNextState', () => {
418433
test('removeAll', () => {

0 commit comments

Comments
 (0)