import cachePreviousArgs from '../cachePreviousArgs'

describe('cachePreviousArgs', () => {
  const stub = sinon.stub()
  const x = {value: 1}
  const y = {value: 2}

  const fn = (x, y) => { stub(); return x.value + y.value }
  let cachedFn

  beforeEach(() => {
    cachedFn = cachePreviousArgs(fn)
  })

  afterEach(() => {
    stub.resetHistory()
  })

  it('returns the value of the original function', () => {
    expect(cachedFn(x, y)).to.eq(3)
  })

  it('caches the function when the arguments are the same', () => {
    expect(cachedFn(x, y)).to.eq(3)
    expect(cachedFn(x, y)).to.eq(3)
    expect(stub.callCount).to.eq(1)
  })

  it('does not cache the function when one of the args is different', () => {
    expect(cachedFn(x, y)).to.eq(3)
    expect(cachedFn(x, {value: 4})).to.eq(5)
    expect(stub.callCount).to.eq(2)
  })
})
