Merge remote-tracking branch 'origin' into org-access-list

This commit is contained in:
Kia Lam
2019-03-01 10:37:44 -05:00
10 changed files with 610 additions and 78 deletions

View File

@@ -138,4 +138,36 @@ describe('APIClient (api.js)', () => {
done();
});
test('associateInstanceGroup calls expected http method with expected data', async (done) => {
const createPromise = () => Promise.resolve();
const mockHttp = ({ post: jest.fn(createPromise) });
const api = new APIClient(mockHttp);
const url = 'foo/bar/';
const id = 1;
await api.associateInstanceGroup(url, id);
expect(mockHttp.post).toHaveBeenCalledTimes(1);
expect(mockHttp.post.mock.calls[0][0]).toEqual(url);
expect(mockHttp.post.mock.calls[0][1]).toEqual({ id });
done();
});
test('disassociateInstanceGroup calls expected http method with expected data', async (done) => {
const createPromise = () => Promise.resolve();
const mockHttp = ({ post: jest.fn(createPromise) });
const api = new APIClient(mockHttp);
const url = 'foo/bar/';
const id = 1;
await api.disassociateInstanceGroup(url, id);
expect(mockHttp.post).toHaveBeenCalledTimes(1);
expect(mockHttp.post.mock.calls[0][0]).toEqual(url);
expect(mockHttp.post.mock.calls[0][1]).toEqual({ id, disassociate: true });
done();
});
});

View File

@@ -60,7 +60,7 @@ describe('<Lookup />', () => {
</I18nProvider>
).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,12 +110,11 @@ describe('<Lookup />', () => {
/>
</I18nProvider>
);
const removeIcon = wrapper.find('.awx-c-icon--remove').first();
const removeIcon = wrapper.find('button[aria-label="close"]').first();
removeIcon.simulate('click');
expect(spy).toHaveBeenCalled();
});
test('"wrapTags" method properly handles data', () => {
const spy = jest.spyOn(Lookup.prototype, 'wrapTags');
test('renders chips from prop value', () => {
mockData = [{ name: 'foo', id: 0 }, { name: 'bar', id: 1 }];
const wrapper = mount(
<I18nProvider>
@@ -129,20 +128,18 @@ describe('<Lookup />', () => {
sortedColumnKey="name"
/>
</I18nProvider>
);
expect(spy).toHaveBeenCalled();
const pill = wrapper.find('span.awx-c-tag--pill');
expect(pill).toHaveLength(2);
).find('Lookup');
const chip = wrapper.find('li.pf-c-chip');
expect(chip).toHaveLength(2);
});
test('toggleSelected successfully adds/removes row from lookupSelectedItems state', () => {
mockData = [{ name: 'foo', id: 1 }];
mockData = [];
const wrapper = mount(
<I18nProvider>
<Lookup
lookup_header="Foo Bar"
onLookupSave={() => { }}
value={mockData}
selected={[]}
getItems={() => { }}
columns={mockColumns}
sortedColumnKey="name"
@@ -164,7 +161,7 @@ describe('<Lookup />', () => {
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(
<I18nProvider>

View File

@@ -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('<OrganizationEdit />', () => {
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(
<MemoryRouter initialEntries={['/organizations/1/edit']} initialIndex={0}>
<OrganizationEdit
match={{ path: '/organizations/:id/edit', url: '/organizations/1/edit', params: { id: 1 } }}
/>
<I18nProvider>
<MemoryRouter initialEntries={['/organizations/1']} initialIndex={0}>
<OrganizationEdit
match={{ params: { id: '1' } }}
api={{
getOrganizationInstanceGroups
}}
organization={mockData}
/>
</MemoryRouter>
</I18nProvider>
).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(
<I18nProvider>
<MemoryRouter initialEntries={['/organizations/1']} initialIndex={0}>
<OrganizationEdit
match={{
path: '/organizations/:id',
url: '/organizations/1',
params: { id: '1' }
}}
organization={mockData}
api={{ getOrganizationInstanceGroups }}
/>
</MemoryRouter>
</I18nProvider>
).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(
<MemoryRouter>
<I18nProvider>
<OrganizationEdit
organization={mockData}
api={api}
match={{ path: '/organizations/:id/edit', url: '/organizations/1/edit' }}
/>
</I18nProvider>
</MemoryRouter>
).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(
<MemoryRouter>
<I18nProvider>
<OrganizationEdit
organization={mockData}
api={api}
match={{ path: '/organizations/:id/edit', url: '/organizations/1/edit' }}
/>
</I18nProvider>
</MemoryRouter>
).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(
<MemoryRouter>
<I18nProvider>
<ConfigContext.Provider value={config}>
<OrganizationEdit
match={{
path: '/organizations/:id',
url: '/organizations/1',
params: { id: '1' }
}}
organization={mockData}
api={api}
/>
</ConfigContext.Provider>
</I18nProvider>
</MemoryRouter>
);
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(
<MemoryRouter>
<I18nProvider>
<OrganizationEdit
match={{
path: '/organizations/:id',
url: '/organizations/1',
params: { id: '1' }
}}
organization={mockData}
api={api}
/>
</I18nProvider>
</MemoryRouter>
);
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(
<I18nProvider>
<MemoryRouter initialEntries={['/organizations/1']} initialIndex={0}>
<OrganizationEdit
match={{
path: '/organizations/:id',
url: '/organizations/1',
params: { id: '1' }
}}
organization={mockData}
api={api}
/>
</MemoryRouter>
</I18nProvider>
).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(
<MemoryRouter>
<I18nProvider>
<OrganizationEdit
match={{
path: '/organizations/:id',
url: '/organizations/1',
params: { id: '1' }
}}
organization={mockData}
api={api}
/>
</I18nProvider>
</MemoryRouter>
);
expect(spy).not.toHaveBeenCalled();
wrapper.find('button[aria-label="Cancel"]').prop('onClick')();
expect(spy).toBeCalled();
});
});

View File

@@ -71,7 +71,7 @@ describe('<OrganizationAdd />', () => {
test('Successful form submission triggers redirect', (done) => {
const onSuccess = jest.spyOn(OrganizationAdd.WrappedComponent.prototype, 'onSuccess');
const mockedResp = { data: { id: 1, related: { instance_groups: '/bar' } } };
const api = { createOrganization: jest.fn().mockResolvedValue(mockedResp), createInstanceGroups: jest.fn().mockResolvedValue('done') };
const api = { createOrganization: jest.fn().mockResolvedValue(mockedResp), associateInstanceGroup: jest.fn().mockResolvedValue('done') };
const wrapper = mount(
<MemoryRouter>
<I18nProvider>
@@ -131,10 +131,10 @@ describe('<OrganizationAdd />', () => {
}
}
});
const createInstanceGroupsFn = jest.fn().mockResolvedValue('done');
const associateInstanceGroupFn = jest.fn().mockResolvedValue('done');
const api = {
createOrganization: createOrganizationFn,
createInstanceGroups: createInstanceGroupsFn
associateInstanceGroup: associateInstanceGroupFn
};
const wrapper = mount(
<MemoryRouter>
@@ -156,7 +156,7 @@ describe('<OrganizationAdd />', () => {
description: '',
name: 'mock org'
});
expect(createInstanceGroupsFn).toHaveBeenCalledWith('/api/v2/organizations/1/instance_groups', 1);
expect(associateInstanceGroupFn).toHaveBeenCalledWith('/api/v2/organizations/1/instance_groups', 1);
});
test('AnsibleSelect component renders if there are virtual environments', () => {