コンポーネントのテスト
import React from 'react';
import { shallow } from 'enzyme';
import HogeButton from './HogeButton';
describe('HogeButton', () => {
it('onClickEvent Test', () => {
const testMock = jest.fn();
const wrapper = shallow(<HogeButton/>);
wrapper.setProps({ actions: {actionHoge : testMock }})
wrapper.find('Button').simulate('click');
expect(testMock).toHaveBeenCalled();
});
});
上記は、onClickイベントの呼び出しの確認用のコード
テストするコンポーネントのインポートを行ない
shallowでレンダリングを行なう
onClickイベントで呼び出される関数を用意し、
simulateで実行
最後にtoHaveBeenCalledで呼び出されていることを確認する。
ファンクションのテスト
import React from 'react';
import Fuga from './Fuga';
describe('Fuga', () => {
it('getFuga Test', () => {
const testFunc = Fuga.getFuga ();
expect(testFunc).toEqual('FugaFuga');
});
});
テストするクラスのインポートを行ない。
testFuncにテストするファンクションを格納
toEqualでテスト結果を確認