improved test setup
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent c3fcf65 · Browse files · 4 files·+74 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+74 added−10 removed
- tests:+63 added−10 removed
- other:+11 added−0 removed
Provenance
- Commit
6990c33- Tree
7fdca6a- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
c3fcf65
4 files changed+74 added−10 removed
modified:__tests__/base.js+63 added-10 removed
1 "use strict"2-import immerProxy from ".."3-import immerEs5 from "../es5"2+import * as immerProxy from ".."3+import * as immerEs5 from "../es5"4+import deepFreeze from "deep-freeze"5 5-runBaseTest("proxy", immerProxy)6-runBaseTest("es5", immerEs5)6+runBaseTest("proxy (no freeze)", immerProxy, false)7+runBaseTest("proxy (autofreeze)", immerProxy, true)8+runBaseTest("es5 (no freeze)", immerEs5, false)9+runBaseTest("es5 (autofreeze)", immerEs5, true)10 8-function runBaseTest(name, immer) {11+function runBaseTest(name, lib, freeze) {12 describe(`base functionality - ${name}`, () => {13+ const immer = lib.default14 let baseState15 let origBaseState16 17 beforeEach(() => {18+ lib.setAutoFreeze(freeze)19 origBaseState = baseState = createBaseState()20 })21 22 it("should return the original without modifications", () => {100 unmodified lines123 })124 expect(nextState).not.toBe(baseState)125 expect(nextState.anArray).not.toBe(baseState.anArray)126 122- expect(nextState.anArray).toEqual([3, "a", "b", {c: 3}, 1])127+ expect(enumerableOnly(nextState.anArray)).toEqual([128+ 3,129+ "a",130+ "b",131+ {c: 3},132+ 1133+ ])134 })135 136 it("should support sorting arrays", () => {137 const nextState = immer(baseState, s => {2 unmodified lines140 s.anArray[3].c = 5141 })142 expect(nextState).not.toBe(baseState)143 expect(nextState.anArray).not.toBe(baseState.anArray)133- expect(nextState.anArray).toEqual([1, 2, 3, {c: 5}])144+ expect(enumerableOnly(nextState.anArray)).toEqual([1, 2, 3, {c: 5}])145 })146 147+ it.only("should support sorting arrays - 2", () => {148+ const nextState = immer([], s => {149+ debugger150+ s.unshift("x")151+ // console.dir(s)152+ // console.dir(Object.getOwnPropertyDescriptor(s, "length"))153+ // console.dir(Object.getOwnPropertyDescriptor(s, 0))154+ expect(s.length).toBe(1)155+ expect(s[0] === "x").toBe(true)156+ })157+ expect(nextState).toEqual(["x"])158+ })159+160+ it.only("should support sorting arrays - 2", () => {161+ const nextState = immer(baseState, s => {162+ debugger163+ s.anArray.unshift("x")164+ s.anArray[3].c = 4165+ // console.dir(s.anArray)166+ s.anArray.sort()167+ // console.dir(s.anArray)168+ s.anArray[3].c = 5169+ s.anArray.unshift("y")170+ })171+ expect(nextState).not.toBe(baseState)172+ expect(nextState.anArray).not.toBe(baseState.anArray)173+ expect(enumerableOnly(nextState.anArray)).toEqual([174+ "y",175+ 1,176+ 2,177+ 3,178+ {c: 5},179+ "x"180+ ])181+ })182+183 it("should updating inside arrays", () => {184 const nextState = immer(baseState, s => {185 s.anArray[2].test = true186 })9 unmodified lines196 s.messy = obj197 })198 expect(nextState).not.toBe(baseState)199 expect(nextState.anArray).toBe(baseState.anArray)153- expect(nextState).toEqual({200+ expect(enumerableOnly(nextState)).toEqual({201 anArray: [3, 2, {c: 3}, 1],202 aProp: "hi",203 messy: {204 nested: {14 unmodified lines219 s.messy = obj220 })221 expect(nextState).not.toBe(baseState)222 expect(nextState.anArray).toBe(baseState.anArray)176- expect(nextState).toEqual({223+ expect(enumerableOnly(nextState)).toEqual({224 anArray: [3, 2, {c: 3}, 1],225 aProp: "hello",226 messy: {227 nested: {76 unmodified lines304 expect(baseState).toEqual(createBaseState())305 })306 307 function createBaseState() {261- return {308+ const data = {309 anArray: [3, 2, {c: 3}, 1],310 aProp: "hi",311 anObject: {312 nested: {1 unmodified line314 },315 coffee: false316 }317 }318+ return freeze ? deepFreeze(data) : data319 }320 })321 }322+323+function enumerableOnly(x) {324+ // this can be done better...325+ return JSON.parse(JSON.stringify(x))326+}modified:immer.js+6 added
64 unmodified lines65 deleteProperty(target, property) {66 const copy = getOrCreateCopy(target)67 delete copy[property]68 return true69+ },70+ getOwnPropertyDescriptor(target, prop) {71+ return Reflect.getOwnPropertyDescriptor(72+ getCurrentSource(target),73+ prop74+ )75 }76 }77 78 // creates a copy for a base object if there ain't one156 unmodified linesmodified:package.json+1 added
35 unmodified lines36373839404142434410 unmodified lines35 unmodified lines "@types/jest": "^22.0.0", "babel-core": "^6.26.0", "babel-jest": "^22.0.4", "babel-preset-env": "^1.6.1", "deep-freeze": "^0.0.1", "immutable": "^3.8.2", "jest": "^22.0.4", "lodash.clonedeep": "^4.5.0", "pre-commit": "^1.2.2",10 unmodified linesmodified:yarn.lock+4 added
923 unmodified lines924 deep-extend@~0.4.0:925 version "0.4.2"926 resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.4.2.tgz#48b699c27e334bf89f10892be432f6e4c7d34a7f"927 928+deep-freeze@^0.0.1:929+ version "0.0.1"930+ resolved "https://registry.yarnpkg.com/deep-freeze/-/deep-freeze-0.0.1.tgz#3a0b0005de18672819dfd38cd31f91179c893e84"931+932 deep-is@~0.1.3:933 version "0.1.3"934 resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.3.tgz#b369d6fb5dbc13eecf524f91b070feedc357cf34"935 2186 unmodified lines