LoginSignup
4
6

More than 5 years have passed since last update.

Jestによるテスト

Posted at

コンポーネントのテスト

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でテスト結果を確認

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6