Created
September 8, 2015 00:41
-
-
Save phillipskevin/eeca2245d2d67831e6b9 to your computer and use it in GitHub Desktop.
Example QUnit Test using StealJS for Dependency Injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("[email protected]#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(); | |
}); | |
}); | |
}); | |
}) |
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
Cool, what would be the ideal API for doing this kind of thing?