-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for new RFC268 based testing API
- Loading branch information
1 parent
d852040
commit cf73491
Showing
8 changed files
with
253 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { | ||
beforeEach, | ||
afterEach | ||
} from 'mocha'; | ||
import { | ||
setupApplicationContext, | ||
teardownApplicationContext | ||
} from '@ember/test-helpers'; | ||
import setupContainerTest from './setup-container-test'; | ||
|
||
export default function setupApplicationTest(options) { | ||
afterEach(function() { | ||
return teardownApplicationContext(this); | ||
}); | ||
|
||
setupContainerTest(options); | ||
|
||
beforeEach(function() { | ||
return setupApplicationContext(this); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
beforeEach, | ||
afterEach | ||
} from 'mocha'; | ||
import { | ||
setupContext, | ||
teardownContext | ||
} from '@ember/test-helpers'; | ||
import { assign, merge } from '@ember/polyfills'; | ||
|
||
const _assign = assign || merge; | ||
|
||
export default function setupUnitTest(options) { | ||
let originalContext; | ||
|
||
beforeEach(function() { | ||
originalContext = _assign({}, this); | ||
|
||
return setupContext(this, options).then(() => { | ||
|
||
let originalPauseTest = this.pauseTest; | ||
this.pauseTest = function Mocha_pauseTest() { | ||
this.timeout(0); // prevent the test from timing out | ||
|
||
return originalPauseTest.call(this); | ||
}; | ||
}); | ||
}); | ||
|
||
afterEach(function() { | ||
return teardownContext(this).then(() => { | ||
// delete any extraneous properties | ||
for (let key in this) { | ||
if (!(key in originalContext)) { | ||
delete this[key]; | ||
} | ||
} | ||
|
||
// copy over the original values | ||
_assign(this, originalContext); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { | ||
beforeEach, | ||
afterEach | ||
} from 'mocha'; | ||
import { | ||
setupRenderingContext, | ||
teardownRenderingContext | ||
} from '@ember/test-helpers'; | ||
import setupContainerTest from './setup-container-test'; | ||
|
||
export default function setupRenderingTest(options) { | ||
afterEach(function() { | ||
return teardownRenderingContext(this); | ||
}); | ||
|
||
setupContainerTest(options); | ||
|
||
beforeEach(function() { | ||
return setupRenderingContext(this); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { describe, it } from 'mocha'; | ||
import { setupApplicationTest } from 'ember-mocha'; | ||
import { expect } from 'chai'; | ||
import { setApplication, visit } from '@ember/test-helpers'; | ||
import Application from '../../app'; | ||
import config from '../../config/environment'; | ||
import hasEmberVersion from 'ember-test-helpers/has-ember-version'; | ||
|
||
describe('setupApplicationTest', function() { | ||
if (!hasEmberVersion(2, 4)) { | ||
return; | ||
} | ||
|
||
setApplication(Application.create(Object.assign({}, config.APP, { autoboot: false }))); | ||
|
||
this.timeout(5000); | ||
|
||
setupApplicationTest(); | ||
|
||
it('can visit subroutes', async function() { | ||
await visit('/'); | ||
expect(this.element.querySelector('h2').textContent.trim()).to.be.empty; | ||
|
||
await visit('/foo'); | ||
expect(this.element.querySelector('h2').textContent.trim()).to.be.equal('this is an acceptance test'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { setupContainerTest } from 'ember-mocha'; | ||
import { describe, it, beforeEach, afterEach } 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'; | ||
|
||
describe('setupContainerTest', function() { | ||
if (!hasEmberVersion(2, 4)) { | ||
return; | ||
} | ||
|
||
describe('context setup', function() { | ||
|
||
setupContainerTest(); | ||
|
||
afterEach(function() { | ||
expect(this.owner, 'Context does not leak between tests').to.be.undefined; | ||
}); | ||
|
||
it('sets up owner', function() { | ||
expect(this.owner, 'Owner exists').to.exist; | ||
this.owner.register('service:dummy', Service.extend({ foo: 'bar' })); | ||
|
||
let subject = this.owner.lookup('service:dummy'); | ||
expect(subject.get('foo')).to.equal('bar'); | ||
}); | ||
|
||
it('has setters and getters', function() { | ||
expect(this) | ||
.to.respondTo('get') | ||
.and.to.respondTo('getProperties') | ||
.and.to.respondTo('set') | ||
.and.to.respondTo('setProperties'); | ||
|
||
this.set('foo', 'bar'); | ||
expect(this.get('foo')).to.equal('bar'); | ||
}); | ||
}); | ||
|
||
describe('pauseTest/resumeTest', function() { | ||
|
||
setupContainerTest(); | ||
|
||
it('can pause tests without timeouts', async function() { | ||
this.timeout(100); | ||
|
||
setTimeout(resumeTest, 200); // resume test after timeout | ||
// make sure pauseTest does not wait forever, if resumeTest fails | ||
let timer = setTimeout(() => { | ||
throw new Error('resumeTest() did not work') | ||
}, 300); | ||
|
||
await pauseTest(); | ||
clearTimeout(timer); | ||
}); | ||
}); | ||
|
||
describe('context in beforeEach/afterEach hooks', function() { | ||
|
||
// @todo will throw `this.get is not a function` if called after `setupRenderingTest()` | ||
afterEach(function() { | ||
expect(this.get('name')).to.equal('blue'); | ||
}); | ||
|
||
setupContainerTest(); | ||
|
||
beforeEach(function() { | ||
this.set('name', 'red'); | ||
}); | ||
|
||
beforeEach(function() { | ||
expect(this.get('name')).to.equal('red'); | ||
}); | ||
|
||
it('has correct context', async function() { | ||
expect(this.get('name')).to.equal('red'); | ||
this.set('name', 'blue'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Component from '@ember/component'; | ||
import { computed } from '@ember/object'; | ||
import { setupRenderingTest } from 'ember-mocha'; | ||
import { describe, it, beforeEach } from 'mocha'; | ||
import { expect } from 'chai'; | ||
import hbs from 'htmlbars-inline-precompile'; | ||
import { render } from '@ember/test-helpers'; | ||
import hasEmberVersion from 'ember-test-helpers/has-ember-version'; | ||
|
||
const PrettyColor = Component.extend({ | ||
classNames: ['pretty-color'], | ||
attributeBindings: ['style'], | ||
style: computed('name', function() { | ||
return 'color: ' + this.get('name') + ';'; | ||
}) | ||
}); | ||
|
||
function setupRegistry(owner) { | ||
owner.register('component:x-foo', Component.extend()); | ||
owner.register('component:pretty-color', PrettyColor); | ||
owner.register('template:components/pretty-color', hbs`Pretty Color: <span class="color-name">{{name}}</span>`); | ||
} | ||
|
||
describe('setupRenderingTest', function() { | ||
if (!hasEmberVersion(2, 4)) { | ||
return; | ||
} | ||
|
||
describe('pretty-color', function() { | ||
setupRenderingTest(); | ||
|
||
beforeEach(function() { | ||
setupRegistry(this.owner); | ||
}); | ||
|
||
it('renders with color', async function() { | ||
this.set('name', 'green'); | ||
await render(hbs`{{pretty-color name=name}}`); | ||
expect(this.element.textContent.trim()).to.equal('Pretty Color: green'); | ||
}); | ||
|
||
it('renders a second time without', async function() { | ||
await render(hbs`{{pretty-color name=name}}`); | ||
expect(this.element.textContent.trim()).to.equal('Pretty Color:'); | ||
}); | ||
}); | ||
|
||
}); |