Skip to content

Commit

Permalink
Merge pull request #233 from rwjblue/add-pause-test-resume-test
Browse files Browse the repository at this point in the history
Add pauseTest and resumeTest functionality.
  • Loading branch information
rwjblue authored Nov 5, 2017
2 parents 21dd391 + f065e1f commit 067bd53
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 2 deletions.
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

0 comments on commit 067bd53

Please sign in to comment.