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

Make setup and teardown of new API async. #236

Merged
merged 1 commit into from
Nov 5, 2017
Merged
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
9 changes: 8 additions & 1 deletion addon-test-support/setup-context.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { run } from '@ember/runloop';
import { run, next } from '@ember/runloop';
import { set, setProperties, get, getProperties } from '@ember/object';
import buildOwner from './build-owner';
import { _setupPromiseListeners } from './ext/rsvp';
Expand Down Expand Up @@ -59,6 +59,9 @@ export default function(context, options = {}) {
Ember.testing = true;
setContext(context);

return new Promise(resolve => {
// ensure "real" async and not "fake" RSVP based async
next(() => {
let resolver = options.resolver;
let owner = buildOwner(resolver);

Expand Down Expand Up @@ -106,4 +109,8 @@ export default function(context, options = {}) {

_setupAJAXHooks();
_setupPromiseListeners();

resolve(context);
});
});
}
7 changes: 7 additions & 0 deletions addon-test-support/setup-rendering-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export default function(context) {
},
];

return new Promise(resolve => {
// ensure "real" async and not "fake" RSVP based async
next(() => {
let { owner } = context;

let dispatcher = owner.lookup('event_dispatcher:main') || Ember.EventDispatcher.create();
Expand Down Expand Up @@ -186,4 +189,8 @@ export default function(context) {
});
});
};

resolve(context);
});
});
}
10 changes: 9 additions & 1 deletion addon-test-support/teardown-context.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { run } from '@ember/runloop';
import { run, next } from '@ember/runloop';
import { _teardownPromiseListeners } from './ext/rsvp';
import { _teardownAJAXHooks } from './settled';
import { Promise } from 'rsvp';
import Ember from 'ember';

export default function(context) {
return new Promise(resolve => {
// ensure "real" async and not "fake" RSVP based async
next(() => {
let { owner } = context;

_teardownPromiseListeners();
_teardownAJAXHooks();

run(owner, 'destroy');
Ember.testing = false;

resolve(context);
});
});
}
9 changes: 8 additions & 1 deletion addon-test-support/teardown-rendering-context.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { guidFor } from '@ember/object/internals';
import { run } from '@ember/runloop';
import { run, next } from '@ember/runloop';
import { Promise } from 'rsvp';
import { RENDERING_CLEANUP } from './setup-rendering-context';

export default function(context) {
return new Promise(resolve => {
// ensure "real" async and not "fake" RSVP based async
next(() => {
let guid = guidFor(context);
let destroyables = RENDERING_CLEANUP[guid];

Expand All @@ -11,4 +15,7 @@ export default function(context) {
}

delete RENDERING_CLEANUP[guid];
resolve(context);
});
});
}
10 changes: 5 additions & 5 deletions tests/unit/settled-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ module('settle', function(hooks) {
return;
}

hooks.beforeEach(function() {
setupContext(this);
setupRenderingContext(this);
hooks.beforeEach(async function() {
await setupContext(this);
await setupRenderingContext(this);

let { owner } = this;

Expand Down Expand Up @@ -185,8 +185,8 @@ module('settle', function(hooks) {
this.server.shutdown();
await settled();

teardownRenderingContext(this);
teardownContext(this);
await teardownRenderingContext(this);
await teardownContext(this);
});

test('it works when async exists in `init`', async function(assert) {
Expand Down
20 changes: 12 additions & 8 deletions tests/unit/setup-context-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ module('setupContext', function(hooks) {
});
});

hooks.afterEach(function() {
hooks.afterEach(async function() {
if (context) {
teardownContext(context);
await teardownContext(context);
context = undefined;
}
});

module('without options', function(hooks) {
hooks.beforeEach(function() {
context = {};
setupContext(context);
return setupContext(context);
});

test('it sets up this.owner', function(assert) {
Expand Down Expand Up @@ -205,24 +205,28 @@ module('setupContext', function(hooks) {
});

module('with custom options', function() {
test('it can specify a custom resolver', function(assert) {
test('it can specify a custom resolver', async function(assert) {
context = {};
let resolver = createCustomResolver({
'service:foo': Service.extend({ isFoo: 'maybe?' }),
});
setupContext(context, { resolver });
await setupContext(context, { resolver });
let { owner } = context;
let instance = owner.lookup('service:foo');
assert.equal(instance.isFoo, 'maybe?', 'uses the custom resolver');
});
});

test('Ember.testing', function(assert) {
test('Ember.testing', async function(assert) {
assert.notOk(Ember.testing, 'precond - Ember.testing is falsey before setup');

context = {};
setupContext(context);
let promise = setupContext(context);

assert.ok(Ember.testing, 'Ember.testing is truthy after setup');
assert.ok(Ember.testing, 'Ember.testing is truthy sync after setup');

await promise;

assert.ok(Ember.testing, 'Ember.testing is truthy async after setup');
});
});
12 changes: 6 additions & 6 deletions tests/unit/setup-rendering-context-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module('setupRenderingContext', function(hooks) {
return;
}

hooks.beforeEach(function() {
hooks.beforeEach(async function() {
setResolverRegistry({
'service:foo': Service.extend({ isFoo: true }),
'template:components/template-only': hbs`template-only component here`,
Expand All @@ -34,13 +34,13 @@ module('setupRenderingContext', function(hooks) {
'template:components/inner-comp': hbs`inner`,
});

setupContext(this);
setupRenderingContext(this);
await setupContext(this);
await setupRenderingContext(this);
});

hooks.afterEach(function() {
teardownRenderingContext(this);
teardownContext(this);
hooks.afterEach(async function() {
await teardownRenderingContext(this);
await teardownContext(this);
});

test('render exposes an `.element` property', async function(assert) {
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/teardown-context-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@ module('teardownContext', function(hooks) {
let context;
hooks.beforeEach(function() {
context = {};
setupContext(context);
return setupContext(context);
});

test('it destroys any instances created', function(assert) {
test('it destroys any instances created', async function(assert) {
let instance = context.owner.lookup('service:foo');
assert.notOk(instance.isDestroyed, 'precond - not destroyed');
assert.notOk(instance.isDestroying, 'precond - not destroying');

teardownContext(context);
await teardownContext(context);

assert.ok(instance.isDestroyed, 'destroyed');
assert.ok(instance.isDestroying, 'destroying');
});

test('it sets Ember.testing to false', function(assert) {
test('it sets Ember.testing to false', async function(assert) {
assert.ok(Ember.testing, 'precond - Ember.testing is truthy');

teardownContext(context);
await teardownContext(context);

assert.notOk(Ember.testing, 'Ember.testing is falsey after teardown');
});
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/teardown-rendering-context-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ module('setupRenderingContext', function(hooks) {
return;
}

hooks.beforeEach(function() {
setupContext(this);
setupRenderingContext(this);
hooks.beforeEach(async function() {
await setupContext(this);
await setupRenderingContext(this);
});

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

test('clears any attributes added to the ember-testing div', function(assert) {
test('clears any attributes added to the ember-testing div', async function(assert) {
let beforeTeardownEl = document.getElementById('ember-testing');
beforeTeardownEl.setAttribute('data-was-set', '');

Expand All @@ -31,7 +31,7 @@ module('setupRenderingContext', function(hooks) {
);
assert.ok(document.contains(beforeTeardownEl), 'precond - ember-testing element is in DOM');

teardownRenderingContext(this);
await teardownRenderingContext(this);

let afterTeardownEl = document.getElementById('ember-testing');

Expand Down