recursive.js
This file
Path
__tests__/recursive.js
187 lines · 5.8 KiB · JavaScript
Last changed by
Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b
History of this fileTake it
Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b
lines 1–187why, one step back1"use strict"2import produce, {setUseProxies} from "../src/immer"3import {isArray, isPlainObject, isString, keys, trim} from "lodash"45const nonPlainObject = () => {}67runTests("proxy", true)8runTests("es5", false)910function runTests(name, useProxies) {11 describe("recursive - " + name, () => {12 setUseProxies(useProxies)1314 const testCases = [15 {16 title: "in a simple non-recursive case",17 recursiveTransform: recursivelyTrimStrings,18 baseState: " hello ",19 desiredState: "hello"20 },21 {22 title: "in a simple non-recursive unchanged case",23 recursiveTransform: recursivelyTrimStrings,24 baseState: "hello",25 desiredState: "hello"26 },27 {28 title: "if baseState is an object that does not change",29 recursiveTransform: recursivelyTrimStrings,30 baseState: {31 a: 1,32 b: "test",33 c: {34 a: [true, null, false, "test"],35 b: 136 }37 }38 },39 {40 title: "if baseState is an array does not change",41 recursiveTransform: recursivelyTrimStrings,42 baseState: [true, null, false, "test"]43 },44 {45 title: "when all leafs are changing",46 recursiveTransform: recursivelyTrimStrings,47 baseState: {48 x: " hello ",49 arr: [50 "world ",51 42,52 " !!!",53 {54 x: 42,55 test: "true "56 }57 ]58 },59 desiredState: {60 x: "hello",61 arr: [62 "world",63 42,64 "!!!",65 {66 x: 42,67 test: "true"68 }69 ]70 }71 },72 {73 title: "when there is an unchanged leaf array",74 recursiveTransform: recursivelyTrimStrings,75 baseState: {76 x: " hello ",77 arr: [1, 2, 3]78 },79 desiredState: {80 x: "hello",81 arr: [1, 2, 3]82 }83 },84 {85 title:86 "when there is an unchanged leaf object in an unchanged array",87 recursiveTransform: recursivelyTrimStrings,88 baseState: {89 arr: [90 "!!!",91 {92 x: 42,93 y: "test"94 }95 ]96 },97 desiredState: {98 arr: [99 "!!!",100 {101 x: 42,102 y: "test"103 }104 ]105 }106 },107 {108 title:109 "when there is an unchanged leaf object in a changed array",110 recursiveTransform: recursivelyTrimStrings,111 baseState: {112 arr: [113 " !!!",114 {115 x: 42,116 y: "test"117 }118 ]119 },120 desiredState: {121 arr: [122 "!!!",123 {124 x: 42,125 y: "test"126 }127 ]128 }129 },130 {131 title:132 "when there is an unchanged leaf array in a changed object",133 recursiveTransform: recursivelyTrimStrings,134 baseState: {135 test: "!!! ",136 arr: [1, 2, 3]137 },138 desiredState: {139 test: "!!!",140 arr: [1, 2, 3]141 }142 },143 {144 title: "when there non-plain objects",145 recursiveTransform: recursivelyTrimStrings,146 baseState: {147 a: [nonPlainObject, 1],148 b: nonPlainObject149 }150 }151 ]152153 testCases154 .slice(0, 9)155 .forEach(({title, baseState, desiredState, recursiveTransform}) => {156 it(`should not leave drafts ${title}`, () => {157 const next = recursiveTransform(baseState)158 if (desiredState) {159 // an exception is thrown during deep comparison if any of the drafts is not finalized160 expect(next).toEqual(desiredState)161 } else {162 expect(next).toBe(baseState)163 }164 })165 })166 })167}168169function recursivelyTrimStrings(source) {170 if (isString(source)) {171 return trim(source)172 } else if (isArray(source)) {173 return produce(source, draft =>174 draft.forEach((el, i) => {175 const v = recursivelyTrimStrings(el)176 draft[i] = v177 })178 )179 } else if (isPlainObject(source)) {180 return produce(source, draft => {181 keys(draft).forEach(key => {182 draft[key] = recursivelyTrimStrings(draft[key])183 })184 })185 }186 return source187}