From 5dc2b6b3b0e959fee33ea39cfbd10a0c7a07cad5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20M=2E=20P=C3=A9rez?= Date: Wed, 12 Sep 2018 15:52:24 +0200 Subject: [PATCH] Handle empty user in getUserPlaylists() --- __tests__/spotify-web-api.js | 35 +++++++++++++++++++++++++++++++++++ src/spotify-web-api.js | 6 +++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/__tests__/spotify-web-api.js b/__tests__/spotify-web-api.js index 1340161e..39c20ec4 100644 --- a/__tests__/spotify-web-api.js +++ b/__tests__/spotify-web-api.js @@ -1447,6 +1447,41 @@ describe('Spotify Web API', () => { }); }); + test('should get the current users playlists with options', done => { + sinon.stub(HttpManager, '_makeRequest', function( + method, + options, + uri, + callback + ) { + expect(method).toBe(superagent.get); + expect(uri).toBe('https://api.spotify.com/v1/me/playlists'); + expect(options.query).toEqual({ limit: 27, offset: 7 }); + callback(null, { + body: { + items: [ + { + uri: 'spotify:user:thelinmichael:playlist:5ieJqeLJjjI8iJWaxeBLuK' + }, + { + uri: 'spotify:user:thelinmichael:playlist:3EsfV6XzCHU8SPNdbnFogK' + } + ] + }, + statusCode: 200 + }); + }); + + var api = new SpotifyWebApi(); + api.setAccessToken('myVeryLongAccessToken'); + + api.getUserPlaylists({ limit: 27, offset: 7 }).then(function(data) { + expect(2).toBe(data.body.items.length); + expect(data.statusCode).toBe(200); + done(); + }); + }); + test('should get a playlist', done => { sinon.stub(HttpManager, '_makeRequest', function( method, diff --git a/src/spotify-web-api.js b/src/spotify-web-api.js index 60b2906f..b79a5f1c 100644 --- a/src/spotify-web-api.js +++ b/src/spotify-web-api.js @@ -457,7 +457,11 @@ SpotifyWebApi.prototype = { var path; if (typeof userId === 'string') { path = '/v1/users/' + encodeURIComponent(userId) + '/playlists'; - } else { + } else if (typeof userId === 'object') { + callback = options; + options = userId; + path = '/v1/me/playlists'; + } /* undefined */ else { path = '/v1/me/playlists'; }