update content loading and error handling

unwind error handling

use auth cookie as source of truth, fetch config only when authenticated
This commit is contained in:
Jake McDermott
2019-05-09 15:59:43 -04:00
parent 534418c81a
commit e72f0bcfd4
50 changed files with 4721 additions and 4724 deletions

View File

@@ -3,12 +3,10 @@
* derived from https://lingui.js.org/guides/testing.html
*/
import React from 'react';
import { shape, object, string, arrayOf, func } from 'prop-types';
import { shape, object, string, arrayOf } from 'prop-types';
import { mount, shallow } from 'enzyme';
import { I18nProvider } from '@lingui/react';
import { ConfigProvider } from '../src/contexts/Config';
import { _NetworkProvider } from '../src/contexts/Network';
import { RootDialogProvider } from '../src/contexts/RootDialog';
const language = 'en-US';
const intlProvider = new I18nProvider(
@@ -36,8 +34,6 @@ const defaultContexts = {
ansible_version: null,
custom_virtualenvs: [],
version: null,
custom_logo: null,
custom_login_info: null,
toJSON: () => '/config/'
},
router: {
@@ -69,30 +65,19 @@ const defaultContexts = {
},
toJSON: () => '/router/',
},
network: {
handleHttpError: () => {},
},
dialog: {}
};
function wrapContexts (node, context) {
const { config, network, dialog } = context;
const { config } = context;
class Wrap extends React.Component {
render () {
// eslint-disable-next-line react/no-this-in-sfc
const { children, ...props } = this.props;
const component = React.cloneElement(children, props);
return (
<RootDialogProvider value={dialog}>
<_NetworkProvider value={network}>
<ConfigProvider
value={config}
i18n={defaultContexts.linguiPublisher.i18n}
>
{component}
</ConfigProvider>
</_NetworkProvider>
</RootDialogProvider>
<ConfigProvider value={config}>
{component}
</ConfigProvider>
);
}
}
@@ -131,8 +116,6 @@ export function mountWithContexts (node, options = {}) {
ansible_version: string,
custom_virtualenvs: arrayOf(string),
version: string,
custom_logo: string,
custom_login_info: string,
}),
router: shape({
route: shape({
@@ -141,36 +124,31 @@ export function mountWithContexts (node, options = {}) {
}).isRequired,
history: shape({}).isRequired,
}),
network: shape({
handleHttpError: func.isRequired,
}),
dialog: shape({
title: string,
setRootDialogMessage: func,
clearRootDialogMessage: func,
}),
...options.childContextTypes
};
return mount(wrapContexts(node, context), { context, childContextTypes });
}
/**
* Wait for element to exist.
* Wait for element(s) to achieve a desired state.
*
* @param[wrapper] - A ReactWrapper instance
* @param[selector] - The selector of the element to wait for.
* @param[selector] - The selector of the element(s) to wait for.
* @param[callback] - Callback to poll - by default this checks for a node count of 1.
*/
export function waitForElement (wrapper, selector) {
export function waitForElement (wrapper, selector, callback = el => el.length === 1) {
const interval = 100;
return new Promise((resolve, reject) => {
let attempts = 30;
(function pollElement () {
wrapper.update();
if (wrapper.exists(selector)) {
return resolve(wrapper.find(selector));
const el = wrapper.find(selector);
if (callback(el)) {
return resolve(el);
}
if (--attempts <= 0) {
return reject(new Error(`Element not found using ${selector}`));
const message = `Expected condition for <${selector}> not met: ${callback.toString()}`;
return reject(new Error(message));
}
return setTimeout(pollElement, interval);
}());