1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove<div class="intro">
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove <p>This example shows how to create an asynchronous test with the YUI Test framework for testing browser-based JavaScript code.
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove A <code>Y.Test.Case</code> object is created with a test that waits for a
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove few seconds before continuing. The <code>Y.Test.Runner</code>
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove is then used to run the tests once the page has loaded.</p>
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove</div>
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove<div class="example yui3-skin-sam">
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove {{>test-async-test-source}}
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove</div>
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove<h2 class="first">Asynchronous Test Example</h2>
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove<p>This example begins by creating a namespace:</p>
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove```
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan GroveY.namespace("example.test");
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove```
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove<p>This namespace serves as the core object upon which others will be added (to prevent creating global objects).</p>
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove<h3>Creating the TestCase</h3>
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove<p>The first step is to create a new <code>Y.Test.Case</code> object called <code>AsyncTestCase</code>.
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove To do so, using the <code>Y.Test.Case</code> constructor and pass in an object literal containing information about the tests to be run:</p>
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove```
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan GroveY.example.test.AsyncTestCase = new Y.Test.Case({
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove //name of the test case - if not provided, one is auto-generated
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove name : "Asynchronous Tests",
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove //---------------------------------------------------------------------
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove // setUp and tearDown methods - optional
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove //---------------------------------------------------------------------
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove /*
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove * Sets up data that is needed by each test.
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove */
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove setUp : function () {
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove this.data = {
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove name: "test",
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove year: 2007,
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove beta: true
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove };
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove },
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove /*
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove * Cleans up everything that was created by setUp().
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove */
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove tearDown : function () {
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove delete this.data;
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove },
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove //---------------------------------------------------------------------
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove // Test methods - names must begin with "test"
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove //---------------------------------------------------------------------
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove testWait : function (){
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove var Assert = Y.Assert;
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove //do some assertions now
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove Assert.isTrue(this.data.beta);
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove Assert.isNumber(this.data.year);
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove //wait five seconds and do some more
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove this.wait(function(){
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove Assert.isString(this.data.name);
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove }, 5000);
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove }
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove});
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove```
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove<p>The object literal passed into the constructor contains two different sections. The first section contains the <code>name</code> property,
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove which is used to determine which <code>Y.Test.Case</code> is being executed. A name is necessary, so one is generated if it isn't specified.</p>
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove<p>Next, the <code>setUp()</code> and <code>tearDown()</code> methods are included. The <code>setUp()</code> method is used in a <code>Y.Test.Case</code>
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove to set up data that may be needed for tests to be completed. This method is called immediately before each test is executed. For this example,
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove <code>setUp()</code> creates a data object. The <code>tearDown()</code> is responsible for undoing what was done in <code>setUp()</code>. It is
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove run immediately after each test is run and, in this case, deletes the data object that was created by <code>setUp</code>. These methods are optional.</p>
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove<p>The second section contains the actual tests to be run. The only test is <code>testWait()</code>, which demonstrates using
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove the <code>wait()</code> method to delay test execution. There are two arguments passed in: a function to run once the test resumes
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove and the number of milliseconds to wait before running this function (same basic format as <code>setTimeout()</code>). When
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove the test resumes, the function is executed in the context of the <code>Y.Test.Case</code> object, meaning that it still has
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove access to all of the same data as the test that called <code>wait()</code>, including properties and methods on the <code>Y.Test.Case</code>
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove itself. This example shows the anonymous function using both the <code>Y.Assert</code> object and the <code>data</code> property
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove of the <code>Y.Test.Case</code>.</p>
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove<h3>Running the tests</h3>
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove<p>With all of the tests defined, the last step is to run them:</p>
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove```
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove//create the console
1ac6d38f8eef642ee4f7191b863a986f4cb7571fDav Glass(new Y.Test.Console({
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove verbose : true,
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove newestOnTop : false
1ac6d38f8eef642ee4f7191b863a986f4cb7571fDav Glass})).render('#testLogger');
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove//add the test suite to the runner's queue
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan GroveY.Test.Runner.add(Y.example.test.AsyncTestCase);
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove//run the tests
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan GroveY.Test.Runner.run();
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove```
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1ac6d38f8eef642ee4f7191b863a986f4cb7571fDav Glass<p>Before running the tests, it's necessary to create a <code>Y.Test.Console</code> object to display the results (otherwise the tests would run
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove but you wouldn't see the results). After that, the <code>Y.Test.Runner</code> is loaded with the <code>Y.Test.Case</code> object by calling
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove <code>add()</code> (any number of <code>Y.Test.Case</code> and <code>TestSuite</code> objects can be added to a <code>TestRunner</code>,
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove this example only adds one for simplicity). The very last step is to call <code>run()</code>, which begins executing the tests in its
1ac6d38f8eef642ee4f7191b863a986f4cb7571fDav Glass queue and displays the results in the <code>Y.Test.Console</code>.</p>
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove<h2>Complete Example Source</h2>
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove```
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove{{>test-async-test-source}}
1b7d9ee6f1128c8cb5e16c3a11ba045998296171Ryan Grove```