Skip to content

Commit

Permalink
Spec
Browse files Browse the repository at this point in the history
  • Loading branch information
rvsia committed Jun 21, 2019
1 parent 510e209 commit e949340
Showing 1 changed file with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import React from 'react';
import { mount } from 'enzyme';
import fetchMock from 'fetch-mock';
import CopyDashboardForm from '../../components/copy-dashboard-form/copy-dashboard-form';

import '../helpers/miqSparkle';
import MiqFormRenderer from '../../forms/data-driven-form';
import * as handleFailure from '../../helpers/handle-failure';

describe('Copy Dashboard form', () => {
let initialProps;
let baseUrl;
let apiUrl;
let dashboardData;
let apiData;
let submitSpyMiqSparkleOn;
let submitSpyMiqSparkleOff;

beforeEach(() => {
initialProps = { dashboardId: '55' };

dashboardData = {
name: 'Clint',
description: 'good dashboard',
owner_id: '12',
};

apiData = {
resources: [
{ name: 'name1', id: '1' },
{ name: '80s', id: '888' },
{ name: 'current group', id: '12' },
],
};

baseUrl = `/report/dashboard_get/${initialProps.dashboardId}`;

apiUrl = '/api/groups?expand=resources';

submitSpyMiqSparkleOn = jest.spyOn(window, 'miqSparkleOn');
submitSpyMiqSparkleOff = jest.spyOn(window, 'miqSparkleOff');
});

afterEach(() => {
fetchMock.restore();

submitSpyMiqSparkleOn.mockRestore();
submitSpyMiqSparkleOff.mockRestore();
});

it('should render correctly and set initialValue', (done) => {
fetchMock
.getOnce(baseUrl, dashboardData);
fetchMock
.getOnce(apiUrl, apiData);

const wrapper = mount(<CopyDashboardForm {...initialProps} />);

setImmediate(() => {
wrapper.update();
expect(wrapper.find(MiqFormRenderer)).toHaveLength(1);
expect(wrapper.find('input[name="name"]').instance().value).toEqual('Clint');
expect(wrapper.find('input[name="description"]').instance().value).toEqual('good dashboard');
expect(wrapper.find('input[name="group_id"]').instance().value).toEqual('12');
expect(submitSpyMiqSparkleOn).toHaveBeenCalledTimes(1);
expect(submitSpyMiqSparkleOff).toHaveBeenCalledTimes(1);
done();
});
});

it('should handle error', (done) => {
fetchMock
.getOnce(baseUrl, dashboardData);
fetchMock
.getOnce(apiUrl, 400);

handleFailure.default = jest.fn();

const wrapper = mount(<CopyDashboardForm {...initialProps} />);

setImmediate(() => {
wrapper.update();
expect(handleFailure.default).toHaveBeenCalled();
done();
});
});
});

0 comments on commit e949340

Please sign in to comment.