When using FuncUnit, break down a user's interaction into three segments: finding elements, executing actions and waiting for assertions.
Find an element with jQuery's selector syntax:
F('.selector')
Execute a user action:
F('.selector').click()
Then wait for an assertion:
F('.anotherselector').visible() //waits until element is visible
FuncUnit can be used to enhance existing unit tests or write new unit tests while keeping the standard QUnit/Jasmine ideology. FuncUnit can also be used to write functional tests as outlined below.
Enhancing unit tests
This section takes a basic QUnit test and uses FuncUnit to simulate user interaction.
Instead of using the built in QUnit assertions, use any of FuncUnit's waits.
test('Hello World!', function() {
F('.sample').text('Hello World!', 'h1 should have text hello world');
});
Note: Included in the examples package is a "Hello World" Jasmine spec.
Testing a simple slider widget
This section takes a common UI Slider from jQueryUI and employs FuncUnit's actions and waits.
The test page is setup with our basic dependencies: jQuery, jQueryUI, QUnit and FuncUnit
To test the slider's change of value, use the drag action.
All FuncUnit actions accept a callback for additional validation. With the slider widget, assert the value has increased upon sliding.
test('drag slider', function() {
F('#qunit-test-area a').drag('+200 +0', function() {
var value = $('#qunit-test-area div').slider('option', 'value');
ok(value > 0, 'value after 200px should not be 0');
});
});
Mix or match your built in assertion library, as well as using FuncUnit's waits and actions to accurately depict user interaction.
Functional testing with a tabs widget
Functional tests, for purposes of these guides, are defined as an application running in its true state. This is to say, not in an isolated fixture div or sandbox area. FuncUnit allows for opening a URL, then providing the same actions and waits to test the application.
Note: It is recommended for these types of tests to have a fixturized service layer such as CanJS fixtures or use an isolated test database when service calls are present.
Using F.open, test a simple tabs widget in its own page.
Then run actions and waits the same as in the slider test and watch as the newly opened window responds!
test('single tab click', function() {
F('li:contains("Fallout")').click();
F('#starcraft').invisible('tab should be hidden');
F('#fallout').visible('tab should be shown');
});
test('clicking one tab then another', function() {
F('li:contains("Fallout")').click();
F('#fallout').visible('tab should be shown');
F('li:contains("Robot Unicorn Attack")').click();
F('#fallout').invisible('tab should be hidden');
F('#rua').visible('tab should be shown');
});
Conclusion
Hopefully, this guide illustrates how FuncUnit provides the holy grail of testing: easy, familiar syntax, in browser running for easy debugging and simple automation.
FuncUnit transforms your development lifecycle, gives your developers confidence and improves quality. For a more complex example with module loaders and multiple widgets, download the examples package and view the Srchr app's tests.
That's it! If you want to learn more, read about FuncUnit's FuncUnit API.
In this guide, we'll use FuncUnit to simulate actions as a user would with a simple tabs and slider widget.
Note:
FuncUnit has two dependencies:
How to get FuncUnit
You can download funcunit.js, or install as a bower component or npm package:
or
If using from npm you can
require
FuncUnit in your application if you're using StealJS or Browserify.This will attach
QUnit
to be your test runner. FuncUnit works with QUnit, Jasmine, and Mocha.All examples below can be downloaded here.
Intro to Funcunit
When using FuncUnit, break down a user's interaction into three segments: finding elements, executing actions and waiting for assertions.
Find an element with jQuery's selector syntax:
Execute a user action:
Then wait for an assertion:
FuncUnit can be used to enhance existing unit tests or write new unit tests while keeping the standard QUnit/Jasmine ideology. FuncUnit can also be used to write functional tests as outlined below.
Enhancing unit tests
This section takes a basic QUnit test and uses FuncUnit to simulate user interaction.
Instead of using the built in QUnit assertions, use any of FuncUnit's waits.
Note: Included in the examples package is a "Hello World" Jasmine spec.
Testing a simple slider widget
This section takes a common UI Slider from jQueryUI and employs FuncUnit's actions and waits.
The test page is setup with our basic dependencies: jQuery, jQueryUI, QUnit and FuncUnit
First, the test file sets up a simple slider:
To test the slider's change of value, use the drag action.
All FuncUnit actions accept a callback for additional validation. With the slider widget, assert the value has increased upon sliding.
Mix or match your built in assertion library, as well as using FuncUnit's waits and actions to accurately depict user interaction.
Functional testing with a tabs widget
Functional tests, for purposes of these guides, are defined as an application running in its true state. This is to say, not in an isolated fixture div or sandbox area. FuncUnit allows for opening a URL, then providing the same actions and waits to test the application.
Note: It is recommended for these types of tests to have a fixturized service layer such as CanJS fixtures or use an isolated test database when service calls are present.
Using F.open, test a simple tabs widget in its own page.
First, setup the QUnit runner page:
Then configure our setup module. Call the
open
method to create a new window with the requested URL. This will act as our sandbox test area.Then run actions and waits the same as in the slider test and watch as the newly opened window responds!
Conclusion
Hopefully, this guide illustrates how FuncUnit provides the holy grail of testing: easy, familiar syntax, in browser running for easy debugging and simple automation.
FuncUnit transforms your development lifecycle, gives your developers confidence and improves quality. For a more complex example with module loaders and multiple widgets, download the examples package and view the Srchr app's tests.
That's it! If you want to learn more, read about FuncUnit's FuncUnit API.