diff --git a/awx/ui_next/src/components/Lookup/CredentialsLookup.test.jsx b/awx/ui_next/src/components/Lookup/CredentialsLookup.test.jsx new file mode 100644 index 0000000000..c0cb0d19fb --- /dev/null +++ b/awx/ui_next/src/components/Lookup/CredentialsLookup.test.jsx @@ -0,0 +1,103 @@ +import React from 'react'; +import { mountWithContexts } from '@testUtils/enzymeHelpers'; +import CredentialsLookup, { _CredentialsLookup } from './CredentialsLookup'; +import { CredentialsAPI, CredentialTypesAPI } from '@api'; + +jest.mock('@api'); + +describe('', () => { + let wrapper; + let lookup; + let credLookup; + + const credentials = [ + { id: 1, kind: 'cloud', name: 'Foo', url: 'www.google.com' }, + { id: 2, kind: 'ssh', name: 'Alex', url: 'www.google.com' }, + ]; + beforeEach(() => { + CredentialTypesAPI.read.mockResolvedValue({ + data: { + results: [ + { + id: 400, + kind: 'ssh', + namespace: 'biz', + name: 'Amazon Web Services', + }, + { id: 500, kind: 'vault', namespace: 'buzz', name: 'Vault' }, + { id: 600, kind: 'machine', namespace: 'fuzz', name: 'Machine' }, + ], + count: 2, + }, + }); + CredentialsAPI.read.mockResolvedValueOnce({ + data: { + results: [ + { id: 1, kind: 'cloud', name: 'Cred 1', url: 'www.google.com' }, + { id: 2, kind: 'ssh', name: 'Cred 2', url: 'www.google.com' }, + { id: 3, kind: 'Ansible', name: 'Cred 3', url: 'www.google.com' }, + { id: 4, kind: 'Machine', name: 'Cred 4', url: 'www.google.com' }, + { id: 5, kind: 'Machine', name: 'Cred 5', url: 'www.google.com' }, + ], + count: 3, + }, + }); + + wrapper = mountWithContexts( + {}} + credentials={credentials} + onChange={() => {}} + tooltip="This is credentials look up" + /> + ); + lookup = wrapper.find('Lookup'); + credLookup = wrapper.find('CredentialsLookup'); + }); + + afterEach(() => { + jest.clearAllMocks(); + wrapper.unmount(); + }); + + test('CredentialsLookup renders properly', () => { + expect(wrapper.find('CredentialsLookup')).toHaveLength(1); + }); + test('Removes credential from directly from input', () => { + const chip = wrapper.find('PFChip').at(0); + expect(chip).toHaveLength(1); + chip.find('ChipButton').invoke('onClick')(); + expect(wrapper.find('PFChip')).toHaveLength(1); + }); + test('can change credential types', async () => { + lookup.prop('selectCategory')({}, 'Vault'); + expect(credLookup.state('selectedCredentialType')).toEqual({ + id: 500, + kind: 'vault', + type: 'buzz', + value: 'Vault', + label: 'Vault', + isDisabled: false, + }); + expect(CredentialsAPI.read).toHaveBeenCalled(); + }); + test('Toggle credentials properly adds credentials', async () => { + function callOnToggle(item, index) { + lookup.prop('onToggleItem')(item); + expect(credLookup.state('credentials')[index].name).toEqual( + `${item.name}` + ); + } + callOnToggle({ name: 'Gatsby', id: 8, kind: 'Machine' }, 2); + callOnToggle({ name: 'Party', id: 9, kind: 'Machine' }, 2); + callOnToggle({ name: 'Animal', id: 10, kind: 'Ansible' }, 3); + + lookup.prop('onToggleItem')({ + id: 1, + kind: 'cloud', + name: 'Foo', + url: 'www.google.com', + }); + expect(credLookup.state('credentials').length).toBe(3); + }); +}); diff --git a/awx/ui_next/src/screens/Template/JobTemplateEdit/JobTemplateEdit.test.jsx b/awx/ui_next/src/screens/Template/JobTemplateEdit/JobTemplateEdit.test.jsx index 6d6c1e4083..dc5c22f4ee 100644 --- a/awx/ui_next/src/screens/Template/JobTemplateEdit/JobTemplateEdit.test.jsx +++ b/awx/ui_next/src/screens/Template/JobTemplateEdit/JobTemplateEdit.test.jsx @@ -40,6 +40,10 @@ const mockJobTemplate = { id: 2, organization_id: 1, }, + credentials: [ + { id: 1, kind: 'cloud', name: 'Foo' }, + { id: 2, kind: 'ssh', name: 'Bar' }, + ], }, }; diff --git a/awx/ui_next/src/screens/Template/shared/JobTemplateForm.test.jsx b/awx/ui_next/src/screens/Template/shared/JobTemplateForm.test.jsx index 071edaecf8..2cac31bea9 100644 --- a/awx/ui_next/src/screens/Template/shared/JobTemplateForm.test.jsx +++ b/awx/ui_next/src/screens/Template/shared/JobTemplateForm.test.jsx @@ -28,6 +28,10 @@ describe('', () => { name: 'qux', }, labels: { results: [{ name: 'Sushi', id: 1 }, { name: 'Major', id: 2 }] }, + credentials: [ + { id: 1, kind: 'cloud', name: 'Foo' }, + { id: 2, kind: 'ssh', name: 'Bar' }, + ], }, }; const mockInstanceGroups = [