简体   繁体   中英

assert true if method has been called using avajs

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?

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);
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM