From b489976a2834183c0b2d045d526dca66ca966a01 Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Fri, 30 Jan 2015 23:11:55 -0500 Subject: [PATCH] Ensure that `setup` and `teardown` callbacks have correct context. Previously, the `setup` callback was invoked via `invokeSteps` (and therefore was bound to the `TestModule` instance itself). Now it is invoked with the same context as the tests themselves. * Ensure `setup` callback is invoked with the test context. * Ensure `teardown` callback is invoked with the test-module context. * Removed `beforeTeardown`. * Added `afterTeardown` which is invoked with the test-module context. * Simplify callback ordering tests. --- lib/ember-test-helpers/test-module.js | 27 ++++++---- tests/test-module-for-component-test.js | 21 ++++++++ tests/test-module-test.js | 65 +++++++++++++++++-------- 3 files changed, 84 insertions(+), 29 deletions(-) diff --git a/lib/ember-test-helpers/test-module.js b/lib/ember-test-helpers/test-module.js index ba166eaa7..c4f5fa144 100644 --- a/lib/ember-test-helpers/test-module.js +++ b/lib/ember-test-helpers/test-module.js @@ -36,6 +36,7 @@ export default Klass.extend({ initSetupSteps: function() { this.setupSteps = []; + this.contextualizedSetupSteps = []; if (this.callbacks.beforeSetup) { this.setupSteps.push( this.callbacks.beforeSetup ); @@ -47,42 +48,50 @@ export default Klass.extend({ this.setupSteps.push(this.setupTestElements); if (this.callbacks.setup) { - this.setupSteps.push( this.callbacks.setup ); + this.contextualizedSetupSteps.push( this.callbacks.setup ); delete this.callbacks.setup; } }, initTeardownSteps: function() { this.teardownSteps = []; + this.contextualizedTeardownSteps = []; - if (this.callbacks.beforeTeardown) { - this.teardownSteps.push( this.callbacks.beforeTeardown ); - delete this.callbacks.beforeTeardown; + if (this.callbacks.teardown) { + this.contextualizedTeardownSteps.push( this.callbacks.teardown ); + delete this.callbacks.teardown; } this.teardownSteps.push(this.teardownContainer); this.teardownSteps.push(this.teardownContext); this.teardownSteps.push(this.teardownTestElements); - if (this.callbacks.teardown) { - this.teardownSteps.push( this.callbacks.teardown ); - delete this.callbacks.teardown; + if (this.callbacks.afterTeardown) { + this.teardownSteps.push( this.callbacks.afterTeardown ); + delete this.callbacks.beforeTeardown; } }, setup: function() { this.invokeSteps(this.setupSteps); this.contextualizeCallbacks(); + this.invokeSteps(this.contextualizedSetupSteps, this.context); }, teardown: function() { + this.invokeSteps(this.contextualizedTeardownSteps, this.context); this.invokeSteps(this.teardownSteps); this.cache = null; }, - invokeSteps: function(steps) { + invokeSteps: function(steps, _context) { + var context = _context; + if (!context) { + context = this; + } + for (var i = 0, l = steps.length; i < l; i++) { - steps[i].call(this); + steps[i].call(context); } }, diff --git a/tests/test-module-for-component-test.js b/tests/test-module-for-component-test.js index 8b8c6b1bb..b77ed9743 100644 --- a/tests/test-module-for-component-test.js +++ b/tests/test-module-for-component-test.js @@ -185,3 +185,24 @@ test("$", function(){ equal($.trim(this.$('.color-name').text()), 'green'); equal($.trim(this.$().text()), 'Pretty Color: green'); }); + +moduleForComponent('pretty-color', 'component:pretty-color -- this.render in setup', { + beforeSetup: function() { + setupRegistry(); + }, + + setup: function() { + this.subject({ + name: 'red' + }); + + this.render(); + } +}); + +test("className", function(){ + // calling `this.$` or `this.subject.$` would + // force it to `render` initially, so we access the `ember-testing` + // div contents directly + equal($.trim($('#ember-testing').text()), 'Pretty Color: red'); +}); diff --git a/tests/test-module-test.js b/tests/test-module-test.js index 6167ab683..3f9bf6f23 100644 --- a/tests/test-module-test.js +++ b/tests/test-module-test.js @@ -17,44 +17,69 @@ function setupRegistry() { var a = 0; var b = 0; var beforeSetupOk = false; -var beforeTeardownOk = false; +var afterTeardownOk = false; + +var callbackOrder, setupContext, teardownContext, beforeSetupContext, afterTeardownContext; moduleFor('component:x-foo', 'TestModule callbacks', { beforeSetup: function() { - setupRegistry(); + beforeSetupContext = this; + callbackOrder = [ 'beforeSetup' ]; - beforeSetupOk = (a === 0); - b += 1; + setupRegistry(); }, setup: function() { - a += 1; - }, + setupContext = this; + callbackOrder.push('setup'); - beforeTeardown: function() { - beforeTeardownOk = (a === 1); - b -= 1; + ok(setupContext !== beforeSetupContext); }, teardown: function() { - a -= 1; + teardownContext = this; + callbackOrder.push('teardown'); + + deepEqual(callbackOrder, [ 'beforeSetup', 'setup', 'teardown']); + equal(setupContext, teardownContext); + }, + + afterTeardown: function() { + afterTeardownContext = this; + callbackOrder.push('afterTeardown'); + + deepEqual(callbackOrder, [ 'beforeSetup', 'setup', 'teardown', 'afterTeardown']); + equal(afterTeardownContext, beforeSetupContext); + ok(afterTeardownContext !== teardownContext); } }); -test("beforeSetup callback is called prior to any test setup", function() { - ok(beforeSetupOk); - equal(b, 1); +test("setup callbacks called in the correct order", function() { + deepEqual(callbackOrder, [ 'beforeSetup', 'setup' ]); }); -test("setup callback is called prior to test", function() { - equal(a, 1); +moduleFor('component:x-foo', 'component:x-foo -- setup context', { + beforeSetup: function() { + setupRegistry(); + }, + + setup: function() { + this.subject({ + name: 'Max' + }); + + this.container.register('service:blah', Ember.Object.extend({ + purpose: 'blabering' + })); + } }); -test("teardown callback is called after test", function() { - equal(a, 1); +test("subject can be initialized in setup", function() { + equal(this.subject().name, 'Max'); }); -test("beforeTeardown callback is called prior to any test teardown", function() { - ok(beforeTeardownOk); - equal(b, 1); +test("can lookup factory registered in setup", function() { + var service = this.container.lookup('service:blah'); + + equal(service.get('purpose'), 'blabering'); });