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

add more unit test coverage for index.jsx

This commit is contained in:
Jake McDermott 2019-01-07 07:26:21 -05:00
parent cb0367ac28
commit 4936238344
No known key found for this signature in database
GPG Key ID: 9A6F084352C3A0B7
2 changed files with 31 additions and 14 deletions

View File

@ -1,11 +1,11 @@
import { mount } from 'enzyme';
import { main } from '../src/index';
import { main, getLanguage } from '../src/index';
const render = template => mount(template);
const data = { custom_logo: 'foo', custom_login_info: '' }
describe('index.jsx', () => {
test('initialization', async (done) => {
test('login loads when unauthenticated', async (done) => {
const isAuthenticated = () => false;
const getRoot = jest.fn(() => Promise.resolve({ data }));
@ -13,7 +13,7 @@ describe('index.jsx', () => {
const wrapper = await main(render, api);
expect(api.getRoot).toHaveBeenCalled();
expect(wrapper.find('Dashboard')).toHaveLength(0);
expect(wrapper.find('App')).toHaveLength(0);
expect(wrapper.find('Login')).toHaveLength(1);
const { src } = wrapper.find('Login Brand img').props();
@ -22,7 +22,7 @@ describe('index.jsx', () => {
done();
});
test('dashboard is loaded when authenticated', async (done) => {
test('app loads when authenticated', async (done) => {
const isAuthenticated = () => true;
const getRoot = jest.fn(() => Promise.resolve({ data }));
@ -30,9 +30,22 @@ describe('index.jsx', () => {
const wrapper = await main(render, api);
expect(api.getRoot).toHaveBeenCalled();
expect(wrapper.find('Dashboard')).toHaveLength(1);
expect(wrapper.find('App')).toHaveLength(1);
expect(wrapper.find('Login')).toHaveLength(0);
wrapper.find('header a').simulate('click');
wrapper.update();
expect(wrapper.find('App')).toHaveLength(1);
expect(wrapper.find('Login')).toHaveLength(0);
done();
});
test('getLanguage returns the expected language code', () => {
expect(getLanguage({ languages: ['es-US'] })).toEqual('es');
expect(getLanguage({ languages: ['es-US'], language: 'fr-FR', userLanguage: 'en-US' })).toEqual('es');
expect(getLanguage({ language: 'fr-FR', userLanguage: 'en-US' })).toEqual('fr');
expect(getLanguage({ userLanguage: 'en-US' })).toEqual('en');
});
});

View File

@ -60,21 +60,25 @@ const http = axios.create({ xsrfCookieName: 'csrftoken', xsrfHeaderName: 'X-CSRF
// see: https://developer.mozilla.org/en-US/docs/Web/API/Navigator
//
const language = (navigator.languages && navigator.languages[0])
|| navigator.language
|| navigator.userLanguage;
const languageWithoutRegionCode = language.toLowerCase().split(/[_-]+/)[0];
const catalogs = { en, ja };
export function getLanguage (nav) {
const language = (nav.languages && nav.languages[0]) || nav.language || nav.userLanguage;
const languageWithoutRegionCode = language.toLowerCase().split(/[_-]+/)[0];
return languageWithoutRegionCode;
};
//
// Function Main
//
export async function main (render, api) {
const catalogs = { en, ja };
const language = getLanguage(navigator);
const el = document.getElementById('app');
// fetch additional config from server
const { data: { custom_logo, custom_login_info } } = await api.getRoot();
const defaultRedirect = () => (<Redirect to="/home" />);
const loginRoutes = (
<Switch>
<Route
@ -94,7 +98,7 @@ export async function main (render, api) {
return render(
<HashRouter>
<I18nProvider
language={languageWithoutRegionCode}
language={language}
catalogs={catalogs}
>
<I18n>
@ -102,8 +106,8 @@ export async function main (render, api) {
<Background>
{!api.isAuthenticated() ? loginRoutes : (
<Switch>
<Route path="/login" render={() => (<Redirect to="/home" />)} />
<Route exact path="/" render={() => (<Redirect to="/home" />)} />
<Route path="/login" render={defaultRedirect} />
<Route exact path="/" render={defaultRedirect} />
<Route
render={() => (
<App