open-nomad/ui/tests/helpers/glimmer-factory.js

36 lines
1.2 KiB
JavaScript
Raw Normal View History

// 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.
//
// 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
);
});
2021-12-28 14:45:20 +00:00
hooks.afterEach(function () {
delete this.createComponent;
});
}
// 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) {
return (args = {}) => {
const componentManager = owner.lookup('component-manager:glimmer');
const componentClass = owner.factoryFor(`component:${componentKey}`).class;
return componentManager.createComponent(componentClass, { named: args });
};
}