( name, autoFreeze, useStrictShallowCopy, useListener, useArrayMethods = false )
| 53 | class Foo {} |
| 54 | |
| 55 | function runBaseTest( |
| 56 | name, |
| 57 | autoFreeze, |
| 58 | useStrictShallowCopy, |
| 59 | useListener, |
| 60 | useArrayMethods = false |
| 61 | ) { |
| 62 | const listener = useListener ? function () {} : undefined |
| 63 | |
| 64 | const {produce, produceWithPatches} = createPatchedImmer({ |
| 65 | autoFreeze, |
| 66 | useStrictShallowCopy |
| 67 | }) |
| 68 | |
| 69 | // When `useListener` is true, append a function to the arguments of every |
| 70 | // uncurried `produce` call in every test. This makes tests easier to read. |
| 71 | function createPatchedImmer(options) { |
| 72 | const immer = new Immer(options) |
| 73 | |
| 74 | const {produce} = immer |
| 75 | immer.produce = function (...args) { |
| 76 | return typeof args[1] === "function" && args.length < 3 |
| 77 | ? produce(...args, listener) |
| 78 | : produce(...args) |
| 79 | } |
| 80 | |
| 81 | return immer |
| 82 | } |
| 83 | |
| 84 | describe(`base functionality - ${name}`, () => { |
| 85 | let baseState |
| 86 | let origBaseState |
| 87 | |
| 88 | beforeEach(() => { |
| 89 | origBaseState = baseState = createBaseState() |
| 90 | |
| 91 | // Allow running our tests with and without the array method plugin |
| 92 | if (useArrayMethods) { |
| 93 | enableArrayMethods() |
| 94 | } else { |
| 95 | clearPlugin(PluginArrayMethods) |
| 96 | } |
| 97 | }) |
| 98 | |
| 99 | it("returns the original state when no changes are made", () => { |
| 100 | const nextState = produce(baseState, s => { |
| 101 | expect(s.aProp).toBe("hi") |
| 102 | expect(s.anObject.nested).toMatchObject({yummie: true}) |
| 103 | }) |
| 104 | expect(nextState).toBe(baseState) |
| 105 | }) |
| 106 | |
| 107 | it("does structural sharing", () => { |
| 108 | const random = Math.random() |
| 109 | const nextState = produce(baseState, s => { |
| 110 | s.aProp = random |
| 111 | }) |
| 112 | expect(nextState).not.toBe(baseState) |
no test coverage detected
searching dependent graphs…