Skip to content

Commit

Permalink
Trying to tackle some TODOs and add test cases while I'm at it.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeoMix committed May 11, 2015
1 parent 3a3d525 commit 8b2ed74
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 29 deletions.
7 changes: 2 additions & 5 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,7 @@ module.exports = function(grunt) {
cwd: 'dist/',
src: '**/*',
dest: '<%= meta.releaseDirectory %>'
}
},
concat: {
// TODO: This isn't really a concat anymore. Just a copy operation.
},
// Content scripts don't use RequireJS so they need to be concatenated and moved to dist with a separate task
contentScripts: {
files: {
Expand Down Expand Up @@ -237,7 +234,7 @@ module.exports = function(grunt) {
grunt.task.run('replace:updateVersion');
}

grunt.task.run('requirejs', 'replace:transformManifest', 'replace:localDebug', 'concat:contentScripts', 'less', 'imagemin', 'clean:dist');
grunt.task.run('requirejs', 'replace:transformManifest', 'replace:localDebug', 'copy:contentScripts', 'less', 'imagemin', 'clean:dist');

// Build chrome release
grunt.task.run('compressRelease:' + chromeReleaseDirectory);
Expand Down
42 changes: 18 additions & 24 deletions src/js/background/collection/searchResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,50 @@
userFriendlyName: chrome.i18n.getMessage('searchResults'),

addSongs: function(songs) {
var searchResults = this._songsAsSearchResults(songs);
this.add(searchResults);

if (searchResults.length > 0) {
if (songs.length > 0) {
var searchResults = this._songsAsSearchResults(songs);
this.add(searchResults);

// Emit a custom event signaling items have been added.
// Useful for not responding to add until all items have been added.
this.trigger('add:completed', this);
}
},

// Returns the collection of SearchResults' underlying songs.
// Returns an array of Song models corresponding to the current collection of SearchResults
getSongs: function() {
return this.map(function(model) {
return model.get('song');
});
},

// Reset the collection's values by mapping a single or list of Song objects into
// JSON objects which will be instantiated by Backbone.
// Reset the collection with SearchResults derived from a collection, array, or individual Song
resetSongs: function(songs) {
var searchResults = this._songsAsSearchResults(songs);
this.reset(searchResults);
},

// TODO: Maybe return non-JSON? Would that make this shorter?
// Takes a collection, array, or individual Song model and returns an array of SearchResult models
_songsAsSearchResults: function(songs) {
var searchResults = [];

if (songs instanceof Backbone.Collection) {
searchResults = songs.map(function(song) {
return {
song: song,
title: song.get('title')
};
});
searchResults = songs.map(this._songAsSearchResult.bind(this));
} else if (_.isArray(songs)) {
searchResults = _.map(songs, function(song) {
return {
song: song,
title: song.get('title')
};
});
searchResults = _.map(songs, this._songAsSearchResult.bind(this));
} else {
searchResults.push({
song: songs,
title: songs.get('title')
});
searchResults.push(this._songAsSearchResult(songs));
}

return searchResults;
},

// Takes an individual Song model and returns a SearchResult model
_songAsSearchResult: function(song) {
return new SearchResult({
song: song,
title: song.get('title')
});
}
});

Expand Down
73 changes: 73 additions & 0 deletions src/js/test/background/collection/searchResultsTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
define(function(require) {
'use strict';

var Songs = require('background/collection/songs');
var SearchResults = require('background/collection/searchResults');
var TestUtility = require('test/testUtility');

describe('SearchResults', function() {
beforeEach(function() {
this.searchResults = new SearchResults();
});

it('should be able to parse a song into a search result', function() {
var song = TestUtility.buildSong();
var searchResult = this.searchResults._songAsSearchResult(song);

expect(searchResult.get('title')).to.equal(song.get('title'));
expect(searchResult.get('song')).to.equal(song);
});

it('should be able to parse an array of songs into an array of search results', function() {
var songs = [TestUtility.buildSong('N92CLZlsNRw'), TestUtility.buildSong('bek1y2uiQGA')];
var searchResults = this.searchResults._songsAsSearchResults(songs);

expect(searchResults.length).to.equal(2);
expect(searchResults[0].get('song')).to.equal(songs[0]);
expect(searchResults[1].get('song')).to.equal(songs[1]);
});

it('should be able to parse a collection of songs into an array of search results', function() {
var songs = new Songs([TestUtility.buildSong('N92CLZlsNRw'), TestUtility.buildSong('bek1y2uiQGA')]);
var searchResults = this.searchResults._songsAsSearchResults(songs);

expect(searchResults.length).to.equal(2);
expect(searchResults[0].get('song')).to.equal(songs.at(0));
expect(searchResults[1].get('song')).to.equal(songs.at(1));
});

it('should be able to parse a single song into an array of search results', function() {
var song = TestUtility.buildSong();
var searchResults = this.searchResults._songsAsSearchResults(song);

expect(searchResults.length).to.equal(1);
expect(searchResults[0].get('song')).to.equal(song);
});

it('should be able to add songs', function() {
var songs = new Songs([TestUtility.buildSong('N92CLZlsNRw'), TestUtility.buildSong('bek1y2uiQGA')]);
this.searchResults.addSongs(songs);

expect(this.searchResults.length).to.equal(songs.length);
});

it('should trigger an add:completed event after adding songs', function() {
sinon.spy(this.searchResults, 'trigger');
var songs = new Songs([TestUtility.buildSong('N92CLZlsNRw'), TestUtility.buildSong('bek1y2uiQGA')]);
this.searchResults.addSongs(songs);

expect(this.searchResults.trigger.calledWith('add:completed')).to.equal(true);
this.searchResults.trigger.restore();
});

it('should be able to get songs', function() {
var songs = new Songs([TestUtility.buildSong('N92CLZlsNRw'), TestUtility.buildSong('bek1y2uiQGA')]);
this.searchResults.addSongs(songs);

var retrievedSongs = this.searchResults.getSongs();
expect(retrievedSongs.length).to.equals(songs.length);
expect(retrievedSongs[0]).to.equal(songs.at(0));
expect(retrievedSongs[1]).to.equal(songs.at(1));
});
});
});
1 change: 1 addition & 0 deletions src/js/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
var DataSourceTests = require('test/background/model/dataSourceTests');
var UtilityTests = require('test/common/model/utilityTests');
var YouTubeV3APITests = require('test/background/model/youTubeV3APITests');
var SearchResultsTests = require('test/background/collection/searchResultsTests');

var BrowserSettingsDialogViewTest = require('test/foreground/view/dialog/browserSettingsDialogViewTest');
var BrowserSettingsViewTest = require('test/foreground/view/dialog/browserSettingsViewTest');
Expand Down

0 comments on commit 8b2ed74

Please sign in to comment.