Skip to content

Commit

Permalink
Add moduleForAcceptance
Browse files Browse the repository at this point in the history
  • Loading branch information
captain-enjoyable committed Jan 21, 2016
1 parent 474be00 commit e60117d
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 87 deletions.
8 changes: 5 additions & 3 deletions lib/ember-test-helpers.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import Ember from 'ember';
import TestModule from 'ember-test-helpers/test-module';
import Ember from 'ember';
import TestModule from 'ember-test-helpers/test-module';
import TestModuleForAcceptance from 'ember-test-helpers/test-module-for-acceptance';
import TestModuleForComponent from 'ember-test-helpers/test-module-for-component';
import TestModuleForModel from 'ember-test-helpers/test-module-for-model';
import TestModuleForModel from 'ember-test-helpers/test-module-for-model';
import { getContext, setContext } from 'ember-test-helpers/test-context';
import { setResolver } from 'ember-test-helpers/test-resolver';

Ember.testing = true;

export {
TestModule,
TestModuleForAcceptance,
TestModuleForComponent,
TestModuleForModel,
getContext,
Expand Down
135 changes: 135 additions & 0 deletions lib/ember-test-helpers/abstract-test-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { Klass } from 'klassy';
import { _setupAJAXHooks, _teardownAJAXHooks } from './wait';
import { setContext, unsetContext } from './test-context';

import Ember from 'ember';

export default Klass.extend({
init(description, options) {
this.description = description;
this.callbacks = options || {};

this.initSetupSteps();
this.initTeardownSteps();
},

setup(assert) {
return this.invokeSteps(this.setupSteps, this, assert).then(() => {
this.contextualizeCallbacks();
return this.invokeSteps(this.contextualizedSetupSteps, this.context, assert);
});
},

teardown(assert) {
return this.invokeSteps(this.contextualizedTeardownSteps, this.context, assert).then(() => {
return this.invokeSteps(this.teardownSteps, this, assert);
}).then(() => {
this.cache = null;
this.cachedCalls = null;
});
},

initSetupSteps() {
this.setupSteps = [];
this.contextualizedSetupSteps = [];

if (this.callbacks.beforeSetup) {
this.setupSteps.push( this.callbacks.beforeSetup );
delete this.callbacks.beforeSetup;
}

this.setupSteps.push(this.setupContext);
this.setupSteps.push(this.setupTestElements);
this.setupSteps.push(this.setupAJAXListeners);

if (this.callbacks.setup) {
this.contextualizedSetupSteps.push( this.callbacks.setup );
delete this.callbacks.setup;
}
},

invokeSteps(steps, context, assert) {
steps = steps.slice();

function nextStep() {
var step = steps.shift();
if (step) {
// guard against exceptions, for example missing components referenced from needs.
return new Ember.RSVP.Promise((resolve) => {
resolve(step.call(context, assert));
}).then(nextStep);
} else {
return Ember.RSVP.resolve();
}
}
return nextStep();
},

contextualizeCallbacks() {

},

initTeardownSteps() {
this.teardownSteps = [];
this.contextualizedTeardownSteps = [];

if (this.callbacks.teardown) {
this.contextualizedTeardownSteps.push( this.callbacks.teardown );
delete this.callbacks.teardown;
}

this.teardownSteps.push(this.teardownContext);
this.teardownSteps.push(this.teardownTestElements);
this.teardownSteps.push(this.teardownAJAXListeners);

if (this.callbacks.afterTeardown) {
this.teardownSteps.push( this.callbacks.afterTeardown );
delete this.callbacks.afterTeardown;
}
},

setupTestElements() {
if (Ember.$('#ember-testing').length === 0) {
Ember.$('<div id="ember-testing"/>').appendTo(document.body);
}
},

setupContext(options) {
var config = Ember.merge({
dispatcher: null,
inject: {}
}, options);

setContext(config);
},

setupAJAXListeners() {
_setupAJAXHooks();
},

teardownAJAXListeners() {
_teardownAJAXHooks();
},

teardownTestElements() {
Ember.$('#ember-testing').empty();

// Ember 2.0.0 removed Ember.View as public API, so only do this when
// Ember.View is present
if (Ember.View && Ember.View.views) {
Ember.View.views = {};
}
},

teardownContext() {
var context = this.context;
this.context = undefined;
unsetContext();

if (context && context.dispatcher && !context.dispatcher.isDestroyed) {
Ember.run(function() {
context.dispatcher.destroy();
});
}
}
});
30 changes: 30 additions & 0 deletions lib/ember-test-helpers/test-module-for-acceptance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import AbstractTestModule from './abstract-test-module';
import Ember from 'ember';
import { getContext } from './test-context';

export default AbstractTestModule.extend({
setupContext() {
this._super({ application: this.createApplication() });
},

teardownContext() {
Ember.run(() => {
getContext().application.destroy();
});

this._super();
},

createApplication() {
let { Application, config } = this.callbacks;
let application;

Ember.run(() => {
application = Application.create(config);
application.setupForTesting();
application.injectTestHelpers();
});

return application;
}
});
85 changes: 4 additions & 81 deletions lib/ember-test-helpers/test-module.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import Ember from 'ember';
import { getContext, setContext, unsetContext } from './test-context';
import { Klass } from 'klassy';
import { getContext } from './test-context';
import AbstractTestModule from './abstract-test-module';
import { getResolver } from './test-resolver';
import buildRegistry from './build-registry';
import hasEmberVersion from './has-ember-version';
import { _setupAJAXHooks, _teardownAJAXHooks } from './wait';

export default Klass.extend({
export default AbstractTestModule.extend({
init: function(subjectName, description, callbacks) {
// Allow `description` to be omitted, in which case it should
// default to `subjectName`
Expand Down Expand Up @@ -97,44 +96,6 @@ export default Klass.extend({
}
},

setup: function() {
var self = this;
return self.invokeSteps(self.setupSteps).then(function() {
self.contextualizeCallbacks();
return self.invokeSteps(self.contextualizedSetupSteps, self.context);
});
},

teardown: function() {
var self = this;
return self.invokeSteps(self.contextualizedTeardownSteps, self.context).then(function() {
return self.invokeSteps(self.teardownSteps);
}).then(function() {
self.cache = null;
self.cachedCalls = null;
});
},

invokeSteps: function(steps, _context) {
var context = _context;
if (!context) {
context = this;
}
steps = steps.slice();
function nextStep() {
var step = steps.shift();
if (step) {
// guard against exceptions, for example missing components referenced from needs.
return new Ember.RSVP.Promise(function(ok) {
ok(step.call(context));
}).then(nextStep);
} else {
return Ember.RSVP.resolve();
}
}
return nextStep();
},

setupContainer: function() {
if (this.isIntegration || this.isLegacy) {
this._setupIntegratedContainer();
Expand All @@ -151,16 +112,14 @@ export default Klass.extend({
return container.lookupFactory(subjectName);
};

setContext({
this._super({
container: this.container,
registry: this.registry,
factory: factory,
dispatcher: null,
register: function() {
var target = this.registry || this.container;
return target.register.apply(target, arguments);
},
inject: {}
});

var context = this.context = getContext();
Expand All @@ -180,16 +139,6 @@ export default Klass.extend({
}
},

setupTestElements: function() {
if (Ember.$('#ember-testing').length === 0) {
Ember.$('<div id="ember-testing"/>').appendTo(document.body);
}
},

setupAJAXListeners: function() {
_setupAJAXHooks();
},

teardownSubject: function() {
var subject = this.cache.subject;

Expand All @@ -207,32 +156,6 @@ export default Klass.extend({
});
},

teardownContext: function() {
var context = this.context;
this.context = undefined;
unsetContext();

if (context.dispatcher && !context.dispatcher.isDestroyed) {
Ember.run(function() {
context.dispatcher.destroy();
});
}
},

teardownTestElements: function() {
Ember.$('#ember-testing').empty();

// Ember 2.0.0 removed Ember.View as public API, so only do this when
// Ember.View is present
if (Ember.View && Ember.View.views) {
Ember.View.views = {};
}
},

teardownAJAXListeners: function() {
_teardownAJAXHooks();
},

defaultSubject: function(options, factory) {
return factory.create(options);
},
Expand Down
72 changes: 72 additions & 0 deletions tests/test-module-for-acceptance-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import Ember from 'ember';
import { TestModuleForAcceptance } from 'ember-test-helpers';
import test from 'tests/test-support/qunit-test';
import qunitModuleFor from 'tests/test-support/qunit-module-for';
import Resolver, { setResolverRegistry } from 'tests/test-support/resolver';
import { getContext } from 'ember-test-helpers/test-context';

function moduleForAcceptance(description, callbacks) {
qunitModuleFor(new TestModuleForAcceptance(description, callbacks));
}

let Application = Ember.Application.extend({
rootElement: '#ember-testing',
Resolver
});

moduleForAcceptance('TestModuleForAcceptance | Lifecycle', {
Application,

beforeSetup(assert) {
assert.expect(6);
assert.strictEqual(getContext(), undefined);

setResolverRegistry({
'router:main': Ember.Router.extend({ location: 'none' })
});
},

setup(assert) {
assert.ok(getContext().application instanceof Ember.Application);
},

teardown(assert) {
assert.ok(getContext().application instanceof Ember.Application);
},

afterTeardown(assert) {
assert.strictEqual(getContext(), undefined);
assert.strictEqual(Ember.$('#ember-testing').children().length, 0);
}
});

test('Lifecycle is correct', function() {
ok(true);
});

moduleForAcceptance('TestModuleForAcceptance | Basic acceptance tests', {
Application,

beforeSetup() {
setResolverRegistry({
'router:main': Ember.Router.extend({ location: 'none' }),
'template:index': Ember.Handlebars.compile('This is the index page.')
});
}
});

test('Basic acceptance test using instance test helpers', function() {
this.application.testHelpers.visit('/');

this.application.testHelpers.andThen(function() {
equal(Ember.$('#ember-testing').text(), 'This is the index page.');
});
});

test('Basic acceptance test using global test helpers', function() {
window.visit('/');

window.andThen(function() {
equal(Ember.$('#ember-testing').text(), 'This is the index page.');
});
});
Loading

0 comments on commit e60117d

Please sign in to comment.