Skip to content

Instantly share code, notes, and snippets.

@jonfazzaro
Last active October 19, 2016 13:44
Show Gist options
  • Save jonfazzaro/3b4605f3a9efce069a3f67df053f6be8 to your computer and use it in GitHub Desktop.
Save jonfazzaro/3b4605f3a9efce069a3f67df053f6be8 to your computer and use it in GitHub Desktop.
A syntactic sugar packet for Squire.js (https://github.com/iammerrick/Squire.js/).
define(function (require) {
var Squire = require('Squire');
return {
load: load
};
function load(modelId, dependencies) {
var s = new Squire();
addMockedDependencies(s, dependencies)
s.store(listed(dependencies));
return resquire(s, modelId);
}
function resquire(squire, modelId) {
var p = $.Deferred();
squire.require([modelId, 'mocks'], function (model, mocks) {
p.resolve(model, hash(mocks.store), mocks.store);
});
return p;
}
function hash(store) {
var o = {};
for (var moduleId in store)
o[simple(moduleId)] = store[moduleId];
return o;
}
function addMockedDependencies(squire, dependencies) {
for (var moduleId in (dependencies || {}))
if (dependencies[moduleId])
squire.mock(moduleId, dependencies[moduleId]);
}
function listed(mocks) {
var list = [];
for (var id in mocks || [])
list.push(id);
return list;
}
function simple(id) {
var tokens = id.split('/');
return tokens[tokens.length - 1];
}
function toArray(object) {
var array = [];
for (var property in object)
array.push(object[property]);
return array;
}
});
@jonfazzaro
Copy link
Author

jonfazzaro commented Aug 22, 2016

A syntactic sugar packet for Squire.js.

Turns this

beforeEach(function(done) {
    new Squire()
        .mock('services/permissions', mockPermissions())
        .mock('services/api', mockedApi())
        .mock('viewmodels/projects/phase', mockedPhaseViewModel())
        .mock('services/projects', mockedProjects())
        .mock('viewmodels/projects/budget', mockedBudget())
        .mock('services/phaseFieldLabel', mockedPhaseFieldLabel())
        .mock('viewmodels/projects/projectclosure', mockedProjectClosure())
        .mock('viewmodels/projects/projecthealth', mockedProjectHealth())
        .mock('viewmodels/projects/expensebudget', mockedExpenseBudget())
        .store(['services/permissions',
                'services/api',
                'services/phaseenums',
                'services/enumerations',
                'viewmodels/projects/phase',
                'viewmodels/projects/phasefirstchildmodal',
                'services/projects',
                'viewmodels/projects/budget',
                'services/phaseFieldLabel',
                'viewmodels/projects/projectclosure',
                'services/dialog',
                'viewmodels/projects/projecthealth',
                'viewmodels/projects/expensebudget'])
        .require(['viewmodels/projects/phases', 'mocks'], function (model, mocks) {
            _model = model;
            _mocked.permissions = mocks.store['services/permissions'],
            _mocked.api = mocks.store['services/api'],
            _mocked.phaseenums = mocks.store['services/phaseenums'],
            _mocked.enumerations = mocks.store['services/enumerations'],
            _mocked.phase = mocks.store['viewmodels/projects/phase'],
            _mocked.phasefirstchildmodal = mocks.store['viewmodels/projects/phasefirstchildmodal'],
            _mocked.projects = mocks.store['services/projects'],
            _mocked.budget = mocks.store['viewmodels/projects/budget'],
            _mocked.phaseFieldLabel = mocks.store['services/phaseFieldLabel'],
            _mocked.projectclosure = mocks.store['viewmodels/projects/projectclosure'],
            _mocked.dialog = mocks.store['services/dialog'],
            _mocked.projecthealth = mocks.store['viewmodels/projects/projecthealth'],
            _mocked.expensebudget = mocks.store['viewmodels/projects/expensebudget']
            done();
        });
});

into this.

beforeEach(function(done) {
    return esquire.load('viewmodels/projects/phases', {
        'services/permissions': mockPermissions(),
        'services/api': mockedApi(),
        'viewmodels/projects/phase': mockedPhaseViewModel(),
        'services/projects': mockedProjects(),
        'viewmodels/projects/budget': mockedBudget(),
        'services/phaseFieldLabel': mockedPhaseFieldLabel(),
        'viewmodels/projects/projectclosure': mockedProjectClosure(),
        'viewmodels/projects/projecthealth': mockedProjectHealth(),
        'viewmodels/projects/expensebudget': mockedExpenseBudget(),
        'services/enumerations': undefined,
        'viewmodels/projects/phasefirstchildmodal': undefined,
        'services/dialog': undefined
    }).then(function (model, mocked, stored) {
        _model = model;
        _mocked = mocked;
    }).then(done);
});

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