Skip to content

Dependency Injection #509

@justinbmeyer

Description

@justinbmeyer

I would like to support dependency injection with Steal. This would be great from a testing perspective. I know @phillipskevin got something like this started.

Example:

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();
            });
        });
    });
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions