From c3fc00c45a7dc904f93ffab8f0f001706abc5c9d Mon Sep 17 00:00:00 2001 From: Marliana Lara Date: Fri, 22 Feb 2019 14:53:47 -0500 Subject: [PATCH] Add Organization Edit tests and fix Lookup unit tests --- __tests__/components/Lookup.test.jsx | 17 +- .../Organization/OrganizationEdit.test.jsx | 238 +++++++++++++++++- 2 files changed, 241 insertions(+), 14 deletions(-) diff --git a/__tests__/components/Lookup.test.jsx b/__tests__/components/Lookup.test.jsx index c505fe1bc1..95763d4912 100644 --- a/__tests__/components/Lookup.test.jsx +++ b/__tests__/components/Lookup.test.jsx @@ -60,7 +60,7 @@ describe('', () => { ).find('Lookup'); expect(spy).not.toHaveBeenCalled(); - expect(wrapper.state('lookupSelectedItems')).toEqual([]); + expect(wrapper.state('lookupSelectedItems')).toEqual(mockSelected); const searchItem = wrapper.find('.pf-c-input-group__text#search'); searchItem.first().simulate('click'); expect(spy).toHaveBeenCalled(); @@ -110,7 +110,7 @@ describe('', () => { /> ); - const removeIcon = wrapper.find('.awx-c-icon--remove').first(); + const removeIcon = wrapper.find('button[aria-label="close"]').first(); removeIcon.simulate('click'); expect(spy).toHaveBeenCalled(); }); @@ -135,14 +135,13 @@ describe('', () => { expect(overflowChip).toHaveLength(1); }); test('toggleSelected successfully adds/removes row from lookupSelectedItems state', () => { - mockData = [{ name: 'foo', id: 1 }]; + mockData = []; const wrapper = mount( { }} value={mockData} - selected={[]} getItems={() => { }} columns={mockColumns} sortedColumnKey="name" @@ -164,7 +163,7 @@ describe('', () => { expect(wrapper.state('lookupSelectedItems')).toEqual([]); }); test('saveModal calls callback with selected items', () => { - mockData = [{ name: 'foo', id: 1 }]; + mockData = []; const onLookupSaveFn = jest.fn(); const wrapper = mount( @@ -198,10 +197,10 @@ describe('', () => { { }} - data={mockData} - selected={[]} + value={mockData} columns={mockColumns} sortedColumnKey="name" + getItems={() => { }} /> ).find('Lookup'); @@ -217,10 +216,10 @@ describe('', () => { { }} - data={mockData} - selected={[]} + value={mockData} columns={mockColumns} sortedColumnKey="name" + getItems={() => { }} /> ).find('Lookup'); diff --git a/__tests__/pages/Organizations/screens/Organization/OrganizationEdit.test.jsx b/__tests__/pages/Organizations/screens/Organization/OrganizationEdit.test.jsx index 6bcb5c6b37..39c638bba6 100644 --- a/__tests__/pages/Organizations/screens/Organization/OrganizationEdit.test.jsx +++ b/__tests__/pages/Organizations/screens/Organization/OrganizationEdit.test.jsx @@ -1,16 +1,244 @@ import React from 'react'; import { mount } from 'enzyme'; import { MemoryRouter } from 'react-router-dom'; +import { I18nProvider } from '@lingui/react'; +import { ConfigContext } from '../../../../../src/context'; +import APIClient from '../../../../../src/api'; import OrganizationEdit from '../../../../../src/pages/Organizations/screens/Organization/OrganizationEdit'; describe('', () => { - test('initially renders succesfully', () => { + const mockData = { + name: 'Foo', + description: 'Bar', + custom_virtualenv: 'Fizz', + id: 1, + related: { + instance_groups: '/api/v2/organizations/1/instance_groups' + } + }; + + test('should request related instance groups from api', () => { + const mockInstanceGroups = [ + { name: 'One', id: 1 }, + { name: 'Two', id: 2 } + ]; + const getOrganizationInstanceGroups = jest.fn(() => ( + Promise.resolve({ data: { results: mockInstanceGroups } }) + )); mount( - - + + + + + + ).find('OrganizationEdit'); + + expect(getOrganizationInstanceGroups).toHaveBeenCalledTimes(1); + }); + + test('componentDidMount should set instanceGroups to state', async () => { + const mockInstanceGroups = [ + { name: 'One', id: 1 }, + { name: 'Two', id: 2 } + ]; + const getOrganizationInstanceGroups = jest.fn(() => ( + Promise.resolve({ data: { results: mockInstanceGroups } }) + )); + const wrapper = mount( + + + + + + ).find('OrganizationEdit'); + + await wrapper.instance().componentDidMount(); + expect(wrapper.state().form.instanceGroups.value).toEqual(mockInstanceGroups); + }); + test('onLookupSave successfully sets instanceGroups state', () => { + const api = jest.fn(); + const wrapper = mount( + + + + + + ).find('OrganizationEdit'); + + wrapper.instance().onLookupSave([ + { + id: 1, + name: 'foo' + } + ], 'instanceGroups'); + expect(wrapper.state().form.instanceGroups.value).toEqual([ + { + id: 1, + name: 'foo' + } + ]); + }); + test('calls "onFieldChange" when input values change', () => { + const api = new APIClient(); + const spy = jest.spyOn(OrganizationEdit.WrappedComponent.prototype, 'onFieldChange'); + const wrapper = mount( + + + + + + ).find('OrganizationEdit'); + + expect(spy).not.toHaveBeenCalled(); + wrapper.instance().onFieldChange('foo', { target: { name: 'name' } }); + wrapper.instance().onFieldChange('bar', { target: { name: 'description' } }); + expect(spy).toHaveBeenCalledTimes(2); + }); + test('AnsibleSelect component renders if there are virtual environments', () => { + const api = jest.fn(); + const config = { + custom_virtualenvs: ['foo', 'bar'], + }; + const wrapper = mount( + + + + + + ); + expect(wrapper.find('FormSelect')).toHaveLength(1); + expect(wrapper.find('FormSelectOption')).toHaveLength(2); + }); + test('calls onSubmit when Save button is clicked', () => { + const api = jest.fn(); + const spy = jest.spyOn(OrganizationEdit.WrappedComponent.prototype, 'onSubmit'); + const wrapper = mount( + + + + + + ); + expect(spy).not.toHaveBeenCalled(); + wrapper.find('button[aria-label="Save"]').prop('onClick')(); + expect(spy).toBeCalled(); + }); + test('onSubmit associates and disassociates instance groups', async () => { + const mockInstanceGroups = [ + { name: 'One', id: 1 }, + { name: 'Two', id: 2 } + ]; + const getOrganizationInstanceGroupsFn = jest.fn(() => ( + Promise.resolve({ data: { results: mockInstanceGroups } }) + )); + const mockDataForm = { + name: 'Foo', + description: 'Bar', + custom_virtualenv: 'Fizz', + }; + const updateOrganizationDetailsFn = jest.fn().mockResolvedValue(1, mockDataForm); + const associateInstanceGroupFn = jest.fn().mockResolvedValue('done'); + const disassociateInstanceGroupFn = jest.fn().mockResolvedValue('done'); + const api = { + getOrganizationInstanceGroups: getOrganizationInstanceGroupsFn, + updateOrganizationDetails: updateOrganizationDetailsFn, + associateInstanceGroup: associateInstanceGroupFn, + disassociateInstanceGroup: disassociateInstanceGroupFn + }; + const wrapper = mount( + + + + + + ).find('OrganizationEdit'); + + await wrapper.instance().componentDidMount(); + + wrapper.instance().onLookupSave([ + { name: 'One', id: 1 }, + { name: 'Three', id: 3 } + ], 'instanceGroups'); + + await wrapper.instance().onSubmit(); + expect(updateOrganizationDetailsFn).toHaveBeenCalledWith(1, mockDataForm); + expect(associateInstanceGroupFn).toHaveBeenCalledWith('/api/v2/organizations/1/instance_groups', 3); + expect(associateInstanceGroupFn).not.toHaveBeenCalledWith('/api/v2/organizations/1/instance_groups', 1); + expect(associateInstanceGroupFn).not.toHaveBeenCalledWith('/api/v2/organizations/1/instance_groups', 2); + + expect(disassociateInstanceGroupFn).toHaveBeenCalledWith('/api/v2/organizations/1/instance_groups', 2); + expect(disassociateInstanceGroupFn).not.toHaveBeenCalledWith('/api/v2/organizations/1/instance_groups', 1); + expect(disassociateInstanceGroupFn).not.toHaveBeenCalledWith('/api/v2/organizations/1/instance_groups', 3); + }); + + test('calls "onCancel" when Cancel button is clicked', () => { + const api = jest.fn(); + const spy = jest.spyOn(OrganizationEdit.WrappedComponent.prototype, 'onCancel'); + const wrapper = mount( + + + + + + ); + expect(spy).not.toHaveBeenCalled(); + wrapper.find('button[aria-label="Cancel"]').prop('onClick')(); + expect(spy).toBeCalled(); }); });