ember-test-helpers
Advanced tools
Comparing version 0.7.0-beta.5 to 0.7.0-beta.6
@@ -6,11 +6,11 @@ // TODO: deprecate these once new API is rolled out | ||
export { default as TestModuleForModel } from './legacy-0-6-x/test-module-for-model'; | ||
export { getContext, setContext, unsetContext } from './legacy-0-6-x/test-context'; | ||
export { setResolver } from './resolver'; | ||
export { default as setupContext } from './setup-context'; | ||
export { default as setupContext, getContext, setContext, unsetContext } from './setup-context'; | ||
export { default as teardownContext } from './teardown-context'; | ||
export { default as setupRenderingContext } from './setup-rendering-context'; | ||
export { default as setupRenderingContext, render, clearRender } from './setup-rendering-context'; | ||
export { default as teardownRenderingContext } from './teardown-rendering-context'; | ||
export { default as settled } from './settled'; | ||
import Ember from 'ember'; | ||
Ember.testing = true; |
@@ -5,4 +5,4 @@ import { run } from '@ember/runloop'; | ||
import { _setupPromiseListeners, _teardownPromiseListeners } from '../ext/rsvp'; | ||
import { _setupAJAXHooks, _teardownAJAXHooks } from '../wait'; | ||
import { getContext, setContext, unsetContext } from './test-context'; | ||
import { _setupAJAXHooks, _teardownAJAXHooks } from '../settled'; | ||
import { getContext, setContext, unsetContext } from '../setup-context'; | ||
@@ -9,0 +9,0 @@ import Ember from 'ember'; |
import { run } from '@ember/runloop'; | ||
import AbstractTestModule from './abstract-test-module'; | ||
import { getContext } from './test-context'; | ||
import { getContext } from '../setup-context'; | ||
@@ -5,0 +5,0 @@ export default class extends AbstractTestModule { |
@@ -5,7 +5,22 @@ import { run } from '@ember/runloop'; | ||
import { _setupPromiseListeners } from './ext/rsvp'; | ||
import { _setupAJAXHooks } from './wait'; | ||
import { _setupAJAXHooks } from './settled'; | ||
let __test_context__; | ||
export function setContext(context) { | ||
__test_context__ = context; | ||
} | ||
export function getContext() { | ||
return __test_context__; | ||
} | ||
export function unsetContext() { | ||
__test_context__ = undefined; | ||
} | ||
/* | ||
* Responsible for: | ||
* | ||
* - sets the "global testing context" to the provided context | ||
* - create an owner object and set it on the provided context (e.g. this.owner) | ||
@@ -17,2 +32,4 @@ * - setup this.set, this.setProperties, this.get, and this.getProperties to the provided context | ||
export default function(context, options = {}) { | ||
setContext(context); | ||
let resolver = options.resolver; | ||
@@ -19,0 +36,0 @@ let owner = buildOwner(resolver); |
@@ -6,5 +6,28 @@ import { guidFor } from '@ember/object/internals'; | ||
import global from './global'; | ||
import { getContext } from './setup-context'; | ||
export const RENDERING_CLEANUP = Object.create(null); | ||
export function render(template) { | ||
let context = getContext(); | ||
if (!context || typeof context.render !== 'function') { | ||
throw new Error('Cannot call `render` without having first called `setupRenderingContext`.'); | ||
} | ||
return context.render(template); | ||
} | ||
export function clearRender() { | ||
let context = getContext(); | ||
if (!context || typeof context.clearRender !== 'function') { | ||
throw new Error( | ||
'Cannot call `clearRender` without having first called `setupRenderingContext`.' | ||
); | ||
} | ||
return context.clearRender(); | ||
} | ||
/* | ||
@@ -26,2 +49,3 @@ * Responsible for: | ||
rootTestElement.innerHTML = fixtureResetValue; | ||
element = undefined; | ||
}, | ||
@@ -28,0 +52,0 @@ ]; |
import { run } from '@ember/runloop'; | ||
import { _teardownPromiseListeners } from './ext/rsvp'; | ||
import { _teardownAJAXHooks } from './wait'; | ||
import { _teardownAJAXHooks } from './settled'; | ||
@@ -5,0 +5,0 @@ export default function(context) { |
@@ -1,98 +0,7 @@ | ||
/* globals self */ | ||
import { run } from '@ember/runloop'; | ||
import { Promise as EmberPromise } from 'rsvp'; | ||
import jQuery from 'jquery'; | ||
import Ember from 'ember'; | ||
var requests; | ||
function incrementAjaxPendingRequests(_, xhr) { | ||
requests.push(xhr); | ||
} | ||
function decrementAjaxPendingRequests(_, xhr) { | ||
// In most Ember versions to date (current version is 2.16) RSVP promises are | ||
// configured to flush in the actions queue of the Ember run loop, however it | ||
// is possible that in the future this changes to use "true" micro-task | ||
// queues. | ||
// | ||
// The entire point here, is that _whenever_ promises are resolved, this | ||
// counter will decrement. In the specific case of AJAX, this means that any | ||
// promises chained off of `$.ajax` will properly have their `.then` called | ||
// _before_ this is decremented (and testing continues) | ||
EmberPromise.resolve().then(() => { | ||
for (var i = 0; i < requests.length; i++) { | ||
if (xhr === requests[i]) { | ||
requests.splice(i, 1); | ||
} | ||
} | ||
}); | ||
} | ||
export function _teardownAJAXHooks() { | ||
if (!jQuery) { | ||
return; | ||
} | ||
jQuery(document).off('ajaxSend', incrementAjaxPendingRequests); | ||
jQuery(document).off('ajaxComplete', decrementAjaxPendingRequests); | ||
} | ||
export function _setupAJAXHooks() { | ||
requests = []; | ||
if (!jQuery) { | ||
return; | ||
} | ||
jQuery(document).on('ajaxSend', incrementAjaxPendingRequests); | ||
jQuery(document).on('ajaxComplete', decrementAjaxPendingRequests); | ||
} | ||
var _internalCheckWaiters; | ||
if (Ember.__loader.registry['ember-testing/test/waiters']) { | ||
_internalCheckWaiters = Ember.__loader.require('ember-testing/test/waiters').checkWaiters; | ||
} | ||
function checkWaiters() { | ||
if (_internalCheckWaiters) { | ||
return _internalCheckWaiters(); | ||
} else if (Ember.Test.waiters) { | ||
if (Ember.Test.waiters.any(([context, callback]) => !callback.call(context))) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
export default function wait(_options) { | ||
var options = _options || {}; | ||
var waitForTimers = options.hasOwnProperty('waitForTimers') ? options.waitForTimers : true; | ||
var waitForAJAX = options.hasOwnProperty('waitForAJAX') ? options.waitForAJAX : true; | ||
var waitForWaiters = options.hasOwnProperty('waitForWaiters') ? options.waitForWaiters : true; | ||
return new EmberPromise(function(resolve) { | ||
var watcher = self.setInterval(function() { | ||
if (waitForTimers && (run.hasScheduledTimers() || run.currentRunLoop)) { | ||
return; | ||
} | ||
if (waitForAJAX && requests && requests.length > 0) { | ||
return; | ||
} | ||
if (waitForWaiters && checkWaiters()) { | ||
return; | ||
} | ||
// Stop polling | ||
self.clearInterval(watcher); | ||
// Synchronously resolve the promise | ||
run(null, resolve); | ||
}, 10); | ||
}); | ||
} | ||
export { | ||
default, | ||
_setupAJAXHooks, | ||
_setupPromiseListeners, | ||
_teardownAJAXHooks, | ||
_teardownPromiseListeners, | ||
} from './settled'; |
# Change Log | ||
## [v0.7.0-beta.6](https://github.com/emberjs/ember-test-helpers/tree/v0.7.0-beta.6) (2017-10-17) | ||
[Full Changelog](https://github.com/emberjs/ember-test-helpers/compare/v0.7.0-beta.5...v0.7.0-beta.6) | ||
**Implemented enhancements:** | ||
- Expose `settled` helper function. [\#223](https://github.com/emberjs/ember-test-helpers/pull/223) ([rwjblue](https://github.com/rwjblue)) | ||
- Expose importable helper functions. [\#222](https://github.com/emberjs/ember-test-helpers/pull/222) ([rwjblue](https://github.com/rwjblue)) | ||
**Merged pull requests:** | ||
- Update README for new API iteration. [\#225](https://github.com/emberjs/ember-test-helpers/pull/225) ([rwjblue](https://github.com/rwjblue)) | ||
- Continue to flesh out more tests for new API's. [\#224](https://github.com/emberjs/ember-test-helpers/pull/224) ([rwjblue](https://github.com/rwjblue)) | ||
- Move `setContext` into `setupContext`. [\#221](https://github.com/emberjs/ember-test-helpers/pull/221) ([rwjblue](https://github.com/rwjblue)) | ||
## [v0.7.0-beta.5](https://github.com/emberjs/ember-test-helpers/tree/v0.7.0-beta.5) (2017-10-16) | ||
@@ -4,0 +18,0 @@ [Full Changelog](https://github.com/emberjs/ember-test-helpers/compare/v0.7.0-beta.4...v0.7.0-beta.5) |
{ | ||
"name": "ember-test-helpers", | ||
"version": "0.7.0-beta.5", | ||
"version": "0.7.0-beta.6", | ||
"description": "Helpers for testing Ember.js applications", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -13,76 +13,22 @@ # ember-test-helpers [![Build Status](https://secure.travis-ci.org/emberjs/ember-test-helpers.png?branch=master)](http://travis-ci.org/emberjs/ember-test-helpers) | ||
### TestModule | ||
The exports of this library are intended to be utility functions that can be used to bring the | ||
standard Ember testing experience to any testing framework. | ||
The `TestModule` class is used to configure modules for unit testing | ||
different aspects of your Ember application. This class can be extended to | ||
create modules focused on particular types of unit tests. | ||
A quick summary of the exports from the `ember-test-helpers` module: | ||
`TestModule` is intended to be used in conjunction with modules specific to | ||
a test framework. For instance, you could create QUnit-compatible modules with | ||
a method such as: | ||
* `setResolver` - This function is used to allow the other functions build a valid | ||
container/registry that is able to look objects up from your application (just as a running Ember | ||
application would). | ||
* `setContext` - Invoked by the host testing framework to set the current testing context (generally | ||
the `this` within a running test). | ||
* `getContext` - Used to retrieve the current testing context. | ||
* `unsetContext` - Used to ensure that all handles on the testing context are released (allowing GC). | ||
* `setupContext` - Sets up a given testing context with `owner`, `get`, `set`, etc properties. | ||
* `teardownContext` - Cleans up any objects created as part of the owner created in `setupContext`. | ||
* `setupRenderingContext` - Sets up the provided context with the ability to render template | ||
snippets by adding `render`, `clearRender`, etc. | ||
* `teardownRenderingContext` - Cleans up any work done in a rendering test. | ||
* `settled` - Returns a promise which will resolve when all async from AJAX, test waiters, and | ||
scheduled timers have completed. | ||
```javascript | ||
function moduleFor(name, description, callbacks) { | ||
let module = new TestModule(name, description, callbacks); | ||
QUnit.module(module.name, { | ||
beforeEach() { | ||
return module.setup(); | ||
}, | ||
afterEach() { | ||
return module.teardown(); | ||
} | ||
}); | ||
} | ||
``` | ||
------ | ||
`TestModule(name [, description [, callbacks]])` | ||
* `name` - the full name of the test subject as it is registered in a container | ||
(e.g. 'controller:application', 'route:index', etc.). | ||
* `description` (optional) - the description of the test module as it should be | ||
displayed in test output. If omitted, defaults to `name`. | ||
* `callbacks` (optional) - an object that may include setup and teardown steps | ||
as well as the other units needed by tests. | ||
* `callbacks.resolver` (optional) - a Resolver instance to be used for the test | ||
module. Takes precedence over the globally set Resolver. | ||
### TestModuleForComponent | ||
`TestModuleForComponent` extends `TestModule` to allow unit testing of Ember | ||
Components. | ||
------ | ||
`TestModuleForComponent(name [, description [, callbacks]])` | ||
* `name` - the short name of the component that you'd use in a template | ||
(e.g. 'x-foo', 'color-picker', etc.). | ||
### TestModuleForModel | ||
`TestModuleForModel` extends `TestModule` to allow unit testing of Ember Data | ||
Models. | ||
------ | ||
`TestModuleForModel(name [, description [, callbacks]])` | ||
* `name` - the short name of the model that you'd use in store operations | ||
(e.g. 'user', 'assignmentGroup', etc.). | ||
### Miscellaneous Helpers | ||
* `getContext` / `setContext` - access the context to be used in each test. | ||
* `setResolver` - sets a Resolver globally which will be used to look up objects | ||
from each test's container. | ||
* `isolatedContainer` - creates a new isolated container for unit testing. | ||
## Collaborating | ||
@@ -89,0 +35,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
325578
1467
0
69