Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missed spot where use of current may fail if the value is not a draft #4412

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"format": "prettier --write \"(src|examples)/**/*.{ts,tsx}\" \"**/*.md\"",
"format:check": "prettier --list-different \"(src|examples)/**/*.{ts,tsx}\" \"docs/*/**.md\"",
"lint": "eslint src examples",
"test": "vitest --run --typecheck",
"test": "vitest --typecheck --run ",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiousity, what's the difference?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argument ordering. If I do yarn test sorted_state_adapter, I want that filename to get passed through as a modifier to --run so that it filters files. If you do it with --typecheck last, it ignores it, and runs all test files instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I see, seems like it's not an issue anymore in the latest version of vitest.

"test:watch": "vitest --watch",
"type-tests": "yarn tsc -p tsconfig.test.json --noEmit",
"prepack": "yarn build",
Expand Down
25 changes: 25 additions & 0 deletions packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createSlice,
configureStore,
nanoid,
PayloadAction,
} from '@reduxjs/toolkit'
import type { BookModel } from './fixtures/book'
import {
Expand Down Expand Up @@ -783,6 +784,30 @@ describe('Sorted State Adapter', () => {
//expect(numSorts).toBeLessThan(25_000)
})

it('should not throw an Immer `current` error when `state.ids` is a plain array', () => {
const book1: BookModel = { id: 'a', title: 'First' }
const initialState = adapter.getInitialState()
const withItems = adapter.addMany(initialState, [book1])
const booksSlice = createSlice({
name: 'books',
initialState,
reducers: {
testCurrentBehavior(state, action: PayloadAction<BookModel>) {
// Will overwrite `state.ids` with a plain array
adapter.removeAll(state)

// will call `splitAddedUpdatedEntities` and call `current(state.ids)`
adapter.upsertMany(state, [book1])
},
},
})

booksSlice.reducer(
initialState,
booksSlice.actions.testCurrentBehavior(book1),
)
})

describe('can be used mutably when wrapped in createNextState', () => {
test('removeAll', () => {
const withTwo = adapter.addMany(state, [TheGreatGatsby, AnimalFarm])
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/entities/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function splitAddedUpdatedEntities<T, Id extends EntityId>(
): [T[], Update<T, Id>[], Id[]] {
newEntities = ensureEntitiesArray(newEntities)

const existingIdsArray = current(state.ids) as Id[]
const existingIdsArray = getCurrent(state.ids) as Id[]
const existingIds = new Set<Id>(existingIdsArray)

const added: T[] = []
Expand Down