Add failing tests for recursive use
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Alexander Kachkaev · committed by Alexander Kachkaev · parent 77c17f7 · Browse files · 1 file·+184 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+184 added−0 removed
- tests:+184 added−0 removed
Provenance
- Commit
3664e8b- Tree
14ed9cc- Author
Alexander Kachkaev <alexander@kachkaev.ru>- Committer
Alexander Kachkaev <alexander@kachkaev.ru>- Parent
77c17f7
1 file changed+184 added−0 removed
added:__tests__/recursive.js+184 added
1+"use strict"2+import produce, {setUseProxies} from "../src/immer"3+import {isArray, isPlainObject, isString, keys, trim} from "lodash"4+5+const nonPlainObject = () => {}6+7+runTests("proxy", true)8+runTests("es5", false)9+10+function runTests(name, useProxies) {11+ describe("recursive - " + name, () => {12+ setUseProxies(useProxies)13+14+ 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+ ]152+153+ testCases.forEach(154+ ({title, baseState, desiredState, recursiveTransform}) => {155+ it(`should not leave drafts ${title}`, () => {156+ const next = recursiveTransform(baseState)157+ if (desiredState) {158+ // an exception is thrown during deep comparison if any of the drafts is not finalized159+ expect(next).toEqual(desiredState)160+ } else {161+ expect(next).toBe(baseState)162+ }163+ })164+ }165+ )166+ })167+}168+169+function 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) => (draft[i] = recursivelyTrimStrings(el)))175+ )176+ } else if (isPlainObject(source)) {177+ return produce(source, draft => {178+ keys(draft).forEach(key => {179+ draft[key] = recursivelyTrimStrings(draft[key])180+ })181+ })182+ }183+ return source184+}