Skip to content

Commit

Permalink
Implement setupTest supporting old and new testing APIs
Browse files Browse the repository at this point in the history
Closes #198
  • Loading branch information
simonihmig committed Mar 5, 2018
1 parent 7833e14 commit 08be15c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
14 changes: 11 additions & 3 deletions addon-test-support/ember-mocha/index.js
Expand Up @@ -2,7 +2,7 @@ import describeModule from 'ember-mocha/describe-module';
import describeComponent from 'ember-mocha/describe-component';
import describeModel from 'ember-mocha/describe-model';
import setupTestFactory from 'ember-mocha/setup-test-factory';
import setupContainerTest from 'ember-mocha/setup-container-test';
import setupTestNew from 'ember-mocha/setup-test';
import setupRenderingTest from 'ember-mocha/setup-rendering-test';
import setupApplicationTest from 'ember-mocha/setup-application-test';
import { it } from 'mocha';
Expand All @@ -14,11 +14,20 @@ import {
TestModuleForAcceptance
} from 'ember-test-helpers';

const setupTest = setupTestFactory(TestModule);
const setupTestOld = setupTestFactory(TestModule);
const setupAcceptanceTest = setupTestFactory(TestModuleForAcceptance);
const setupComponentTest = setupTestFactory(TestModuleForComponent);
const setupModelTest = setupTestFactory(TestModuleForModel);

// wrapper function that supports the old and the new testing APIs, depending on its arguments
function setupTest(moduleName) {
// when the first argument is a string, this is the old testing API
if (typeof moduleName === 'string') {
return setupTestOld(...arguments);
}
return setupTestNew(...arguments);
}

export {
describeModule,
describeComponent,
Expand All @@ -27,7 +36,6 @@ export {
setupAcceptanceTest,
setupComponentTest,
setupModelTest,
setupContainerTest,
setupRenderingTest,
setupApplicationTest,
it,
Expand Down
4 changes: 2 additions & 2 deletions addon-test-support/ember-mocha/setup-application-test.js
Expand Up @@ -2,10 +2,10 @@ import {
setupApplicationContext,
teardownApplicationContext
} from '@ember/test-helpers';
import setupContainerTest from './setup-container-test';
import setupTest from './setup-test';

export default function setupApplicationTest(options) {
let hooks = setupContainerTest(options);
let hooks = setupTest(options);

hooks.beforeEach(function() {
return setupApplicationContext(this);
Expand Down
4 changes: 2 additions & 2 deletions addon-test-support/ember-mocha/setup-rendering-test.js
Expand Up @@ -2,10 +2,10 @@ import {
setupRenderingContext,
teardownRenderingContext
} from '@ember/test-helpers';
import setupContainerTest from './setup-container-test';
import setupTest from './setup-test';

export default function setupRenderingTest(options) {
let hooks = setupContainerTest(options);
let hooks = setupTest(options);

hooks.beforeEach(function() {
return setupRenderingContext(this);
Expand Down
Expand Up @@ -24,7 +24,7 @@ function setupPauseTest(context) {
};
}

export default function setupUnitTest(options) {
export default function setupTest(options) {
let originalContext;
let beforeEachHooks = [];
let afterEachHooks = [];
Expand Down
@@ -1,19 +1,19 @@
import { setupContainerTest } from 'ember-mocha';
import { setupTest } from 'ember-mocha';
import { describe, it, beforeEach, afterEach, after } from 'mocha';
import { expect } from 'chai';
import { pauseTest, resumeTest } from '@ember/test-helpers';
import Service from '@ember/service';
import hasEmberVersion from 'ember-test-helpers/has-ember-version';
import { Promise } from 'rsvp';

describe('setupContainerTest', function() {
describe('setupTest', function() {
if (!hasEmberVersion(2, 4)) {
return;
}

describe('context setup', function() {

setupContainerTest();
setupTest();

afterEach(function() {
expect(this.owner, 'Context does not leak between tests').to.be.undefined;
Expand Down Expand Up @@ -41,7 +41,7 @@ describe('setupContainerTest', function() {

describe('pauseTest/resumeTest', function() {

setupContainerTest();
setupTest();

it('can pause tests without timeouts', async function() {
this.timeout(100);
Expand All @@ -64,7 +64,7 @@ describe('setupContainerTest', function() {
expect(this.get('name')).to.equal('blue');
});

setupContainerTest();
setupTest();

beforeEach(function() {
this.set('name', 'red');
Expand Down Expand Up @@ -101,7 +101,7 @@ describe('setupContainerTest', function() {
expect(calledSteps, 'hooks are called in correct order').to.deep.equal(['bE1', 'bE2', 'it', 'aE2', 'aE1']);
});

let hooks = setupContainerTest();
let hooks = setupTest();
setupFoo(hooks);

hooks.beforeEach(function() {
Expand Down Expand Up @@ -129,7 +129,7 @@ describe('setupContainerTest', function() {
expect(calledSteps, 'hooks are called in correct order').to.deep.equal(['bE1', 'bE2', 'it', 'aE2', 'aE1']);
});

let hooks = setupContainerTest();
let hooks = setupTest();

hooks.beforeEach(function() {
expect(calledSteps, 'beforeEach waits for promise').to.deep.equal([]);
Expand Down

0 comments on commit 08be15c

Please sign in to comment.