-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathtest-utils.jsx
More file actions
98 lines (83 loc) · 2.88 KB
/
test-utils.jsx
File metadata and controls
98 lines (83 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { Provider } from 'react-redux';
import { render, screen } from '@testing-library/react';
import PropTypes from 'prop-types';
import { createStore, applyMiddleware } from 'redux';
import { thunk } from 'redux-thunk';
import { createTheme, ThemeProvider } from '@mui/material/styles';
import { I18nextProvider } from 'react-i18next';
import createRootReducer from '../../src/state/reducers/rootReducer';
import settings from '../../src/config/settings';
import createI18nInstance from '../../src/i18n';
import { failIfErrorDialogPresent, safeFindByRole, safeFindAllByRole } from './safe-queries';
/** Mirador viewer setup for Integration tests */
import { viewer as miradorViewer } from '../../src/index';
export * from '@testing-library/react';
export { renderWithProviders as render, safeFindByRole };
const rootReducer = createRootReducer();
const theme = createTheme(settings.theme);
const i18n = createI18nInstance();
/**
* Hook up our rendered object to redux
*/
function renderWithProviders(
ui,
{
preloadedState = {},
// Automatically create a store instance if no store was passed in
store = createStore(rootReducer, preloadedState, applyMiddleware(thunk)),
...renderOptions
} = {},
) {
/** :nodoc: */
function Wrapper({ children }) {
return (
<I18nextProvider i18n={i18n}>
<ThemeProvider theme={theme}>
<Provider store={store}>
{children}
</Provider>
</ThemeProvider>
</I18nextProvider>
);
}
Wrapper.propTypes = {
children: PropTypes.node.isRequired,
};
const rendered = render(ui, { wrapper: Wrapper, ...renderOptions });
// Return an object with the store and all of RTL's query functions
return {
store,
...rendered,
rerender: (newUi, options) => render(newUi, { container: rendered.container, wrapper: Wrapper, ...options }),
};
}
/** adds a mirador viewer to the DOM */
const setupMiradorViewer = async (config, plugins) => {
const viewer = miradorViewer({ ...config, id: undefined }, plugins);
render(
<div
data-testid="mirador"
style={{
bottom: 0, left: 0, position: 'absolute', right: 0, top: 0,
}}
>
{viewer.render()}
</div>,
);
// Return the instance so we can reference it
return viewer;
};
/** */
export const setupIntegrationTestViewer = (config, plugins) => {
beforeEach(async (context) => {
const miradorInstance = await setupMiradorViewer(config, plugins);
context.miradorInstance = miradorInstance; // eslint-disable-line no-param-reassign
// Wait for the viewer to render
expect(await screen.findByTestId('mirador')).toBeInTheDocument();
expect(await screen.findByLabelText('Workspace')).toBeInTheDocument();
await failIfErrorDialogPresent({ waitForRole: 'main' });
if ((config.windows || []).length > 0) {
await safeFindAllByRole('region', { name: /Window:/i });
}
});
};