2020-10-15 17:35:23 +00:00
|
|
|
// Used in glimmer component unit tests. Glimmer components should typically
|
|
|
|
// be tested with integration tests, but occasionally individual methods or
|
|
|
|
// properties have logic that isn't coupled to rendering or the DOM and can
|
|
|
|
// be better tested in a unit fashion.
|
|
|
|
//
|
2020-10-15 09:18:55 +00:00
|
|
|
// Use like
|
|
|
|
//
|
|
|
|
// setupGlimmerComponentFactory(hooks, 'my-component')
|
|
|
|
//
|
|
|
|
// test('testing my component', function(assert) {
|
|
|
|
// const component = this.createComponent({ hello: 'world' });
|
|
|
|
// assert.equal(component.args.hello, 'world');
|
|
|
|
// });
|
|
|
|
export default function setupGlimmerComponentFactory(hooks, componentKey) {
|
2021-12-28 14:45:20 +00:00
|
|
|
hooks.beforeEach(function () {
|
2021-12-28 16:08:12 +00:00
|
|
|
this.createComponent = glimmerComponentInstantiator(
|
|
|
|
this.owner,
|
|
|
|
componentKey
|
|
|
|
);
|
2020-10-15 09:18:55 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
hooks.afterEach(function () {
|
2020-10-15 09:18:55 +00:00
|
|
|
delete this.createComponent;
|
|
|
|
});
|
|
|
|
}
|
2020-10-15 17:35:23 +00:00
|
|
|
|
|
|
|
// Look up the component class in the glimmer component manager and return a
|
|
|
|
// function to construct components as if they were functions.
|
|
|
|
function glimmerComponentInstantiator(owner, componentKey) {
|
2021-02-23 01:33:10 +00:00
|
|
|
return (args = {}) => {
|
2020-10-15 17:35:23 +00:00
|
|
|
const componentManager = owner.lookup('component-manager:glimmer');
|
|
|
|
const componentClass = owner.factoryFor(`component:${componentKey}`).class;
|
|
|
|
return componentManager.createComponent(componentClass, { named: args });
|
|
|
|
};
|
|
|
|
}
|