onLoad: (details: { key: string; name: string }) => void;
}
+interface State {
+ loading: boolean;
+}
+
export default class WorkspaceRuleViewer extends React.PureComponent<Props> {
+ state: State = { loading: true };
+
+ componentDidUpdate(prevProps: Props) {
+ if (prevProps.rule.key !== this.props.rule.key) {
+ this.setState({ loading: true });
+ }
+ }
+
handleClose = () => {
this.props.onClose(this.props.rule.key);
};
handleLoaded = (rule: { name: string }) => {
this.props.onLoad({ key: this.props.rule.key, name: rule.name });
+ // Allow time for the actual rendering, and the browser to pick it up.
+ setTimeout(() => {
+ this.setState({ loading: false });
+ }, 1000);
};
render() {
const { rule } = this.props;
+ const { loading } = this.state;
return (
<div className="workspace-viewer">
<WorkspaceRuleTitle rule={rule} />
</WorkspaceHeader>
- <div className="workspace-viewer-container" style={{ height: this.props.height }}>
+ <div
+ aria-busy={loading}
+ aria-live="polite"
+ className="workspace-viewer-container"
+ style={{ height: this.props.height }}>
<WorkspaceRuleDetails
onLoad={this.handleLoaded}
organizationKey={rule.organization}
import { shallow } from 'enzyme';
import WorkspaceRuleViewer, { Props } from '../WorkspaceRuleViewer';
+jest.useFakeTimers();
+
it('should render', () => {
expect(shallowRender()).toMatchSnapshot();
});
+it('should correctly mark the content as busy loading (aria)', () => {
+ const rule = { key: 'foo', name: 'Foo', organization: 'org' };
+ const wrapper = shallowRender({ rule });
+ const instance = wrapper.instance();
+ const container = () => wrapper.find('.workspace-viewer-container');
+
+ expect(container().prop('aria-busy')).toBe(true);
+
+ instance.handleLoaded(rule);
+ jest.runAllTimers();
+ wrapper.update();
+ expect(container().prop('aria-busy')).toBe(false);
+
+ const newRule = { key: 'bar', name: 'Bar', organization: 'org' };
+ wrapper.setProps({ rule: newRule }).update();
+ expect(container().prop('aria-busy')).toBe(true);
+
+ instance.handleLoaded(newRule);
+ jest.runAllTimers();
+ wrapper.update();
+ expect(container().prop('aria-busy')).toBe(false);
+});
+
it('should close', () => {
const onClose = jest.fn();
const wrapper = shallowRender({ onClose });
});
function shallowRender(props?: Partial<Props>) {
- const rule = { key: 'foo', organization: 'org' };
- return shallow(
+ const rule = { key: 'foo', name: 'Foo', organization: 'org' };
+ return shallow<WorkspaceRuleViewer>(
<WorkspaceRuleViewer
height={300}
onClose={jest.fn()}