Created
January 29, 2020 11:55
-
-
Save xcambar/99a00d1eb03d45a47f2453a5dbe58201 to your computer and use it in GitHub Desktop.
Testing component initialisation and required arguments in Ember 3.15 (Octane)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Component from '@glimmer/component'; | |
export default class ThrowTestComponent extends Component { | |
constructor() { | |
super(...arguments) | |
if (!this.args.test) { | |
throw new Error('hello') | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { module, test, skip } from 'qunit'; | |
import { setupRenderingTest } from 'ember-qunit'; | |
import { render } from '@ember/test-helpers'; | |
import { hbs } from 'ember-cli-htmlbars'; | |
module('Integration | Component | throw-test', function(hooks) { | |
setupRenderingTest(hooks); | |
test('integration style: it works with an argument', async function(assert) { | |
await render(hbs`<ThrowTest @test="123"/>`); | |
assert.ok(true) | |
}); | |
// Can't get this one to work. Send help. | |
skip('integration style: it fails without an argument', async function(assert) { | |
assert.expect(1) | |
await render(hbs`<ThrowTest/>`); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { module, test } from 'qunit'; | |
import { setupTest } from 'ember-qunit'; | |
// Adapted from https://timgthomas.com/2019/11/unit-testing-glimmer-components/ | |
// Without it, Glimmer won't let me instanciate components with simple `new MyComponent(...)` | |
// | |
// START | |
import GlimmerComponentManager from 'hey/component-managers/glimmer'; | |
export default function createComponent(owner, lookupPath, named = {}) { | |
let { class: componentClass } = owner.factoryFor(lookupPath); | |
let componentManager = new GlimmerComponentManager(owner); | |
return componentManager.createComponent(componentClass, { named }); | |
} | |
//END | |
module('Unit | Component | throw-test', function(hooks) { | |
setupTest(hooks); | |
test('unit style: it works with an argument', async function(assert) { | |
/* let component = */ createComponent(this.owner, 'component:throw-test', { test: 'hello' }) | |
assert.ok(true, 'it worked') | |
}); | |
test('unit style: it fails without an argument', async function(assert) { | |
try { | |
/* let component = */ createComponent(this.owner, 'component:throw-test', {}) | |
} catch(e) { | |
assert.ok(true, 'it did not render') | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment