I have an object of type PlaylistItem. In the 'real application', a PlaylistItem will always be added to a Playlist before saving.
However, I am trying to use Jasmine to test my PlaylistItem's save functionality. My first thought was to create a PlaylistItem spec and, inside of that spec, test the PlaylistItem's saving functionality.
Here is my PlaylistItem spec:
define(['playlistItem'], function(PlaylistItem) {
describe('The PlaylistItem', function() {
it("instantiates without paramaters", function() {
var playlistItem = new PlaylistItem();
expect(playlistItem).not.toEqual(null);
expect(playlistItem.isNew()).toEqual(true);
expect(playlistItem.get('id') === null);
expect(playlistItem.get('playlistId') === null);
expect(playlistItem.get('videoId') === '');
expect(playlistItem.get('title') === '');
expect(playlistItem.get('selected') === false);
expect(playlistItem.get('relatedVideos') === []);
});
it("instantiates with paramaters", function() {
var title = 'Test Title';
var playlistItem = new PlaylistItem({
title: title
});
expect(playlistItem).not.toEqual(null);
expect(playlistItem.isNew()).toEqual(true);
expect(playlistItem.get('title')).toEqual(title);
});
// TODO: Should playlistItem be responsible for testing is savability, or the playlist itself?
// I need a playlist ID to be able to test this successfully, so hard to say
it("saves", function() {
var playlistItem = null;
var title = 'Test Title';
var videoId = 'C8aBuRCcfSY';
runs(function() {
playlistItem = new PlaylistItem({
title: title,
videoId: videoId
});
playlistItem.save();
});
waitsFor(function() {
return playlistItem !== null && !playlistItem.isNew();
});
runs(function() {
expect(playlistItem.get('videoId')).toEqual(videoId);
expect(playlistItem.get('title')).toEqual(title);
});
});
});
});
and here is the accompanying PlaylistItem:
//PlaylistItems have a one-to-one relationship with a Video object via the videoId property.
define(['programState'], function(programState) {
'use strict';
var PlaylistItem = Backbone.Model.extend({
defaults: function() {
return {
//PK is a composite key of playlistId and position so that server and client can both derive PK without waiting on each other.
playlistId: null,
position: -1,
videoId: '',
title: '',
selected: false,
relatedVideos: [] //Not stoked about having these here, but doing it for convenience for now.
};
},
urlRoot: programState.getBaseUrl() + 'PlaylistItem/'
});
//Public exposure of a constructor for building new PlaylistItem objects.
return function (config) {
var playlistItem = new PlaylistItem(config);
return playlistItem;
};
});
My current save test fails because the PlaylistItem's position is -1 and its playlistId is null. Both of these fields are required.
So, my question is: Should PlaylistItem spec couple to Playlist in order to test PlaylistItem 'save,' or does this need indicate that the Playlist spec should test PlaylistItem 'save' ? The latter seems counter-intuitive, but the former seems like a code smell.
Advice appreciated, thanks.