Skip to content

Commit

Permalink
adding playBasedOnTrack method to radio module
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdevwords authored and chrisdevwords committed Aug 13, 2017
1 parent 6df44a8 commit fe3a9c8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/spotify/radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ const TRACK_NAME = (artist, songName) =>
`"${songName}" by ${artist}`;

const RECCOMENDED_TRACKS_ENDPOINT = `${API_BASE}/recommendations`;
const SLACK_SUCCESS_MESSAGE = playlistName =>
`Playlist changed to ${playlistName}`;

module.exports = {

PLAYLIST_NAME,
SLACK_SUCCESS_MESSAGE,

getRecommendationsFromTrack(trackId, trackInfo, accessToken) {

Expand Down Expand Up @@ -45,7 +48,6 @@ module.exports = {

createStation(playlistUri, spotifyUri, accessToken) {


const trackId = extractFromUri(spotifyUri, 'track');
let playlistName;

Expand Down Expand Up @@ -81,5 +83,24 @@ module.exports = {
}
return Promise.reject(err)
});
},

playBasedOnTrack(playlistUri, trackUri, accessToken, spotifyLocalUrl) {

return this
.createStation(playlistUri, trackUri, accessToken)
.then(() =>
request
.post({
uri: `${spotifyLocalUrl}/api/spotify/playlist`,
body: {
playlist: playlistUri
},
json: true
})
)
.then(resp =>
SLACK_SUCCESS_MESSAGE(resp.playlist.title)
);
}
};
58 changes: 58 additions & 0 deletions test/spotify/radio/test_playBasedOnTrack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const { beforeEach, afterEach, describe, it } = require('mocha');
const { expect, config } = require('chai');
const request = require('request-promise-native');
const sinon = require('sinon');
const radio = require('../../../src/spotify/radio');


const context = describe;

config.includeStack = true;

describe('The spotify.radio.playBasedOnTrack method', () => {

context('with a working spotifyLocal connection', () => {

const playlistName = radio.PLAYLIST_NAME(
'"Everyone" by Van Morrison'
);

beforeEach(() => {
sinon
.stub(radio, 'createStation')
.resolves({})

sinon
.stub(request, 'post')
.resolves({
playlist: {
title: playlistName
}
})

});

afterEach(() => {
radio.createStation.restore();
request.post.restore();
});

it('resolves with a message for slack', (done) => {

const trackUri = 'spotify:track:528kEbmXBOuMbxdn7YQAXx';
const spotifyLocalUrl = 'http://localhost:5000';
const playlistUri = 'spotify:user:awpoops:playlist:5PP1I2m0uxEBb3VKLhI7bP';
const token = 'BQDCP3FOWeE8e9a9lKUeu4JB0o1eN8UQzLvsWiyXgcr9RaPhM3o8L7XLXcopGnzl3vK4w8R_hlzy2-MW6x0PPS14b7CDMw_ctMcLUvWPLYEokMHKd9ENL-_fLwsRVhQFyHphY90hVYYthsizEWy8jdxMT_uptqf2auwAu6zhowkCxSYW9qG6F62_AkLQL5qu1t64DOmp4X8NtEP84ZGGiw11lbvGPl_wf6qS2ADuedQqxVSNoL4';

radio
.playBasedOnTrack(playlistUri, trackUri, token, spotifyLocalUrl)
.then((message) => {
expect(message).to.equal(
radio.SLACK_SUCCESS_MESSAGE(playlistName)
)
done();
})
.catch(done);
})
});
});

0 comments on commit fe3a9c8

Please sign in to comment.