ft-webplatform/test/unit/mock-factories.js
Martin Donnelly 76920442b1 init
2017-09-28 13:04:16 +01:00

98 lines
3.0 KiB
JavaScript

import {bootstrap} from 'aurelia-bootstrapper';
import {StageComponent} from 'aurelia-testing';
import {getEndlessObject, getMockStore, wait} from '../../src/lib/test-utils.js';
/**
* Mock Component Config
* @typedef {Object} MockComponentConfig
* @property {string} markup - html markup
* @property {string|string[]} [resources=[]] inserted into StageComponent.withResources
* @property {Object} [mockStore={}] default store contents
* @property {Object} [mockBeans={}] beans contents, defaults to endless object
* @property {Object} [mockState={}] state to send to component.viewModel.mapState
* @property {Object} [mockAttributes={}] attribute values to be inserted into html
*/
/**
* @param {MockComponentConfig} defaultConfig
* @returns {{create: (function([MockComponentConfig]))}}
*/
export function mockComponentFactory(defaultConfig = {}) {
return {
/**
* @param {MockComponentConfig} [testConfig]
* @returns {[StageComponent,Promise,*]}
*/
create: (testConfig = {}) => {
let component = StageComponent
.withResources(testConfig.resources || defaultConfig.resources || [])
.inView(testConfig.markup || defaultConfig.markup);
component.mockStore = getMockStore(testConfig.mockStore || defaultConfig.mockStore || {});
component.mockBeans = testConfig.mockStore || defaultConfig.mockBeans || getEndlessObject();
component.configure = (aurelia) => {
aurelia.container.registerInstance('Store', component.mockStore);
aurelia.container.registerInstance('Beans', component.mockBeans);
aurelia.use.basicConfiguration();
};
component.boundTo(testConfig.mockAttributes || defaultConfig.mockAttributes || {});
let promise = component.create(bootstrap);
let state = testConfig.mockState || defaultConfig.mockState;
if (state) {
promise = promise
.then(() => {
component.viewModel.mapState(state);
})
.then(wait());
}
return [component, promise, component.viewModel];
}
};
}
/**
* @param {Object} mockState
* @param {StageComponent} component
* @param {Function} done
* @return {Promise}
*/
export function expectMockStateToPopulateComponent(mockState, component, done) {
let promise = component.create(bootstrap);
promise
.then(() => {
component.viewModel.mapState(mockState);
})
.then(wait())
.then(() => {
expect(component.viewModel.isPopulated).toBe(true);
done();
})
.catch((e)=>{
expect(e).toBeNull();
done();
});
return promise;
}
/**
* @param {StageComponent} component
* @param {Function} done
* @return {Promise}
*/
export function expectEmptyMockStateToNotPopulateComponent(component, done) {
let promise = component.create(bootstrap);
promise
.then(() => {
component.viewModel.mapState({});
})
.then(wait())
.then(() => {
expect(component.viewModel.isPopulated).toBe(false);
done();
})
.catch((e)=>{
expect(e).toBeNull();
done();
});
return promise;
}