Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React from 'react';
- import { shallow } from 'enzyme';
- import Dashboard from './Dashboard';
- import moxios from 'moxios';
- describe('111', () => {
- const errorMsg = 'Group already exist';
- const event = {
- preventDefault: () => {},
- target: {
- group: '',
- country: ''
- }
- };
- beforeEach(() => {
- moxios.install();
- moxios.stubRequest('/api/dashboard', {
- status: 400,
- response: {
- message: errorMsg
- }
- });
- });
- afterEach(() => {
- moxios.uninstall();
- });
- it('(await for a method) should set error state when dashboard api returns error', async () => {
- const wrapper = shallow(<Dashboard />);
- await wrapper.instance().onFormSubmit(event);
- expect(wrapper.state('error')).toBe(errorMsg);
- });
- it(
- '(promise) should set error state when dashboard api returns error',
- () => {
- const wrapper = shallow(<Dashboard />);
- wrapper.find('form').simulate('submit', event);
- return new Promise(resolve =>
- setImmediate(() => {
- expect(wrapper.state('error')).toBe(errorMsg);
- resolve();
- })
- );
- },
- 1000
- );
- it('(await tick) should set error state when dashboard api returns error', async () => {
- const wrapper = shallow(<Dashboard />);
- wrapper.find('form').simulate('submit', event);
- await tick();
- expect(wrapper.state('error')).toBe(errorMsg);
- });
- it('(done) should set error state when dashboard api returns error', done => {
- const wrapper = shallow(<Dashboard />);
- wrapper.find('form').simulate('submit', event);
- setImmediate(() => {
- expect(wrapper.state('error')).toBe(errorMsg);
- done();
- });
- });
- });
- function tick() {
- return new Promise(resolve => {
- setTimeout(resolve, 1);
- });
- }
Add Comment
Please, Sign In to add comment