1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-01 08:21:15 +03:00
awx/__tests__/App.test.jsx

66 lines
2.3 KiB
React
Raw Normal View History

import React from 'react';
import { MemoryRouter } from 'react-router-dom';
import { shallow, mount } from 'enzyme';
import App from '../src/App';
import api from '../src/api';
import { API_LOGOUT, API_CONFIG } from '../src/endpoints';
2018-11-13 17:46:43 +03:00
import Dashboard from '../src/pages/Dashboard';
import Login from '../src/pages/Login';
describe('<App />', () => {
test('renders without crashing', () => {
const appWrapper = shallow(<App />);
expect(appWrapper.length).toBe(1);
});
test('renders login page when not authenticated', () => {
api.isAuthenticated = jest.fn();
api.isAuthenticated.mockReturnValue(false);
const appWrapper = mount(<MemoryRouter><App /></MemoryRouter>);
2018-10-25 04:15:08 +03:00
const login = appWrapper.find(Login);
expect(login.length).toBe(1);
const dashboard = appWrapper.find(Dashboard);
expect(dashboard.length).toBe(0);
});
test('renders dashboard when authenticated', () => {
api.isAuthenticated = jest.fn();
api.isAuthenticated.mockReturnValue(true);
const appWrapper = mount(<MemoryRouter><App /></MemoryRouter>);
2018-10-25 04:15:08 +03:00
const dashboard = appWrapper.find(Dashboard);
expect(dashboard.length).toBe(1);
const login = appWrapper.find(Login);
expect(login.length).toBe(0);
});
test('onNavToggle sets state.isNavOpen to opposite', () => {
2018-12-05 15:56:53 +03:00
const appWrapper = shallow(<App.WrappedComponent />);
expect(appWrapper.state().isNavOpen).toBe(true);
appWrapper.instance().onNavToggle();
expect(appWrapper.state().isNavOpen).toBe(false);
});
2018-11-06 20:25:36 +03:00
test('api.logout called from logout button', async () => {
const logOutButtonSelector = 'button[aria-label="Logout"]';
2018-11-13 17:53:36 +03:00
api.get = jest.fn().mockImplementation(() => Promise.resolve({}));
const appWrapper = mount(<MemoryRouter><App /></MemoryRouter>);
const logOutButton = appWrapper.find(logOutButtonSelector);
expect(logOutButton.length).toBe(1);
logOutButton.simulate('click');
2018-11-06 20:25:36 +03:00
appWrapper.setState({ activeGroup: 'foo', activeItem: 'bar' });
2018-11-13 17:53:36 +03:00
expect(api.get).toHaveBeenCalledWith(API_LOGOUT);
});
test('Componenet makes REST call to API_CONFIG endpoint when mounted', () => {
api.get = jest.fn().mockImplementation(() => Promise.resolve({}));
const appWrapper = shallow(<App.WrappedComponent />);
expect(api.get).toHaveBeenCalledTimes(1);
expect(api.get).toHaveBeenCalledWith(API_CONFIG);
});
});