Skip to content

Commit

Permalink
add more tests for different .add() syntaxes
Browse files Browse the repository at this point in the history
  • Loading branch information
englercj committed Mar 4, 2015
1 parent 901c0d7 commit 54c602a
Showing 1 changed file with 188 additions and 5 deletions.
193 changes: 188 additions & 5 deletions test/unit/Loader.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var url = 'http://localhost/file',
loader = null;
var loader = null;

describe('Loader', function () {
beforeEach(function () {
Expand All @@ -21,12 +20,196 @@ describe('Loader', function () {
});

describe('#add', function () {
it('should add a resource to the queue', function () {
loader.add('test-resource', 'http://google.com');
var name = 'test-resource',
url = 'http://localhost/file',
options = {
crossOrigin: true,
loadType: Resource.LOAD_TYPE.IMAGE,
xhrType: Resource.XHR_RESPONSE_TYPE.DOCUMENT
},
callback = function () {};

it('creates a resource using all arguments', function () {
loader.add(name, url, options, callback);

expect(loader.queue.length()).to.equal(0);
expect(loader._buffer).to.have.length(1);

var res = loader._buffer[0];

expect(res).to.be.an.instanceOf(Resource);
expect(res).to.have.property('name', name);
expect(res).to.have.property('url', url);
expect(res).to.have.property('crossOrigin', options.crossOrigin);
expect(res).to.have.property('loadType', options.loadType);
expect(res).to.have.property('xhrType', options.xhrType);
expect(res).to.have.property('_events')
.that.has.property('afterMiddleware')
.that.is.a('function');
});

it('creates a resource with just name, url, and options', function () {
loader.add(name, url, options);

expect(loader.queue.length()).to.equal(0);
expect(loader._buffer).to.have.length(1);

var res = loader._buffer[0];

expect(res).to.be.an.instanceOf(Resource);
expect(res).to.have.property('name', name);
expect(res).to.have.property('url', url);
expect(res).to.have.property('crossOrigin', options.crossOrigin);
expect(res).to.have.property('loadType', options.loadType);
expect(res).to.have.property('xhrType', options.xhrType);
});

it('creates a resource with just name, url, and a callback', function () {
loader.add(name, url, callback);

expect(loader.queue.length()).to.equal(0);
expect(loader._buffer).to.have.length(1);

var res = loader._buffer[0];

expect(res).to.be.an.instanceOf(Resource);
expect(res).to.have.property('name', name);
expect(res).to.have.property('url', url);
expect(res).to.have.property('_events')
.that.has.property('afterMiddleware')
.that.is.a('function');
});

it('creates a resource with just name and url', function () {
loader.add(name, url);

expect(loader.queue.length()).to.equal(0);
expect(loader._buffer).to.have.length(1);

var res = loader._buffer[0];

expect(res).to.be.an.instanceOf(Resource);
expect(res).to.have.property('name', name);
expect(res).to.have.property('url', url);
});

it('creates a resource with just url, options, and a callback', function () {
loader.add(url, options, callback);

expect(loader.queue.length()).to.equal(0);
expect(loader._buffer).to.have.length(1);
expect(loader._buffer[0]).to.be.an.instanceOf(Resource);

var res = loader._buffer[0];

expect(res).to.be.an.instanceOf(Resource);
expect(res).to.have.property('name', url);
expect(res).to.have.property('url', url);
expect(res).to.have.property('crossOrigin', options.crossOrigin);
expect(res).to.have.property('loadType', options.loadType);
expect(res).to.have.property('xhrType', options.xhrType);
expect(res).to.have.property('_events')
.that.has.property('afterMiddleware')
.that.is.a('function');
});

it('creates a resource with just url and options', function () {
loader.add(url, options);

expect(loader.queue.length()).to.equal(0);
expect(loader._buffer).to.have.length(1);

var res = loader._buffer[0];

expect(res).to.be.an.instanceOf(Resource);
expect(res).to.have.property('name', url);
expect(res).to.have.property('url', url);
expect(res).to.have.property('crossOrigin', options.crossOrigin);
expect(res).to.have.property('loadType', options.loadType);
expect(res).to.have.property('xhrType', options.xhrType);
});

it('creates a resource with just url and a callback', function () {
loader.add(url, callback);

expect(loader.queue.length()).to.equal(0);
expect(loader._buffer).to.have.length(1);

var res = loader._buffer[0];

expect(res).to.be.an.instanceOf(Resource);
expect(res).to.have.property('name', url);
expect(res).to.have.property('url', url);
expect(res).to.have.property('_events')
.that.has.property('afterMiddleware')
.that.is.a('function');
});

it('creates a resource with just url', function () {
loader.add(url);

expect(loader.queue.length()).to.equal(0);
expect(loader._buffer).to.have.length(1);

var res = loader._buffer[0];

expect(res).to.be.an.instanceOf(Resource);
expect(res).to.have.property('name', url);
expect(res).to.have.property('url', url);
});

it('creates a resource with just an object (name/url keys) and callback param', function () {
loader.add({ name: name, url: url }, callback);

expect(loader.queue.length()).to.equal(0);
expect(loader._buffer).to.have.length(1);

var res = loader._buffer[0];

expect(res).to.be.an.instanceOf(Resource);
expect(res).to.have.property('name', name);
expect(res).to.have.property('url', url);
expect(res).to.have.property('_events')
.that.has.property('afterMiddleware')
.that.is.a('function');
});

it('creates a resource with just an object (name/url/callback keys)', function () {
loader.add({ name: name, url: url, onComplete: callback });

expect(loader.queue.length()).to.equal(0);
expect(loader._buffer).to.have.length(1);

var res = loader._buffer[0];

expect(res).to.be.an.instanceOf(Resource);
expect(res).to.have.property('name', name);
expect(res).to.have.property('url', url);
expect(res).to.have.property('_events')
.that.has.property('afterMiddleware')
.that.is.a('function');
});

it('creates a resource with just an object (url/callback keys)', function () {
loader.add({ url: url, onComplete: callback });

expect(loader.queue.length()).to.equal(0);
expect(loader._buffer).to.have.length(1);

var res = loader._buffer[0];

expect(res).to.be.an.instanceOf(Resource);
expect(res).to.have.property('name', url);
expect(res).to.have.property('url', url);
expect(res).to.have.property('_events')
.that.has.property('afterMiddleware')
.that.is.a('function');
});

it('throws an error if url isn\'t passed', function () {
expect(loader.add).to.throw(Error);
expect(function () { loader.add(options); }).to.throw(Error);
expect(function () { loader.add(callback); }).to.throw(Error);
expect(function () { loader.add(options, callback); }).to.throw(Error);
});
});

Expand Down

0 comments on commit 54c602a

Please sign in to comment.