dimanche 22 janvier 2017

Sinon error: function is already wrapped

Eventhough I am using the afterEach I still get the following error:Attempted to wrap setTimeout which is already wrapped.What am I doing wrong?I am using ava to do this unit test. THe test I am trying to create is very simple. It is pretty much check the render and a click action. There is also a test to check if the setTImeOt function has been called with the right arguments.

import test from 'ava';
import React from 'react';
import { CartMessage } from 'components/Cart/CartMessage';
import { shallow, mount } from 'enzyme';
import { spy, stub } from 'sinon';

let props;
let popError;
let cartmessage;
let timeoutSpy;


test.beforeEach(() => {
  props = {
    message: 'testing',
    count: 2,
    popError: stub().returns(Promise.resolve()),
  };
  cartmessage = shallow(<CartMessage {...props}/>)

  timeoutSpy = spy(window, 'setTimeout');
});

test.afterEach(()=>{
  timeoutSpy.restore()
})

test('renders okay?', (t) => {
  t.truthy(Cartmessage)
});

test('componentDidMount calls setTimeout with proper args', t => {
  t.true(timeoutSpy.calledWithExactly(() => this.setState({ MessageOpen: true }), 1))
})

test('onClose called?', t => {
  const wrapper = shallow(<CartMessage {...props}  />);
  wrapper.find('i').simulate('click');
  t.true(timeoutSpy.calledWithExactly(this.props.popError, 1))
})

test('timeout i called with the right args', (t) => {
  t.true(timeoutSpy.calledWithExactly(this.props.popError, 1));
})

Aucun commentaire:

Enregistrer un commentaire