Config applicationFolder as root to resolve environment file (used for genereate spa resonse content).
Usage
This is Jest example of simple integration test, that loads the IMA.js app homepage and mocks all http get requests.
`javascript
import { initImaApp, clearImaApp } from '@ima/plugin-testing-integration';describe('Integration tests', () => {
const response = {
// Response mock
};
let app;
let http;
beforeEach(async () => {
app = await initImaApp({
initBindApp: (ns, oc) => {
http = oc.get('$Http');
// Mock http.get method so the application
// wont make any external requests
// and return mocked response
http.get = jest.fn().mockReturnValue(
Promise.resolve(response)
);
}
});
// Load some specific application page
await app.oc.get('$Router').route('/');
});
afterEach(() => {
clearImaApp(app);
});
it('can load homepage', () => {
// Check that a http get method has been called
expect(http.get).toHaveBeenCalled();
// You can use document, or window to
// make some validation of dom content
expect(document.title).toEqual('IMA.js-core');
});
});
`Usage with Enzyme
You can boost the integration tests by using Enzyme mount method to render the IMA.js application.
Note, that Enzyme PageRenderer extends default IMA PageRenderer. If you choose to override PageRenderer in your tests, it may cause some unexpected behavior.
API
EnzymePageRenderer extends the app object returned by initImaApp method with following values.
$3
Returns:
- enzyme.ReactWrapper|enzyme.ReactWrapper[] Return value of enzyme.mount()Function returning Enzyme ReactWrapper, or array of Enzyme ReactWrappers (in case, that there are multiple ReactDOM.render and ReactDOM.hydrate calls inside PageRenderer, but this should not happen in current setup).
$3
Use
setConfigjavascript
const { setConfig } = require('@ima/plugin-testing-integration');
const EnzymePageRenderer = require('@ima/plugin-testing-integration/EnzymePageRenderer');setConfig({
TestPageRenderer: EnzymePageRenderer
});
`For example with jest test runner you can use jest setupFiles.
$3
`javascript
describe('Home page', () => {
let app; beforeEach(async () => {
app = await initImaApp();
// Load some specific application page
await app.oc.get('$Router').route('/');
});
afterEach(() => {
clearImaApp(app);
});
it('can update input value', () => {
const value = 'Input edited by enzyme';
// Get the Enzyme ReactWrapper
const wrapper = app.wrapper();
// Find some input on the page using Enzyme find method
const input = wrapper.find('input').at(0);
// Simulate value change using Enzyme simulate method
input.simulate('change', { target: { value } });
// Check, that component props are updated
expect(input.props.value).toEqual(value);
});
});
`Integration tests described
Integration test needs to run IMA application to test complex logic across app components in real applicattion runtime. For example Create IMA.js App
This plugin contains all necesary functionality to run IMA app:
$3
Contains basic IMA config, which can be overriden by argument of
setConfig function mentioned above
$3
Resolves boot components to extend native init methods
initSettingsThere are two main methods described in api section
$3
This main method performs following steps to initializes app to be as close as possibe to real IMA app inside real browser:
- resolves final app config
- prepare JSDom environment to run IMA app and sets all necessary global variables
- overrides all native timer functions to save timers into internal storage (so these can be cleared on app exit)
- runs
prebootScript from config if defined and awaits its completion
- creates IMA app object
- resolves IMA app boot config
- awaits for load state of client app
- boots IMA client app
- initializes IMA router inside JSDom environment
$3
This method sets back all native timer functions, clears all pending timers used in integration test run, also calls
unAop