Skip to content

Commit

Permalink
add tests and fix for #94
Browse files Browse the repository at this point in the history
  • Loading branch information
englercj committed Jun 29, 2017
1 parent e443070 commit 7275c86
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -31,7 +31,6 @@
"build": "browserify -d -s Loader -e ./lib/index.js -o ./dist/resource-loader.js",
"lib": "babel src --out-dir lib -s",
"minify": "uglifyjs --output ./dist/resource-loader.min.js -- ./dist/resource-loader.js",
"dev": "watchify -d -s Loader -e ./src/index.js -o ./dist/resource-loader.js",
"lint": "eslint src/ test/",
"start": "npm run full-build",
"pretest": "npm run full-build",
Expand Down
17 changes: 14 additions & 3 deletions src/Resource.js
Expand Up @@ -59,6 +59,9 @@ export default class Resource {
* element to use for loading, instead of creating one.
* @param {boolean} [options.metadata.skipSource=false] - Skips adding source(s) to the load element. This
* is useful if you want to pass in a `loadElement` that you already added load sources to.
* @param {string|string[]} [options.metadata.mimeType] - The mime type to use for the source element of a video/audio
* elment. If the urls are an array, you can pass this as an array as well where each index is the mime type to
* use for the corresponding url index.
*/
constructor(name, url, options) {
if (typeof name !== 'string' || typeof url !== 'string') {
Expand Down Expand Up @@ -549,12 +552,20 @@ export default class Resource {
this.data.src = Array.isArray(this.url) ? this.url[0] : this.url;
}
else if (Array.isArray(this.url)) {
const mimeTypes = this.metadata.mimeType;

for (let i = 0; i < this.url.length; ++i) {
this.data.appendChild(this._createSource(type, this.url[i]));
this.data.appendChild(
this._createSource(type, this.url[i], Array.isArray(mimeTypes) ? mimeTypes[i] : mimeTypes)
);
}
}
else {
this.data.appendChild(this._createSource(type, this.url));
const mimeTypes = this.metadata.mimeType;

this.data.appendChild(
this._createSource(type, this.url, Array.isArray(mimeTypes) ? mimeTypes[0] : mimeTypes)
);
}
}

Expand Down Expand Up @@ -642,7 +653,7 @@ export default class Resource {
*/
_createSource(type, url, mime) {
if (!mime) {
mime = `${type}/${url.substr(url.lastIndexOf('.') + 1)}`;
mime = `${type}/${this._getExtension(url)}`;
}

const source = document.createElement('source');
Expand Down
53 changes: 53 additions & 0 deletions test/unit/Resource.test.js
Expand Up @@ -390,9 +390,62 @@ describe('Resource', () => {
res.url = 'http://nowhere.me/image.jpeg?query=movie.wmv&file=data.json';
expect(res._getExtension()).to.equal('jpeg');

res.url = 'http://nowhere.me/image.jpeg?query=movie.wmv&file=data.json#/derp.mp3';
expect(res._getExtension()).to.equal('jpeg');

res._setFlag(Resource.STATUS_FLAGS.DATA_URL, true);
res.url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAMSURBVBhXY2BgYAAAAAQAAVzN/2kAAAAASUVORK5CYII='; // eslint-disable-line max-len
expect(res._getExtension()).to.equal('png');
});
});

describe('#_createSource', () => {
it('Should return the correct src url', () => {
res.url = 'http://www.google.com/audio.mp3';
expect(res._createSource('audio', res.url)).to.have.property('src', res.url);

res.url = 'http://domain.net/really/deep/path/that/goes/for/a/while/movie.wmv';
expect(res._createSource('video', res.url)).to.have.property('src', res.url);

res.url = 'http://somewhere.io/path.with.dots/and_a-bunch_of.symbols/audio.mp3';
expect(res._createSource('audio', res.url)).to.have.property('src', res.url);

res.url = 'http://nowhere.me/audio.mp3?query=true&string=false&name=real';
expect(res._createSource('audio', res.url)).to.have.property('src', res.url);

res.url = 'http://nowhere.me/audio.mp3?query=movie.wmv&file=data.json';
expect(res._createSource('audio', res.url)).to.have.property('src', res.url);

res.url = 'http://nowhere.me/audio.mp3?query=movie.wmv&file=data.json';
expect(res._createSource('audio', res.url)).to.have.property('src', res.url);

res._setFlag(Resource.STATUS_FLAGS.DATA_URL, true);
res.url = 'data:audio/wave;base64,UklGRjIAAABXQVZFZm10IBIAAAABAAEAQB8AAEAfAAABAAgAAABmYWN0BAAAAAAAAABkYXRhAAAAAA=='; // eslint-disable-line max-len
expect(res._createSource('audio', res.url)).to.have.property('src', res.url);
});

it('Should correctly auto-detect the mime type', () => {
res.url = 'http://www.google.com/audio.mp3';
expect(res._createSource('audio', res.url)).to.have.property('type', 'audio/mp3');

res.url = 'http://domain.net/really/deep/path/that/goes/for/a/while/movie.wmv';
expect(res._createSource('video', res.url)).to.have.property('type', 'video/wmv');

res.url = 'http://somewhere.io/path.with.dots/and_a-bunch_of.symbols/audio.mp3';
expect(res._createSource('audio', res.url)).to.have.property('type', 'audio/mp3');

res.url = 'http://nowhere.me/audio.mp3?query=true&string=false&name=real';
expect(res._createSource('audio', res.url)).to.have.property('type', 'audio/mp3');

res.url = 'http://nowhere.me/audio.mp3?query=movie.wmv&file=data.json';
expect(res._createSource('audio', res.url)).to.have.property('type', 'audio/mp3');

res.url = 'http://nowhere.me/audio.mp3?query=movie.wmv&file=data.json';
expect(res._createSource('audio', res.url)).to.have.property('type', 'audio/mp3');

res._setFlag(Resource.STATUS_FLAGS.DATA_URL, true);
res.url = 'data:audio/wave;base64,UklGRjIAAABXQVZFZm10IBIAAAABAAEAQB8AAEAfAAABAAgAAABmYWN0BAAAAAAAAABkYXRhAAAAAA=='; // eslint-disable-line max-len
expect(res._createSource('audio', res.url)).to.have.property('type', 'audio/wave');
});
});
});

0 comments on commit 7275c86

Please sign in to comment.