meteorspark:test-helpers

v0.2.0Published 10 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

meteorspark:test-helpers

A collection of helpers for Meteor TinyTest based unittesting

QuickStart

Step 1: Add meteorspark:test-helpers to your package.js's Package.OnTest:

1Package.onTest(function(api) {
2  api.use('meteorspark:test-helpers', ['server', 'client']);
3});

Step 2: Initiate a TestHelpers instance:

1var th = new TestHelpers(options);

Constructor

th = new TestHelpers([options]) Anywhere

Default Options

1{
2    timeout: 1000 // timeout definition ms, time before helpers with timeout
3                  // fails with a "timeout" message
4}

Methods

onCompleteOnce = th.getOnCompleteOnce(onComplete[, test_specific_computations]) Anywhere

Use in Tinytest.addAsync tests, in tests in which onComplete, that TinyTest expects to be called only once, might be called more than once.

Returns a callback to be used instead of the provided onComplete. This callback will call the original onComplete only once and will run an optional callback provided to it in it's first call.

You can provide a computation or a list of comuptations as the second argument, in which case the helpers will stop() each one of them when its provided callback is called.

Arguments:

onComplete: The original onComplete provided to the test function Tinytest.addAsync.

test_specific_computations: a computation or an array of computations to stop() when the returned callback is called.

onCompleteOnce Arguments:

1onCompleteOnce([cb])

cb: an optional function to be called only the first time onCompleteOnce called.

Example:

1var th = new TestHelpers();
2
3Tinytest.addAsync('Test Example', function (test, onComplete) {
4    var session_var, subscription;
5
6    var computations = [
7        Tracker.autorun(function () {
8            subscription = Session.get(subscription);
9        }),
10        Tracker.autorun(function () {
11            Meteor.subscribe(subscription, function () {
12                test.ok();
13
14                onCompleteOnce();
15            });
16        })
17    ];
18
19    onCompleteOnce = th.getOnCompleteOnce(onComplete, computations);
20
21    setTimeout(function () {
22        onCompleteOnce(function () {
23            // Will be called only if onCompleteOnce called here for the first
24            // time        
25
26            test.fail("Timedout");
27
28            onCompleteOnce();
29        });
30    }, 100);
31});

onCompleteOnce = th.getOnCompleteOnceOrTimeout(test, onComplete[, test_specific_computations]) Anywhere

Same as th.getOnCompleteOnce but will fail automatically if timeout, as defined in the timeout option.

need to pass the test object as the first param.

th.getOnCompleteOnceOrTimeoutWithUser(test, onComplete, user, password[, test_specific_computations], cb) Anywhere

Same as th.getOnCompleteOnceOrTimeout but will login with user and logout onComplete.

Unlike the previous methods onCompleteOnce is returned by the method but passed to cb as its second param upon successful login.

If login failed the test will fail, cb won't be called, and non of the test_specific_computations will run.

Credits

Developed by Professional Meteor Services.

License

MIT

Author

Daniel Chcouri