github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/tests/helpers/glimmer-factory.js (about)

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