Skip to content

Commit 094a840

Browse files
committed
Fix create playlist test and implementation
1 parent a95f7f5 commit 094a840

File tree

4 files changed

+32
-20
lines changed

4 files changed

+32
-20
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Documentation updates to keep up to date with ES6, thanks [@dandv](https://github.com/dandv)!
1010
* `reason` added to error object, which is returned by the [Player API](https://developer.spotify.com/documentation/web-api/reference/player/). Much appreciated [@konstantinjdobler](https://github.com/konstantinjdobler)!
1111
* Binding for [Add an Item to the User's Playback Queue](https://developer.spotify.com/documentation/web-api/reference/player/add-to-queue/) endpoint added. Thanks [@thattomperson](https://github.com/thattomperson) and [@AriciducaZagaria](https://github.com/AriciducaZagaria)!
12+
* Update the Create Playlist endpoint to avoid using `user_id` as it's no longer necessary. Cheers [@marinacrachi](https://github.com/marinacrachi)!
1213

1314
More coming before NPM release.
1415

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ The library includes helper functions to do the following:
9292

9393
#### Player
9494

95+
* Add an Item to the User's Playback Queue
9596
* Get a user's available devices
9697
* Get information about the user's current playback
9798
* Get current user’s recently played tracks
@@ -717,6 +718,9 @@ spotifyApi.getPlaylistsForCategory('party', {
717718

718719
/* Player */
719720

721+
// Add an Item to the User's Playback Queue
722+
// TBD
723+
720724
// Get a User's Available Devices
721725
// TBD
722726

__tests__/spotify-web-api.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,7 @@ describe('Spotify Web API', () => {
15131513
});
15141514

15151515
test('should create a playlist', function(done) {
1516-
sinon.stub(HttpManager, '_makeRequest', function(
1516+
sinon.stub(HttpManager, '_makeRequest').callsFake(function(
15171517
method,
15181518
options,
15191519
uri,
@@ -1578,8 +1578,7 @@ describe('Spotify Web API', () => {
15781578

15791579
api.createPlaylist(
15801580
'My Cool Playlist',
1581-
{ description: 'It\'s really cool' },
1582-
{ public: false },
1581+
{ description: 'It\'s really cool', public: false },
15831582
function(err, data) {
15841583
expect(data.body.name).toBe('My Cool Playlist');
15851584
expect(data.body.description).toBe('It\s really cool');
@@ -1590,8 +1589,8 @@ describe('Spotify Web API', () => {
15901589
);
15911590
});
15921591

1593-
test('should create a playlist using callback with user id', done => {
1594-
sinon.stub(HttpManager, '_makeRequest', function(
1592+
test('should create a playlist using callback with user id (backward compatability)', done => {
1593+
sinon.stub(HttpManager, '_makeRequest').callsFake(function(
15951594
method,
15961595
options,
15971596
uri,
@@ -1601,19 +1600,21 @@ describe('Spotify Web API', () => {
16011600
expect(uri).toBe(
16021601
'https://api.spotify.com/v1/me/playlists'
16031602
);
1604-
expect(JSON.parse(options.data)).toEqual({ name: 'My Cool Playlist' });
1605-
callback(null, { body: { name: 'My Cool Playlist' }, statusCode: 200 });
1603+
expect(JSON.parse(options.data)).toEqual({ name: 'My Cool Playlist', public : false });
1604+
callback(null, { body: { name: 'My Cool Playlist', public: false }, statusCode: 200 });
16061605
expect(options.query).toBeFalsy();
16071606
});
16081607

16091608
var api = new SpotifyWebApi();
16101609

1611-
api.createPlaylist('thelinmichael', 'My Cool Playlist', {},
1612-
function(err, data) {
1613-
expect(data.body.name).toBe('My Cool Playlist');
1614-
expect(data.statusCode).toBe(200);
1615-
done();
1616-
});
1610+
api.createPlaylist('thelinmichael', 'My Cool Playlist', { 'public' : false },
1611+
function(err, data) {
1612+
expect(data.body.name).toBe('My Cool Playlist');
1613+
expect(data.body.description).toBeFalsy();
1614+
expect(data.body.public).toBe(false);
1615+
expect(data.statusCode).toBe(200);
1616+
done();
1617+
});
16171618
});
16181619

16191620
test('should change playlist details', done => {
@@ -1814,7 +1815,7 @@ describe('Spotify Web API', () => {
18141815
});
18151816

18161817
test("should add songs to the user's queue:", done => {
1817-
sinon.stub(HttpManager, '_makeRequest', function(
1818+
sinon.stub(HttpManager, '_makeRequest').callsFake(function(
18181819
method,
18191820
options,
18201821
uri,

src/spotify-web-api.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -517,16 +517,22 @@ SpotifyWebApi.prototype = {
517517
*/
518518
createPlaylist: function(name, options, callback) {
519519
// In case someone is using a version where user id was required
520+
var actualOptions = {};
521+
var actualCallback = null;
520522
if (typeof options === 'string') {
521-
options = callback;
522-
callback = arguments[3];
523+
actualOptions = callback || {};
524+
actualOptions.name = options;
525+
actualCallback = arguments[3];
526+
} else {
527+
actualOptions = options || {};
528+
actualCallback = callback;
529+
actualOptions.name = name;
523530
}
524-
options.name = name;
525-
531+
526532
return WebApiRequest.builder(this.getAccessToken())
527533
.withPath('/v1/me/playlists')
528534
.withHeaders({ 'Content-Type': 'application/json' })
529-
.withBodyParameters(options)
535+
.withBodyParameters(actualOptions)
530536
.build()
531537
.execute(HttpManager.post, actualCallback);
532538
},
@@ -1020,7 +1026,6 @@ SpotifyWebApi.prototype = {
10201026
},
10211027

10221028
/**
1023-
10241029
* Add track or episode to device queue
10251030
* @param {string} [uri] uri of the track or episode to add
10261031
* @param {Object} [options] Options, being device_id.
@@ -1042,6 +1047,7 @@ SpotifyWebApi.prototype = {
10421047
},
10431048

10441049

1050+
/**
10451051
* Get the Current User's Available Devices
10461052
* @param {requestCallback} [callback] Optional callback method to be called instead of the promise.
10471053
* @returns {Promise|undefined} A promise that if successful, resolves into an array of device objects,

0 commit comments

Comments
 (0)