Skip to content
This repository has been archived by the owner on Sep 19, 2018. It is now read-only.

Add test_on and test_onload convenience functions. #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions apisample.htm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ <h1>Sample HTML5 API Tests</h1>
var load_test_attr = async_test("body element fires the onload event set from the attribute");
</script>
<script>
test_on(function(e) {
assert_equals(e.type, "DOMContentLoaded");
}, window, "DOMContentLoaded", "test_on test for window DOMContentLoaded event");

test_onload(function () {
assert_true(true)
}, "test_onload test");

function bodyElement()
{
assert_equals(document.body, document.getElementsByTagName("body")[0]);
Expand Down
14 changes: 14 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,26 @@ tests instances.

The properties argument is identical to that for `test()`.

## Testing with Events ##

In many cases it is convenient to run a step in response to an event or a
callback. A convenient method of doing this is through the step_func method
which returns a function that, when called runs a test step. For example

object.some_event = t.step_func(function(e) {assert_true(e.a)});

For the special case of a test that has a single step run in reponse
to some event, the convenience function `test_on` can be used, for
example:

test_on(function(e) {assert_equals(e.type, "DOMContentLoaded")},
window, "DOMcontentLoaded");

The most common case of all is running a test after the document has
fully loaded; this can be achieved using `test_onload`:

test_onload(function() {assert_equals(window.readyState, "complete")})

For asynchronous callbacks that should never execute, `unreached_func` can
be used. For example:

Expand Down
17 changes: 17 additions & 0 deletions testharness.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,21 @@ policies and contribution forms [3].
return test_obj;
}

function test_on(func, obj, event, name, properties) {
var t = async_test(name, properties);
obj.addEventListener(event, function f(e) {
t.add_cleanup(function() {obj.removeEventListener(f)});
t.step(function() {
func.call(t, e);
});
t.done();
});
}

function test_onload(func, name, properties) {
test_on(func, window, "load", name, properties);
}

function promise_test(func, name, properties) {
var test = async_test(name, properties);
Promise.resolve(test.step(func, test, test))
Expand Down Expand Up @@ -516,6 +531,8 @@ policies and contribution forms [3].

expose(test, 'test');
expose(async_test, 'async_test');
expose(test_on, 'test_on');
expose(test_onload, 'test_onload');
expose(promise_test, 'promise_test');
expose(promise_rejects, 'promise_rejects');
expose(generate_tests, 'generate_tests');
Expand Down