Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save phillipskevin/eeca2245d2d67831e6b9 to your computer and use it in GitHub Desktop.
Save phillipskevin/eeca2245d2d67831e6b9 to your computer and use it in GitHub Desktop.
Example QUnit Test using StealJS for Dependency Injection
import QUnit from 'steal-qunit';
QUnit.module('Tournament Details ViewModel', {
beforeEach: function () {
localStorage.clear();
}
});
test('should load a tournament', (assert) => {
let done = assert.async(),
localSteal,
localTournament,
vm;
// clone a local copy of steal
localSteal = steal.clone();
// use `steal.System` to create the module to be injected for use in test
localTournament = localSteal.System.newModule({
__useDefault: true,
default: {
get: () => Promise.resolve(new can.Map({ name: 'Test Name' }))
}
});
// set up local steal and call `startup()` to read configuration file
localSteal.System.configMain = steal.System.configMain;
localSteal.System.baseURL = steal.System.baseURL;
localSteal.startup({ main: '@empty' }).then(function () {
// set fully normalized module name to inject the test module
localSteal.System.set("bitballs@0.0.1#models/tournament/tournament", localTournament);
// re-import the module-under-test so that it will use the injected dependency
localSteal.System.import('components/tournament/details/viewModel').then(function (ViewModel) {
// run QUnit test using module returned by `System.import`
vm = new ViewModel({
tournamentId: 2
});
vm.bind('tournament', function (ev, newVal) {
assert.equal(newVal.attr('name'), 'Test Name', 'with the correct name' );
done();
});
});
});
})
@matthewp
Copy link

matthewp commented Sep 9, 2015

Cool, what would be the ideal API for doing this kind of thing?

@phillipskevin
Copy link
Author

Sorry, I didn't see your question @matthewp. I'm not sure exactly what a good API would be for this. An issue has been created for this: stealjs/steal#509 so maybe we can get the discussion going there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment