Skip to content

Commit

Permalink
Update Immer to 8.x (#821)
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Nov 27, 2020
1 parent a1ee48d commit 10ade92
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/tutorials/basic-tutorial.md
Expand Up @@ -9,7 +9,7 @@ hide_title: true

Welcome to Redux Toolkit ! This tutorial will show you the basic functions that are included with Redux Toolkit (also known as "RTK" for short).

This tutorial assumes that you are already familiar with the concepts of the core Redux library, as well as how to use it with React. If you aren't, please take some time to read through the [Redux docs](https://redux.js.org) and [React-Redux docs](https://react-redux.js.org) first, as the explanations here focus on how RTK usage differs from "typical" Redux code.
This tutorial assumes that you are already familiar with the concepts of the core [Redux](https://redux.js.org) library, as well as how to use it with [React](https://reactjs.org). If you aren't, please take some time to read through the [Redux docs](https://redux.js.org) and [React-Redux docs](https://react-redux.js.org) first, as the explanations here focus on how RTK usage differs from "typical" Redux code.

## Introduction: Writing a Counter Application

Expand Down
3 changes: 3 additions & 0 deletions etc/redux-toolkit.api.md
Expand Up @@ -13,6 +13,7 @@ import { current } from 'immer';
import { DeepPartial } from 'redux';
import { Dispatch } from 'redux';
import { Draft } from 'immer';
import { freeze } from 'immer';
import { Middleware } from 'redux';
import { OutputParametricSelector } from 'reselect';
import { OutputSelector } from 'reselect';
Expand Down Expand Up @@ -271,6 +272,8 @@ export interface EntityStateAdapter<T> {
// @public (undocumented)
export function findNonSerializableValue(value: unknown, path?: ReadonlyArray<string>, isSerializable?: (value: unknown) => boolean, getEntries?: (value: unknown) => [string, any][], ignoredPaths?: string[]): NonSerializableValue | false;

export { freeze }

// @public
export function getDefaultMiddleware<S = any, O extends Partial<GetDefaultMiddlewareOptions> = {
thunk: true;
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -61,7 +61,7 @@
"src"
],
"dependencies": {
"immer": "^7.0.3",
"immer": "^8.0.0",
"redux": "^4.0.0",
"redux-thunk": "^2.3.0",
"reselect": "^4.0.0"
Expand Down
42 changes: 42 additions & 0 deletions src/createReducer.test.ts
Expand Up @@ -50,6 +50,48 @@ describe('createReducer', () => {
behavesLikeReducer(todosReducer)
})

describe('Immer in a production environment', () => {
let originalNodeEnv = process.env.NODE_ENV

beforeEach(() => {
jest.resetModules()
process.env.NODE_ENV = 'production'
})

afterEach(() => {
process.env.NODE_ENV = originalNodeEnv
})

test('Freezes data in production', () => {
const { createReducer } = require('./createReducer')
const addTodo: AddTodoReducer = (state, action) => {
const { newTodo } = action.payload
state.push({ ...newTodo, completed: false })
}

const toggleTodo: ToggleTodoReducer = (state, action) => {
const { index } = action.payload
const todo = state[index]
todo.completed = !todo.completed
}

const todosReducer = createReducer([] as TodoState, {
ADD_TODO: addTodo,
TOGGLE_TODO: toggleTodo
})

const result = todosReducer([], {
type: 'ADD_TODO',
payload: { text: 'Buy milk' }
})

const mutateStateOutsideReducer = () => (result[0].text = 'edited')
expect(mutateStateOutsideReducer).toThrowError(
'Cannot add property text, object is not extensible'
)
})
})

describe('given pure reducers with immutable updates', () => {
const addTodo: AddTodoReducer = (state, action) => {
const { newTodo } = action.payload
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
@@ -1,6 +1,6 @@
import { enableES5 } from 'immer'
export * from 'redux'
export { default as createNextState, Draft, current } from 'immer'
export { default as createNextState, Draft, current, freeze } from 'immer'
export {
createSelector,
Selector,
Expand Down

0 comments on commit 10ade92

Please sign in to comment.