mirror of
https://github.com/ansible/awx.git
synced 2026-02-28 16:28:43 -03:30
refactoring and updating tests
This commit is contained in:
79
__tests__/components/RoutedTabs.test.jsx
Normal file
79
__tests__/components/RoutedTabs.test.jsx
Normal file
@@ -0,0 +1,79 @@
|
||||
import React from 'react';
|
||||
import { mount } from 'enzyme';
|
||||
|
||||
import { Router } from 'react-router-dom';
|
||||
import { createMemoryHistory } from 'history';
|
||||
import { I18nProvider } from '@lingui/react';
|
||||
import RoutedTabs from '../../src/components/Tabs/RoutedTabs';
|
||||
import { DonateIcon } from '@patternfly/react-icons';
|
||||
|
||||
let wrapper;
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
const tabs = [
|
||||
{ name: 'Details', link: 'organizations/19/details', id: 0 },
|
||||
{ name: 'Access', link: 'organizations/19/access', id: 1 },
|
||||
{ name: 'Teams', link: 'organizations/19/teams', id: 2 }
|
||||
];
|
||||
|
||||
const history = createMemoryHistory({
|
||||
history: {
|
||||
location: {
|
||||
pathname: '/organizations/19/details'
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
describe('<RoutedTabs />', () => {
|
||||
test('RoutedTabs renders successfully', () => {
|
||||
wrapper = mount(
|
||||
<I18nProvider>
|
||||
<Router history={history}>
|
||||
<RoutedTabs
|
||||
tabsArray={tabs}
|
||||
/>
|
||||
</Router>
|
||||
</I18nProvider>
|
||||
).find('RoutedTabs');
|
||||
});
|
||||
|
||||
test('the correct tab is rendered', async () => {
|
||||
const currentTab = 'organizations/1/details';
|
||||
wrapper = mount(
|
||||
<I18nProvider>
|
||||
<Router history={history}>
|
||||
<RoutedTabs
|
||||
tabsArray={tabs}
|
||||
location={currentTab}
|
||||
/>
|
||||
</Router>
|
||||
</I18nProvider>
|
||||
).find('RoutedTabs');
|
||||
wrapper.find('button').at(2).simulate('click');
|
||||
wrapper.update();
|
||||
expect(history.location.pathname).toEqual('/organizations/19/access');
|
||||
});
|
||||
|
||||
test('Given a URL the correct tab is displayed', async (done) => {
|
||||
const currentTab = createMemoryHistory({
|
||||
initialEntries: ['/organizations/19/teams'],
|
||||
});
|
||||
wrapper = mount(
|
||||
<I18nProvider>
|
||||
<Router history={currentTab}>
|
||||
<RoutedTabs
|
||||
tabsArray={tabs}
|
||||
/>
|
||||
</Router>
|
||||
</I18nProvider>
|
||||
).find('RoutedTabs');
|
||||
setImmediate(() => {
|
||||
wrapper.find('Tabs').prop('onSelect')({}, 2);
|
||||
const selectedTab = wrapper.find('li').get(2).props.className;
|
||||
expect(selectedTab).toBe('pf-c-tabs__item pf-m-current');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -3,56 +3,19 @@ import { mount } from 'enzyme';
|
||||
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
import { I18nProvider } from '@lingui/react';
|
||||
import Organization, { _Organization } from '../../../../../src/pages/Organizations/screens/Organization/Organization';
|
||||
import Organization from '../../../../../src/pages/Organizations/screens/Organization/Organization';
|
||||
|
||||
describe('<OrganizationView />', () => {
|
||||
test('initially renders succesfully', () => {
|
||||
const spy = jest.spyOn(_Organization.prototype, 'checkLocation');
|
||||
mount(
|
||||
<I18nProvider>
|
||||
<MemoryRouter initialEntries={['/organizations/1']} initialIndex={0}>
|
||||
<_Organization
|
||||
<Organization
|
||||
match={{ path: '/organizations/:id', url: '/organizations/1' }}
|
||||
location={{ search: '', pathname: '/organizations/1' }}
|
||||
/>
|
||||
</MemoryRouter>
|
||||
</I18nProvider>
|
||||
);
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('handleTabSelect renders the correct tab', async () => {
|
||||
const currentTab = 'organizations/19/access';
|
||||
const spy = jest.spyOn(_Organization.prototype, 'handleTabSelect');
|
||||
const wrapper = mount(
|
||||
<I18nProvider>
|
||||
<MemoryRouter initialEntries={['/organizations/1']} initialIndex={0}>
|
||||
<Organization location={currentTab} />
|
||||
</MemoryRouter>
|
||||
</I18nProvider>
|
||||
).find('Organization');
|
||||
wrapper.find('button').at(2).simulate('click');
|
||||
setImmediate(async () => {
|
||||
wrapper.setState({ activeTabKey: 1 });
|
||||
});
|
||||
wrapper.update();
|
||||
expect(spy).toBeCalled();
|
||||
expect(wrapper.state('activeTabKey')).toBe(1);
|
||||
});
|
||||
|
||||
test('checkLocation renders proper state when new tab is selected', async () => {
|
||||
const currentTab = 'organizations/19/access';
|
||||
const wrapper = mount(
|
||||
<I18nProvider>
|
||||
<MemoryRouter initialEntries={['/organizations/1']} initialIndex={0}>
|
||||
<Organization location={currentTab} />
|
||||
</MemoryRouter>
|
||||
</I18nProvider>
|
||||
).find('Organization');
|
||||
setImmediate(async () => {
|
||||
wrapper.setState({ activeTabKey: 1 });
|
||||
});
|
||||
wrapper.find('button').at(3).simulate('click');
|
||||
expect(wrapper.state('activeTabKey')).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user