Skip to content
Myr

redux.ts

This file

Path

__tests__/redux.ts

232 lines · 4.3 KiB · TypeScript

Last changed by

chore: remove dependency on spec.ts to fix build issue (#1104)Michel Weststratebefore adoption44363f7

History of this file

Take it

The walk stopped after 200 commits: lines older than 07f5cc9 point at it.

chore: remove dependency on spec.ts to fix build issue (#1104)Michel Weststratebefore adoption44363f7

line 1why, one step back
1import {assert, _} from "./spec_ts"

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 2why, one step back
2import {produce, Draft} from "../src/immer"

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 3–232why, one step back
3import * as redux from "redux"
45// Mutable Redux6{7 interface State {8 counter: number9 }1011 interface Action {12 type: string13 payload: number14 }1516 const initialState: State = {17 counter: 018 }1920 /// =============== Actions2122 const reduceCounterProducer = (state: State = initialState, action: Action) =>23 produce(state, draftState => {24 switch (action.type) {25 case "ADD_TO_COUNTER":26 draftState.counter += action.payload27 break28 case "SUB_FROM_COUNTER":29 draftState.counter -= action.payload30 break31 }32 })3334 const reduceCounterCurriedProducer = produce(35 (draftState: State, action: Action) => {36 switch (action.type) {37 case "ADD_TO_COUNTER":38 draftState.counter += action.payload39 break40 case "SUB_FROM_COUNTER":41 draftState.counter -= action.payload42 break43 }44 },45 initialState46 )4748 /// =============== Reducers4950 const reduce = redux.combineReducers({51 counterReducer: reduceCounterProducer52 })5354 const curredReduce = redux.combineReducers({55 counterReducer: reduceCounterCurriedProducer56 })5758 // reducing the current state to get the next state!59 // console.log(reduce(initialState, addToCounter(12));6061 // ================ store6263 const store = redux.createStore(reduce)64 const curriedStore = redux.createStore(curredReduce)6566 it("#470 works with Redux combine reducers", () => {67 assert(68 store.getState().counterReducer,69 _ as {70 counter: number71 }72 )73 assert(74 curriedStore.getState().counterReducer,75 _ as {76 counter: number77 }78 )79 })80}8182// Readonly Redux83{84 {85 interface State {86 readonly counter: number87 }8889 interface Action {90 readonly type: string91 readonly payload: number92 }9394 const initialState: State = {95 counter: 096 }9798 /// =============== Actions99100 const reduceCounterProducer = (101 state: State = initialState,102 action: Action103 ) =>104 produce(state, draftState => {105 switch (action.type) {106 case "ADD_TO_COUNTER":107 draftState.counter += action.payload108 break109 case "SUB_FROM_COUNTER":110 draftState.counter -= action.payload111 break112 }113 })114115 const reduceCounterCurriedProducer = produce(116 (draftState: Draft<State>, action: Action) => {117 switch (action.type) {118 case "ADD_TO_COUNTER":119 draftState.counter += action.payload120 break121 case "SUB_FROM_COUNTER":122 draftState.counter -= action.payload123 break124 }125 },126 initialState127 )128129 /// =============== Reducers130131 const reduce = redux.combineReducers({132 counterReducer: reduceCounterProducer133 })134135 const curredReduce = redux.combineReducers({136 counterReducer: reduceCounterCurriedProducer137 })138139 // reducing the current state to get the next state!140 // console.log(reduce(initialState, addToCounter(12));141142 // ================ store143144 const store = redux.createStore(reduce)145 const curriedStore = redux.createStore(curredReduce)146147 it("#470 works with Redux combine readonly reducers", () => {148 assert(149 store.getState().counterReducer,150 _ as {151 readonly counter: number152 }153 )154 assert(155 curriedStore.getState().counterReducer,156 _ as {157 readonly counter: number158 }159 )160 })161 }162}163164it("works with inferred curried reducer", () => {165 type State = {166 count: number167 }168169 type Action = {170 type: "inc"171 count: number172 }173174 const defaultState = {175 count: 3176 }177178 const store = redux.createStore(179 produce((state: State, action: Action) => {180 if (action.type === "inc") state.count += action.count181 // @ts-expect-error182 state.count2183 }, defaultState)184 )185186 assert(store.getState(), _ as State)187 store.dispatch({188 type: "inc",189 count: 2190 })191192 store.dispatch({193 // @ts-expect-error194 type: "inc2",195 count: 2196 })197})198199it("works with inferred curried reducer - readonly", () => {200 type State = {201 readonly count: number202 }203204 type Action = {205 readonly type: "inc"206 readonly count: number207 }208209 const defaultState: State = {210 count: 3211 }212213 const store = redux.createStore(214 produce((state: Draft<State>, action: Action) => {215 if (action.type === "inc") state.count += action.count216 // @ts-expect-error217 state.count2218 }, defaultState)219 )220221 assert(store.getState(), _ as State)222 store.dispatch({223 type: "inc",224 count: 2225 })226227 store.dispatch({228 // @ts-expect-error229 type: "inc2",230 count: 2231 })232})