Skip to content

Commit 82c0660

Browse files
authored
Merge pull request #2020 from samatar26/fix/duplicate-ids-in-create-entity-adapter
2 parents 7f76635 + e86ed60 commit 82c0660

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
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/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', () => {

packages/toolkit/src/entities/unsorted_state_adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export function createUnsortedStateAdapter<T>(
158158
0
159159

160160
if (didMutateIds) {
161-
state.ids = state.ids.map((id) => newKeys[id] || id)
161+
state.ids = Object.keys(state.entities)
162162
}
163163
}
164164
}

0 commit comments

Comments
 (0)