Skip to content

Commit

Permalink
#9 adding pause, resume command w/ coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdevwords authored and chrisdevwords committed Apr 29, 2017
1 parent 9cda129 commit dfe5210
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 8 deletions.
53 changes: 46 additions & 7 deletions src/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
PL_SET,
QUEUE,
SHUFFLING,
NOT_SHUFFLING
NOT_SHUFFLING,
PAUSED,
RESUMED,
} from './slack-resp';

let _apiRoot;
Expand Down Expand Up @@ -90,15 +92,16 @@ export function skipTrack() {

}

export function getPlaying() {

const uri = `${_apiRoot}/api/spotify/playing/`;

function getPlayingTrack() {
return request
.get({
uri,
uri:`${_apiRoot}/api/spotify/playing/`,
json: true
})
});
}

export function getPlaying() {
return getPlayingTrack()
.then(({ track }) =>
// eslint-disable-next-line babel/new-cap
NOW_PLAYING(track)
Expand Down Expand Up @@ -164,6 +167,38 @@ export function toggleShuffle() {
});
}

export function pause() {
return getPlayingTrack()
.then(({ track }) =>
request
.post({
uri: `${_apiRoot}/api/spotify/pause`,
json: true,
body: {
paused: true
}
})
// eslint-disable-next-line babel/new-cap
.then(() => PAUSED(track))
);
}

export function resume() {
return getPlayingTrack()
.then(({ track }) =>
request
.post({
uri: `${_apiRoot}/api/spotify/pause`,
json: true,
body: {
paused: false
}
})
// eslint-disable-next-line babel/new-cap
.then(() => RESUMED(track))
);
}

export function exec({ text, user_name, command }) {

let error;
Expand All @@ -184,6 +219,10 @@ export function exec({ text, user_name, command }) {
return getQueue();
case '/shuffle':
return toggleShuffle();
case '/pause':
return pause();
case '/resume':
return resume();
case '/playlist':
if (text) {
return setPlaylist(text);
Expand Down
8 changes: 8 additions & 0 deletions src/slack-resp.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export const SKIPPED = (current, { name, requestedBy }) =>
export const ALBUM_ADDED = (position, { artist, name }, by) =>
`Album "${name}" by ${artist} queued by ${by} at position ${position}`;

export const PAUSED = track =>
// eslint-disable-next-line babel/new-cap
`Pausing ${TRACK(track)}.`;

export const RESUMED = track =>
// eslint-disable-next-line babel/new-cap
`Resuming ${TRACK(track)}.`;

export function printQueue(tracks) {
return tracks
.map(TRACK)
Expand Down
74 changes: 73 additions & 1 deletion test/test_commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
ALBUM_ADDED,
NOW_PLAYING,
SHUFFLING,
NOT_SHUFFLING
NOT_SHUFFLING,
PAUSED,
RESUMED
} from '../src/slack-resp';
const { beforeEach, afterEach, describe, it } = mocha;
const { expect, config } = chai;
Expand Down Expand Up @@ -185,4 +187,74 @@ describe('The Slack commands for Spotify Local ', () => {
});
});
});

describe('the /pause command', () => {

const command = '/pause';

beforeEach((done) => {
sinon.stub(request, 'get')
.callsFake(() =>
openMock('local/spotify/api/playing')
);
sinon.stub(request, 'post')
.resolves({ paused: true });
done();
});

afterEach((done) => {
request.get.restore();
request.post.restore();
done();
});

it('resolves with text for slack', (done) => {
exec({ command })
.then((text) => {
expect(text).to.be.a('string');
expect(text).to.eq(PAUSED({
name: 'Sax Attack',
artist: 'Kenny G',
requestedBy: 'Default Playlist'
}));
done();
})
.catch(done);
});
});

describe('the /resume command', () => {

const command = '/resume';

beforeEach((done) => {
sinon.stub(request, 'get')
.callsFake(() =>
openMock('local/spotify/api/playing')
);
sinon.stub(request, 'post')
.resolves({ paused: false });
done();
});

afterEach((done) => {
request.get.restore();
request.post.restore();
done();
});

it('resolves with text for slack', (done) => {
exec({ command })
.then((text) => {
expect(text).to.be.a('string');
expect(text).to.eq(RESUMED({
name: 'Sax Attack',
artist: 'Kenny G',
requestedBy: 'Default Playlist'
}));
done();
})
.catch(done);
});
});
});

0 comments on commit dfe5210

Please sign in to comment.