2

I am trying to unit test js-cookie My code is like this

import Cookie from 'js-cookie'

onChange (e) {
    Cookie.set('locale', e.target.value)
    window.location.reload()
  }

and for unit testing I am doing this

describe('LocaleDropDown', () => {
  const handleLocaleChange = sinon.spy()
  const Cookie = sinon.spy(Cookie,'set')

  it("when simulating a change, Cookie.set should be called", () => {
    const wrapper = shallow(<LocaleDropDown onChange = {handleLocaleChange}/>)
    wrapper.find('select').simulate('change', { target: { value: 'en'}})
    expect(Cookie.set.calledOnce).toBe(true)
  });

})

It fails saying ReferenceError: Cookie is not defined

How should I be testing js-cookie

keerti
  • 245
  • 5
  • 19

1 Answers1

3

You must import the library also inside your unit test, add this line to your test and you'll be fine.

import Cookie from 'js-cookie'
Armin Šupuk
  • 809
  • 1
  • 9
  • 19