diff --git a/test/unit/Loader.test.js b/test/unit/Loader.test.js index 727222b..8966dd0 100644 --- a/test/unit/Loader.test.js +++ b/test/unit/Loader.test.js @@ -1,5 +1,4 @@ -var url = 'http://localhost/file', - loader = null; +var loader = null; describe('Loader', function () { beforeEach(function () { @@ -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); }); });