Skip to content
Myr

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

4 files+74 added−10 removed

modified:__tests__/base.js+63 added-10 removed
1"use strict"2import immerProxy from ".."3import immerEs5 from "../es5"2import * as immerProxy from ".."3import * as immerEs5 from "../es5"4import deepFreeze from "deep-freeze"55runBaseTest("proxy", immerProxy)6runBaseTest("es5", immerEs5)6runBaseTest("proxy (no freeze)", immerProxy, false)7runBaseTest("proxy (autofreeze)", immerProxy, true)8runBaseTest("es5 (no freeze)", immerEs5, false)9runBaseTest("es5 (autofreeze)", immerEs5, true)108function runBaseTest(name, immer) {11function runBaseTest(name, lib, freeze) {12 describe(`base functionality - ${name}`, () => {13 const immer = lib.default14 let baseState15 let origBaseState1617 beforeEach(() => {18 lib.setAutoFreeze(freeze)19 origBaseState = baseState = createBaseState()20 })2122 it("should return the original without modifications", () => {
100 unmodified lines
123 })124 expect(nextState).not.toBe(baseState)125 expect(nextState.anArray).not.toBe(baseState.anArray)126122 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 })135136 it("should support sorting arrays", () => {137 const nextState = immer(baseState, s => {
2 unmodified lines
140 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 })146147 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 })159160 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 })182183 it("should updating inside arrays", () => {184 const nextState = immer(baseState, s => {185 s.anArray[2].test = true186 })
9 unmodified lines
196 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 lines
219 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 lines
304 expect(baseState).toEqual(createBaseState())305 })306307 function createBaseState() {261 return {308 const data = {309 anArray: [3, 2, {c: 3}, 1],310 aProp: "hi",311 anObject: {312 nested: {
1 unmodified line
314 },315 coffee: false316 }317 }318 return freeze ? deepFreeze(data) : data319 }320 })321}322323function enumerableOnly(x) {324 // this can be done better...325 return JSON.parse(JSON.stringify(x))326}
modified:immer.js+6 added
64 unmodified lines
65 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 }7778 // creates a copy for a base object if there ain't one
156 unmodified lines
modified:package.json+1 added
modified:yarn.lock+4 added
923 unmodified lines
924deep-extend@~0.4.0:925 version "0.4.2"926 resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.4.2.tgz#48b699c27e334bf89f10892be432f6e4c7d34a7f"927928deep-freeze@^0.0.1:929 version "0.0.1"930 resolved "https://registry.yarnpkg.com/deep-freeze/-/deep-freeze-0.0.1.tgz#3a0b0005de18672819dfd38cd31f91179c893e84"931932deep-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