Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic implementation of new ember-qunit API. #286

Merged
merged 8 commits into from
Oct 17, 2017
30 changes: 29 additions & 1 deletion addon-test-support/ember-qunit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,42 @@ export { default as moduleFor } from './legacy-2-x/module-for';
export { default as moduleForComponent } from './legacy-2-x/module-for-component';
export { default as moduleForModel } from './legacy-2-x/module-for-model';
export { default as QUnitAdapter } from './adapter';
export { setResolver } from 'ember-test-helpers';
export { setResolver, render, clearRender, settled } from 'ember-test-helpers';
export { module, test, skip, only, todo } from 'qunit';
export { loadTests } from './test-loader';

import { loadTests } from './test-loader';
import Ember from 'ember';
import QUnit from 'qunit';
import QUnitAdapter from './adapter';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how did that ever work?! 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NO CLUE!?!?!?

import {
setupContext,
teardownContext,
setupRenderingContext,
teardownRenderingContext,
} from 'ember-test-helpers';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate import?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, no. The other one is an export { .... } from which does not bring the exported things into the current scope.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, right...


export function setupTest(hooks, options) {
hooks.beforeEach(function() {
setupContext(this, options);
});

hooks.afterEach(function() {
teardownContext(this);
});
}

export function setupRenderingTest(hooks, options) {
setupTest(hooks, options);

hooks.beforeEach(function() {
setupRenderingContext(this);
});

hooks.afterEach(function() {
teardownRenderingContext(this);
});
}

/**
Uses current URL configuration to setup the test container.
Expand Down
48 changes: 48 additions & 0 deletions tests/integration/setup-rendering-test-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { module, test } from 'qunit';
import Component from '@ember/component';
import { helper } from '@ember/component/helper';
import hbs from 'htmlbars-inline-precompile';
import { setupRenderingTest, render } from 'ember-qunit';
import { setResolverRegistry } from '../helpers/resolver';

module('setupRenderingTest tests', function(hooks) {
hooks.beforeEach(function() {
setResolverRegistry({});
});

setupRenderingTest(hooks);

test('can render a simple template', async function(assert) {
await render(hbs`<p>Hello!</p>`);

assert.equal(this.element.textContent, 'Hello!');
});

test('can invoke template only components', async function(assert) {
this.owner.register('template:components/template-only', hbs`template-only component here`);
await render(hbs`{{template-only}}`);

assert.equal(this.element.textContent, 'template-only component here');
});

test('can invoke JS only components', async function(assert) {
this.owner.register(
'component:js-only',
Component.extend({
classNames: ['js-only'],
})
);

await render(hbs`{{js-only}}`);

assert.ok(this.element.querySelector('.js-only'), 'element found for js-only component');
});

test('can invoke helper', async function(assert) {
this.owner.register('helper:jax', helper(([name]) => `${name}-jax`));

await render(hbs`{{jax "max"}}`);

assert.equal(this.element.textContent, 'max-jax');
});
});
71 changes: 71 additions & 0 deletions tests/integration/setup-test-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { module, test } from 'qunit';
import Service, { inject as injectService } from '@ember/service';
import Component from '@ember/component';
import { setupTest } from 'ember-qunit';
import hasEmberVersion from 'ember-test-helpers/has-ember-version';
import { setResolverRegistry } from '../helpers/resolver';

module('setupTest tests', function(hooks) {
hooks.beforeEach(function() {
setResolverRegistry({});
});

setupTest(hooks);

test('can be used for unit style testing', function(assert) {
this.owner.register(
'service:foo',
Service.extend({
someMethod() {
return 'hello thar!';
},
})
);

let subject = this.owner.lookup('service:foo');

assert.equal(subject.someMethod(), 'hello thar!');
});

test('can access a shared service instance', function(assert) {
this.owner.register('service:bar', Service.extend());
this.owner.register(
'service:foo',
Service.extend({
bar: injectService(),
someMethod() {
this.set('bar.someProp', 'derp');
},
})
);

let subject = this.owner.lookup('service:foo');
let bar = this.owner.lookup('service:bar');

assert.notOk(bar.get('someProp'), 'precond - initially undefined');

subject.someMethod();

assert.equal(bar.get('someProp'), 'derp', 'property updated');
});

test('can create a component instance for direct testing without a template', function(assert) {
this.owner.register(
'component:foo-bar',
Component.extend({
someMethod() {
return 'hello thar!';
},
})
);

let subject;
if (hasEmberVersion(2, 12)) {
subject = this.owner.lookup('component:foo-bar');
} else {
subject = this.owner._lookupFactory('component:foo-bar').create();
}

assert.equal(subject.someMethod(), 'hello thar!');
});
});