Skip to content

Instantly share code, notes, and snippets.

@bennage
Created June 19, 2012 04:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennage/2952304 to your computer and use it in GitHub Desktop.
Save bennage/2952304 to your computer and use it in GitHub Desktop.
specs for a _simple_ 'require' function; used for managing dependencies in WinJS app (using Jasmine)
describe('The require function (or service locator)', function () {
beforeEach(function () {
});
it('should exist', function () {
expect(require).toBeDefined();
});
it('should return the Windows object when passed "Windows"', function () {
var service = require('Windows');
expect(service).toBe(Windows);
});
it('should resolve nested objects, such as "Windows.Storage.Search"', function () {
var service = require('Windows.Storage.Search');
expect(service).toBe(Windows.Storage.Search);
});
it('should throw when the service does not exist', function () {
expect(function () { require('NotReal'); }).toThrow(new Error('unable to locate NotReal'));
});
it('should return the correct error message for nested objects when the service does not exist', function () {
expect(function () { require('Not.Real.Object'); }).toThrow(new Error('unable to locate Not.Real.Object'));
});
it('should invoke a resovled function when it is marked as a factory', function () {
var target = {};
window.myFactory = function () {
return target;
};
window.myFactory.isFactory = true;
expect(require('myFactory')).toBe(target);
});
it('should not invoke a resovled function when it is not marked as a factory', function () {
var target = {};
window.myFactory = function () {
return target;
};
expect(typeof require('myFactory')).toBe('function');
});
it('should only invoke a factory once, even when required multiple times', function () {
var count = 0;
window.myFactory = function () {
count++;
return {};
};
window.myFactory.isFactory = true;
require('myFactory');
require('myFactory');
expect(count).toBe(1);
});
});
@bennage
Copy link
Author

bennage commented Jun 19, 2012

I wanted to isolate the dependency resolution from the script loading. Perhaps they shouldn't be separated though.
It's that the script loading adds a lot more complexity. In addition, and maybe the more compelling reason, WinJS already has some script loading. I want to keep the code in my current project, close to the "WinJS way" as is reasonable. I was fearful that introducing a full implementation of AMD would scare people away from an already complex topic.

@andrewdavey
Copy link

I've not played with WinJS at all yet. Have you got a link to anything new/relevant?

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