Skip to content
Myr

added prettier config based on current code layout

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Gregory Assasie · committed by Michel Weststrate · parent fc88944 · Browse files · 3 files+167 added−149 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+167 added−149 removed

  • tests:+148 added−148 removed
  • other:+19 added−1 removed

3 files+167 added−149 removed

modified:__tests__/base.js+148 added-148 removed
1import immer from "..";1import immer from ".."23describe("base", () => {4 let baseState;5 let origBaseState;4 let baseState5 let origBaseState67 beforeEach(() => {8 origBaseState = baseState = createBaseState();9 });8 origBaseState = baseState = createBaseState()9 })1011 it("should return the original without modifications", () => {12 const nextState = immer(baseState, () => {});13 expect(nextState).toBe(baseState);14 });12 const nextState = immer(baseState, () => {})13 expect(nextState).toBe(baseState)14 })1516 it("should return the original without modifications when reading stuff", () => {17 const nextState = immer(baseState, s => {18 expect(s.aProp).toBe("hi");19 expect(s.anObject.nested).toEqual({ yummie: true });20 });21 expect(nextState).toBe(baseState);22 });18 expect(s.aProp).toBe("hi")19 expect(s.anObject.nested).toEqual({yummie: true})20 })21 expect(nextState).toBe(baseState)22 })2324 it("should not return any value: thunk", () => {25 const warning = jest.spyOn(console, "warn");26 immer(baseState, () => ({ bad: "don't do this" }));27 immer(baseState, () => [1, 2, 3]);28 immer(baseState, () => false);29 immer(baseState, () => "");25 const warning = jest.spyOn(console, "warn")26 immer(baseState, () => ({bad: "don't do this"}))27 immer(baseState, () => [1, 2, 3])28 immer(baseState, () => false)29 immer(baseState, () => "")3031 expect(warning).toHaveBeenCalledTimes(4);32 });31 expect(warning).toHaveBeenCalledTimes(4)32 })3334 it("should return a copy when modifying stuff", () => {35 const nextState = immer(baseState, s => {36 s.aProp = "hello world";37 });38 expect(nextState).not.toBe(baseState);39 expect(baseState.aProp).toBe("hi");40 expect(nextState.aProp).toBe("hello world");36 s.aProp = "hello world"37 })38 expect(nextState).not.toBe(baseState)39 expect(baseState.aProp).toBe("hi")40 expect(nextState.aProp).toBe("hello world")41 // structural sharing?42 expect(nextState.nested).toBe(baseState.nested);43 });42 expect(nextState.nested).toBe(baseState.nested)43 })4445 it("deep change bubbles up", () => {46 const nextState = immer(baseState, s => {47 s.anObject.nested.yummie = false;48 });49 expect(nextState).not.toBe(baseState);50 expect(nextState.anObject).not.toBe(baseState.anObject);51 expect(baseState.anObject.nested.yummie).toBe(true);52 expect(nextState.anObject.nested.yummie).toBe(false);53 expect(nextState.anArray).toBe(baseState.anArray);54 });47 s.anObject.nested.yummie = false48 })49 expect(nextState).not.toBe(baseState)50 expect(nextState.anObject).not.toBe(baseState.anObject)51 expect(baseState.anObject.nested.yummie).toBe(true)52 expect(nextState.anObject.nested.yummie).toBe(false)53 expect(nextState.anArray).toBe(baseState.anArray)54 })5556 it("can add props", () => {57 const nextState = immer(baseState, s => {58 s.anObject.cookie = { tasty: true };59 });60 expect(nextState).not.toBe(baseState);61 expect(nextState.anObject).not.toBe(baseState.anObject);62 expect(nextState.anObject.nested).toBe(baseState.anObject.nested);63 expect(nextState.anObject.cookie).toEqual({ tasty: true });64 });58 s.anObject.cookie = {tasty: true}59 })60 expect(nextState).not.toBe(baseState)61 expect(nextState.anObject).not.toBe(baseState.anObject)62 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)63 expect(nextState.anObject.cookie).toEqual({tasty: true})64 })6566 it("can delete props", () => {67 const nextState = immer(baseState, s => {68 delete s.anObject.nested;69 });70 expect(nextState).not.toBe(baseState);71 expect(nextState.anObject).not.toBe(baseState.anObject);72 expect(nextState.anObject.nested).toBe(undefined);73 });68 delete s.anObject.nested69 })70 expect(nextState).not.toBe(baseState)71 expect(nextState.anObject).not.toBe(baseState.anObject)72 expect(nextState.anObject.nested).toBe(undefined)73 })7475 it("ignores single non-modification", () => {76 const nextState = immer(baseState, s => {77 s.aProp = "hi";78 });79 expect(nextState).toBe(baseState);80 });77 s.aProp = "hi"78 })79 expect(nextState).toBe(baseState)80 })8182 it("processes single modification", () => {83 const nextState = immer(baseState, s => {84 s.aProp = "hello";85 s.aProp = "hi";86 });87 expect(nextState).not.toBe(baseState);88 expect(nextState).toEqual(baseState);89 });84 s.aProp = "hello"85 s.aProp = "hi"86 })87 expect(nextState).not.toBe(baseState)88 expect(nextState).toEqual(baseState)89 })9091 it("should support reading arrays", () => {92 const nextState = immer(baseState, s => {93 s.anArray.slice();94 });95 expect(nextState.anArray).toBe(baseState.anArray);96 expect(nextState).toBe(baseState);97 });93 s.anArray.slice()94 })95 expect(nextState.anArray).toBe(baseState.anArray)96 expect(nextState).toBe(baseState)97 })9899 it("should support changing arrays", () => {100 const nextState = immer(baseState, s => {101 s.anArray[3] = true;102 });103 expect(nextState).not.toBe(baseState);104 expect(nextState.anArray).not.toBe(baseState.anArray);105 expect(nextState.anArray[3]).toEqual(true);106 });101 s.anArray[3] = true102 })103 expect(nextState).not.toBe(baseState)104 expect(nextState.anArray).not.toBe(baseState.anArray)105 expect(nextState.anArray[3]).toEqual(true)106 })107108 it("should support changing arrays - 2", () => {109 const nextState = immer(baseState, s => {110 s.anArray.splice(1, 1, "a", "b");111 });112 expect(nextState).not.toBe(baseState);113 expect(nextState.anArray).not.toBe(baseState.anArray);110 s.anArray.splice(1, 1, "a", "b")111 })112 expect(nextState).not.toBe(baseState)113 expect(nextState.anArray).not.toBe(baseState.anArray)114115 expect(nextState.anArray).toEqual([3, "a", "b", { c: 3 }, 1]);116 });115 expect(nextState.anArray).toEqual([3, "a", "b", {c: 3}, 1])116 })117118 it("should support sorting arrays", () => {119 const nextState = immer(baseState, s => {120 s.anArray[2].c = 4;121 s.anArray.sort();122 s.anArray[3].c = 5;123 });124 expect(nextState).not.toBe(baseState);125 expect(nextState.anArray).not.toBe(baseState.anArray);126 expect(nextState.anArray).toEqual([1, 2, 3, { c: 5 }]);127 });120 s.anArray[2].c = 4121 s.anArray.sort()122 s.anArray[3].c = 5123 })124 expect(nextState).not.toBe(baseState)125 expect(nextState.anArray).not.toBe(baseState.anArray)126 expect(nextState.anArray).toEqual([1, 2, 3, {c: 5}])127 })128129 it("should updating inside arrays", () => {130 const nextState = immer(baseState, s => {131 s.anArray[2].test = true;132 });133 expect(nextState).not.toBe(baseState);134 expect(nextState.anArray).not.toBe(baseState.anArray);135 expect(nextState.anArray).toEqual([3, 2, { c: 3, test: true }, 1]);136 });131 s.anArray[2].test = true132 })133 expect(nextState).not.toBe(baseState)134 expect(nextState.anArray).not.toBe(baseState.anArray)135 expect(nextState.anArray).toEqual([3, 2, {c: 3, test: true}, 1])136 })137138 it("reusing object should work", () => {139 const nextState = immer(baseState, s => {140 debugger;141 const obj = s.anObject;142 delete s.anObject;143 s.messy = obj;144 });145 expect(nextState).not.toBe(baseState);146 expect(nextState.anArray).toBe(baseState.anArray);140 debugger141 const obj = s.anObject142 delete s.anObject143 s.messy = obj144 })145 expect(nextState).not.toBe(baseState)146 expect(nextState.anArray).toBe(baseState.anArray)147 expect(nextState).toEqual({148 anArray: [3, 2, { c: 3 }, 1],148 anArray: [3, 2, {c: 3}, 1],149 aProp: "hi",150 messy: {151 nested: {152 yummie: true152 yummie: true,153 },154 coffee: false155 }156 });157 expect(nextState.messy.nested).toBe(baseState.anObject.nested);158 });154 coffee: false,155 },156 })157 expect(nextState.messy.nested).toBe(baseState.anObject.nested)158 })159160 it("refs should be transparent", () => {161 const nextState = immer(baseState, s => {162 const obj = s.anObject;163 s.aProp = "hello";164 delete s.anObject;165 obj.coffee = true;166 s.messy = obj;167 });168 expect(nextState).not.toBe(baseState);169 expect(nextState.anArray).toBe(baseState.anArray);162 const obj = s.anObject163 s.aProp = "hello"164 delete s.anObject165 obj.coffee = true166 s.messy = obj167 })168 expect(nextState).not.toBe(baseState)169 expect(nextState.anArray).toBe(baseState.anArray)170 expect(nextState).toEqual({171 anArray: [3, 2, { c: 3 }, 1],171 anArray: [3, 2, {c: 3}, 1],172 aProp: "hello",173 messy: {174 nested: {175 yummie: true175 yummie: true,176 },177 coffee: true178 }179 });180 expect(nextState.messy.nested).toBe(baseState.anObject.nested);181 });177 coffee: true,178 },179 })180 expect(nextState.messy.nested).toBe(baseState.anObject.nested)181 })182183 it("should allow setting to undefined a defined draft property", () => {184 const nextState = immer(baseState, s => {185 s.aProp = undefined;186 });187 expect(nextState).not.toBe(baseState);188 expect(baseState.aProp).toBe("hi");189 expect(nextState.aProp).toBe(undefined);190 });185 s.aProp = undefined186 })187 expect(nextState).not.toBe(baseState)188 expect(baseState.aProp).toBe("hi")189 expect(nextState.aProp).toBe(undefined)190 })191192 afterEach(() => {193 expect(baseState).toBe(origBaseState);194 expect(baseState).toEqual(createBaseState());195 });193 expect(baseState).toBe(origBaseState)194 expect(baseState).toEqual(createBaseState())195 })196197 function createBaseState() {198 return {199 anArray: [3, 2, { c: 3 }, 1],199 anArray: [3, 2, {c: 3}, 1],200 aProp: "hi",201 anObject: {202 nested: {203 yummie: true203 yummie: true,204 },205 coffee: false206 }207 };205 coffee: false,206 },207 }208 }209});209})210211describe("readme example", () => {212 it("works", () => {213 const baseState = [214 {215 todo: "Learn typescript",216 done: true216 done: true,217 },218 {219 todo: "Try immer",220 done: false221 }222 ];220 done: false,221 },222 ]223224 const nextState = immer(baseState, state => {225 state.push({ todo: "Tweet about it" });226 state[1].done = true;227 });225 state.push({todo: "Tweet about it"})226 state[1].done = true227 })228229 // the new item is only added to the next state,230 // base state is unmodified231 expect(baseState.length).toBe(2);232 expect(nextState.length).toBe(3);231 expect(baseState.length).toBe(2)232 expect(nextState.length).toBe(3)233234 // same for the changed 'done' prop235 expect(baseState[1].done).toBe(false);236 expect(nextState[1].done).toBe(true);235 expect(baseState[1].done).toBe(false)236 expect(nextState[1].done).toBe(true)237238 // unchanged data is structurally shared239 expect(nextState[0]).toBe(baseState[0]);239 expect(nextState[0]).toBe(baseState[0])240 // changed data not (dûh)241 expect(nextState[1]).not.toBe(baseState[1]);242 });243});241 expect(nextState[1]).not.toBe(baseState[1])242 })243})
modified:.gitignore-1 removed
54 unmodified lines
55.yarn-integrity5657# dotenv environment variables file58.env59.idea5960.idea
added:.prettierrc+19 added
1{2 "bracketSpacing": false,3 "printWidth": 80,4 "proseWrap": "never",5 "requirePragma": false,6 "semi": false,7 "singleQuote": false,8 "tabWidth": 2,9 "trailingComma": "all",10 "useTabs": false,11 "overrides": [12 {13 "files": "*.json",14 "options": {15 "printWidth": 20016 }17 }18 ]19}