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 pauseTest and resumeTest functionality. #233

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/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ export { default as TestModuleForComponent } from './legacy-0-6-x/test-module-fo
export { default as TestModuleForModel } from './legacy-0-6-x/test-module-for-model';

export { setResolver } from './resolver';
export { default as setupContext, getContext, setContext, unsetContext } from './setup-context';
export {
default as setupContext,
getContext,
setContext,
unsetContext,
pauseTest,
resumeTest,
} from './setup-context';
export { default as teardownContext } from './teardown-context';
export { default as setupRenderingContext, render, clearRender } from './setup-rendering-context';
export { default as teardownRenderingContext } from './teardown-rendering-context';
Expand Down
43 changes: 43 additions & 0 deletions addon-test-support/setup-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import buildOwner from './build-owner';
import { _setupPromiseListeners } from './ext/rsvp';
import { _setupAJAXHooks } from './settled';
import Ember from 'ember';
import { Promise } from 'rsvp';
import { assert } from '@ember/debug';
import global from './global';

let __test_context__;

Expand All @@ -19,6 +22,30 @@ export function unsetContext() {
__test_context__ = undefined;
}

export function pauseTest() {
let context = getContext();

if (!context || typeof context.pauseTest !== 'function') {
throw new Error(
'Cannot call `pauseTest` without having first called `setupTest` or `setupRenderingTest`.'
);
}

return context.pauseTest();
}

export function resumeTest() {
let context = getContext();

if (!context || typeof context.resumeTest !== 'function') {
throw new Error(
'Cannot call `resumeTest` without having first called `setupTest` or `setupRenderingTest`.'
);
}

return context.resumeTest();
}

/*
* Responsible for:
*
Expand Down Expand Up @@ -61,6 +88,22 @@ export default function(context, options = {}) {
return getProperties(context, args);
};

let resume;
context.resumeTest = function resumeTest() {
assert('Testing has not been paused. There is nothing to resume.', resume);
resume();
global.resumeTest = resume = undefined;
};

context.pauseTest = function pauseTest() {
console.info('Testing paused. Use `resumeTest()` to continue.'); // eslint-disable-line no-console

return new Promise(resolve => {
resume = resolve;
global.resumeTest = resumeTest;
}, 'TestAdapter paused promise');
};

_setupAJAXHooks();
_setupPromiseListeners();
}
78 changes: 77 additions & 1 deletion tests/unit/setup-context-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { module, test } from 'qunit';
import Service, { inject as injectService } from '@ember/service';
import { setupContext, teardownContext, getContext } from 'ember-test-helpers';
import {
setupContext,
teardownContext,
getContext,
pauseTest,
resumeTest,
} from 'ember-test-helpers';
import hasEmberVersion from 'ember-test-helpers/has-ember-version';
import { setResolverRegistry, createCustomResolver } from '../helpers/resolver';
import Ember from 'ember';
Expand Down Expand Up @@ -126,6 +132,76 @@ module('setupContext', function(hooks) {

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

test('can pauseTest to be resumed "later"', async function(assert) {
assert.expect(5);

let promise = context.pauseTest();

// do some random things while "paused"
setTimeout(function() {
assert.step('5 ms');
}, 5);

setTimeout(function() {
assert.step('20 ms');
}, 20);

setTimeout(function() {
assert.step('30 ms');
}, 30);

setTimeout(function() {
assert.step('50 ms');
context.resumeTest();
}, 50);

await promise;

assert.verifySteps(['5 ms', '20 ms', '30 ms', '50 ms']);
});

test('imported pauseTest and resumeTest allow customizations by test frameworks', async function(
assert
) {
assert.expect(2);

let originalPauseTest = context.pauseTest;
context.pauseTest = () => {
assert.ok(true, 'contexts pauseTest was called');
return originalPauseTest();
};

let originalResumeTest = context.resumeTest;
context.resumeTest = () => {
assert.ok(true, 'customized resumeTest was called');
return originalResumeTest();
};

let promise = pauseTest();

resumeTest();

await promise;
});

test('pauseTest sets up a window.resumeTest to easily resume', async function(assert) {
assert.equal(window.resumeTest, undefined, 'precond - starts out as undefined');

let promise = context.pauseTest();

assert.equal(
resumeTest,
window.resumeTest,
'window.resumeTest is the same as this.resumeTest'
);

context.resumeTest();

assert.equal(window.resumeTest, undefined, 'window.resumeTest is cleared after invocation');

await promise;
});
});

module('with custom options', function() {
Expand Down