Skip to content

Commit

Permalink
test: patch the produce function for interweaved Immer instances too
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jan 4, 2019
1 parent be06ef5 commit 31bbe64
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions __tests__/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,25 @@ runBaseTest("es5 (autofreeze)", false, true)
runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)

function runBaseTest(name, useProxies, autoFreeze, useListener) {
const immer = new Immer({
const listener = useListener ? function() {} : undefined
const {produce} = createPatchedImmer({
useProxies,
autoFreeze
})

// make sure logic doesn't break when listener is attached
const listener = useListener ? function() {} : undefined
const produce = (...args) =>
typeof args[1] === "function" && args.length < 3
? immer.produce(...args, listener)
: immer.produce(...args)
// When `useListener` is true, append a function to the arguments of every
// uncurried `produce` call in every test. This makes tests easier to read.
function createPatchedImmer(options) {
const immer = new Immer(options)

const {produce} = immer
immer.produce = (...args) =>
typeof args[1] === "function" && args.length < 3
? produce(...args, listener)
: produce(...args)

return immer
}

describe(`base functionality - ${name}`, () => {
let baseState
Expand Down Expand Up @@ -807,8 +815,8 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) {

it("works with interweaved Immer instances", () => {
const options = {useProxies, autoFreeze}
const one = new Immer(options)
const two = new Immer(options)
const one = createPatchedImmer(options)
const two = createPatchedImmer(options)

const base = {}
const result = one.produce(base, s1 =>
Expand Down

0 comments on commit 31bbe64

Please sign in to comment.