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

[WIP] New API Iteration #258

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion lib/ember-qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,36 @@ import moduleFor from 'ember-qunit/module-for';
import moduleForComponent from 'ember-qunit/module-for-component';
import moduleForModel from 'ember-qunit/module-for-model';
import QUnitAdapter from 'ember-qunit/adapter';
import { setResolver } from 'ember-test-helpers';
import setupTestFactory from 'ember-qunit/setup-test-factory';
import { buildOptionsWrapperFactory } from 'ember-qunit/qunit-module';
import {
setResolver,
TestModule,
TestModuleForIntegration,
TestModuleForAcceptance
} from 'ember-test-helpers';

export { module, test, skip, only, todo } from 'qunit';

const setupTest = setupTestFactory(TestModule);
const setupAcceptanceTest = setupTestFactory(TestModuleForAcceptance);
const setupIntegrationTest = setupTestFactory(TestModuleForIntegration);


const buildUnitOptions = buildOptionsWrapperFactory(TestModule);
const buildIntegrationOptions = buildOptionsWrapperFactory(TestModuleForIntegration);
const buildAcceptanceOptions = buildOptionsWrapperFactory(TestModuleForAcceptance);

export {
moduleFor,
moduleForComponent,
moduleForModel,
setupTest,
setupAcceptanceTest,
setupIntegrationTest,
buildUnitOptions,
buildIntegrationOptions,
buildAcceptanceOptions,
setResolver,
QUnitAdapter
};
90 changes: 62 additions & 28 deletions lib/ember-qunit/qunit-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ function callbackFor(name, callbacks) {
return callback;
}

export function createModule(Constructor, name, description, callbacks) {
if (!callbacks && typeof description === 'object') {
callbacks = description;
description = name;
export function wrapCallbacks(Constructor, _name, _callbacks) {
let callbacks = _callbacks;
let name = _name;
if (!callbacks && typeof name === 'object') {
callbacks = name;
name = '';
}

var before = callbackFor('before', callbacks);
Expand All @@ -29,34 +31,66 @@ export function createModule(Constructor, name, description, callbacks) {
var after = callbackFor('after', callbacks);

var module;
var moduleName = typeof description === 'string' ? description : name;
var options = {};

if (callbacks) {
for (let key in callbacks) {
options[key] = callbacks[key];
}
}

options.before = function() {
module = new Constructor(name || '', callbacks);

qunitModule(moduleName, {
before() {
// storing this in closure scope to avoid exposing these
// private internals to the test context
module = new Constructor(name, description, callbacks);
if (before) {
return before.apply(this, arguments);
},
}
};

beforeEach() {
// provide the test context to the underlying module
module.setContext(this);
options.beforeEach = function() {
// provide the test context to the underlying module
module.setContext(this);

return module.setup(...arguments).then(() => {
return module.setup(...arguments).then(() => {
if (beforeEach) {
return beforeEach.apply(this, arguments);
});
},

afterEach() {
let result = afterEach.apply(this, arguments);
return Ember.RSVP.resolve(result).then(() => module.teardown(...arguments));
},

after() {
let result = after.apply(this, arguments);
module = null;
return result;
}
});
};

options.afterEach = function() {
let result;

if (afterEach) {
result = afterEach.apply(this, arguments);
}
});

return Ember.RSVP.resolve(result).then(() => module.teardown(...arguments));
};

options.after = function() {
let result;

if (before) {
result = after.apply(this, arguments);
}

module = null;

return result;
};

return options;
}

export function buildOptionsWrapperFactory(Constructor) {
return function optionsWrapperFactory(name, callbacks) {
return wrapCallbacks(Constructor, name, callbacks);
};
}

export function createModule(Constructor, name, description, callbacks) {
let options = wrapCallbacks(Constructor, name, callbacks || description);

qunitModule(name, options);
}
24 changes: 24 additions & 0 deletions lib/ember-qunit/setup-test-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export default function(Constructor) {
return function setupTest(hooks, options) {
let module;

hooks.before(function() {
// TODO: should we actually have this in the API
module = new Constructor('', options);
});

hooks.beforeEach(function() {
module.setContext(this);

return module.setup(...arguments);
});

hooks.afterEach(function() {
return module.teardown(...arguments);
});

hooks.after(function() {
module = null;
});
};
}
17 changes: 17 additions & 0 deletions tests/build-integration-options-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* global setTimeout */

import Ember from 'ember';
import { module, test, buildIntegrationOptions } from 'ember-qunit';
import setupRegistry from './test-support/setup-registry';

module('component:x-foo', buildIntegrationOptions({
before: setupRegistry
}));

test('renders', function(assert) {
assert.expect(1);

this.render(Ember.Handlebars.compile(`{{pretty-color name="red"}}`));

assert.equal(this.$('.color-name').text(), 'red');
});
18 changes: 18 additions & 0 deletions tests/setup-integration-test-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* global setTimeout */

import Ember from 'ember';
import { module, test, setupIntegrationTest } from 'ember-qunit';
import setupRegistry from './test-support/setup-registry';

module('component:x-foo', function(hooks) {
setupIntegrationTest(hooks);

test('renders', function(assert) {
setupRegistry();
assert.expect(1);

this.render(Ember.Handlebars.compile(`{{pretty-color name="red"}}`));

assert.equal(this.$('.color-name').text(), 'red');
});
});
18 changes: 18 additions & 0 deletions tests/test-support/setup-registry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Ember from 'ember';
import { setResolverRegistry } from './resolver';

var PrettyColor = Ember.Component.extend({
classNames: ['pretty-color'],
attributeBindings: ['style'],
style: function(){
return 'color: ' + this.get('name') + ';';
}.property('name')
});

export default function setupRegistry() {
setResolverRegistry({
'component:x-foo': Ember.Component.extend(),
'component:pretty-color': PrettyColor,
'template:components/pretty-color': Ember.Handlebars.compile('Pretty Color: <span class="color-name">{{name}}</span>')
});
}