github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/tests/helpers/glimmer-factory.js (about)

     1  // Used in glimmer component unit tests. Glimmer components should typically
     2  // be tested with integration tests, but occasionally individual methods or
     3  // properties have logic that isn't coupled to rendering or the DOM and can
     4  // be better tested in a unit fashion.
     5  //
     6  // Use like
     7  //
     8  // setupGlimmerComponentFactory(hooks, 'my-component')
     9  //
    10  // test('testing my component', function(assert) {
    11  //   const component = this.createComponent({ hello: 'world' });
    12  //   assert.equal(component.args.hello, 'world');
    13  // });
    14  export default function setupGlimmerComponentFactory(hooks, componentKey) {
    15    hooks.beforeEach(function() {
    16      this.createComponent = glimmerComponentInstantiator(this.owner, componentKey);
    17    });
    18  
    19    hooks.afterEach(function() {
    20      delete this.createComponent;
    21    });
    22  }
    23  
    24  // Look up the component class in the glimmer component manager and return a
    25  // function to construct components as if they were functions.
    26  function glimmerComponentInstantiator(owner, componentKey) {
    27    return (args = {}) => {
    28      const componentManager = owner.lookup('component-manager:glimmer');
    29      const componentClass = owner.factoryFor(`component:${componentKey}`).class;
    30      return componentManager.createComponent(componentClass, { named: args });
    31    };
    32  }