-1

I am trying to create a unit test using react,ava, etc..I am having issue creating a simple unit test to check if a method was called. My test should pass if the method has been called. However when i check the code coverage I get a message saying "function not covered". Below is the code I am using to test it.

import test from 'ava';
import React from 'react';
import { Cart } from 'components/Cart/CartDisplay';
import { shallow } from 'enzyme';

let props;

test.beforeEach(() => {
props = {
popError: () => {},
message: '',
count: 2,
displayCart:() => {},
onClose:() => {}
};

});

test('renders okay?', (t) => {
shallow(
<Cart {...props} />
);
t.pass('yes');
});

 test('Cart Displayed okay?', (t) => {
 props.displayCart();
 t.pass('yes');
 });

What am I doing wrong?

zeid10
  • 511
  • 8
  • 28

1 Answers1

1

After a couple of tries, I was able to figure it out:

import test from 'ava';
import React from 'react';
import { Cart } from 'components/Cart/CartDisplay';
import { shallow,mount } from 'enzyme';
import sinon from 'sinon';
import {expect} from 'chai';

let props;

test.beforeEach(() => {
  props = {
    popError: () => {},
    message: '',
    count: 2,
    displayCart:() => {},
    onClose:() => {}
  };

});

test('Cart Display called?', t => {
    sinon.spy(Cart.prototype, 'cartDisplay');
    const wrapper = mount(<BannerMessage />);
    expect(Cart.prototype.componentDidMount.calledOnce).to.equal(true);
})
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
zeid10
  • 511
  • 8
  • 28