Skip to content

Commit

Permalink
Refactor search methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreKavalerski committed Dec 24, 2017
1 parent 3ab7998 commit 6e3252e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 73 deletions.
9 changes: 7 additions & 2 deletions examples/albums.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { searchAlbums } from '../src/main';
/* to run: babel-node album.js */
import SpotifyWrapper from '../src/index';

global.fetch = require('node-fetch');

const albums = searchAlbums('Incubus');
const spotify = new SpotifyWrapper({
token: 'BQDsad0YtusZVbU6SW3h2Og2jhaPoWul52J5DU6T6wAkf839-Fnu6_qoy7hS0twLB1dxgq28z1K1s3nd3MClQ5laAFEPEXMnl6VaL0lS6_7hwA5DKOoi0fQyW6BdkrEw7_cW7O5CzDdzQnI4FuA',
});

const albums = spotify.search.albums('Charlie Brown Jr');

albums.then(data => data.albums.items.map(item => console.log(item.name)));
13 changes: 4 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import {
search,
searchAlbums,
searchArtists,
searchPlaylists,
searchTracks,
} from './search';

import album from './album';
import artist from './artist';
import search from './search';

import { API_URL } from './config';
import toJSON from './utils';

/* global fetch */

Expand All @@ -20,6 +14,7 @@ export default class SpotifyWrapper {

this.album = album.bind(this)();
this.artist = artist.bind(this)();
this.search = search.bind(this)();
}

request(url) {
Expand All @@ -28,6 +23,6 @@ export default class SpotifyWrapper {
Authorization: `Bearer ${this.token}`,
},
};
return fetch(url, headers);
return fetch(url, headers).then(toJSON);
}
}
23 changes: 11 additions & 12 deletions src/search.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { API_URL, HEADERS } from './config';
import toJSON from './utils';
/* global fetch */
function searcher(type, query) {
return this.request(`${this.apiURL}/search?q=${query}&type=${type}`);
}

export const search = (query, type) =>
fetch(`${API_URL}/search?q=${query}&type=${type}`, HEADERS)
.then(toJSON);

export const searchArtists = query => search(query, 'artist');

export const searchAlbums = query => search(query, 'album');
export const searchTracks = query => search(query, 'track');
export const searchPlaylists = query => search(query, 'playlist');
export default function search() {
return {
artists: searcher.bind(this, 'artist'),
albums: searcher.bind(this, 'album'),
tracks: searcher.bind(this, 'track'),
playlists: searcher.bind(this, 'playlist'),
};
}
71 changes: 21 additions & 50 deletions tests/search.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import chai, { expect } from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import sinonStubPromise from 'sinon-stub-promise';
import { search, searchAlbums, searchArtists, searchTracks, searchPlaylists } from '../src/search';
import API_URL from '../src/config';

import SpotifyWrapper from '../src/index';

chai.use(sinonChai);
sinonStubPromise(sinon);
Expand All @@ -13,10 +11,14 @@ sinonStubPromise(sinon);
global.fetch = require('node-fetch');

describe('Search', () => {
let spotify;
let fetchedStub;
let promise;

beforeEach(() => {
spotify = new SpotifyWrapper({
token: 'foo',
});
fetchedStub = sinon.stub(global, 'fetch');
promise = fetchedStub.returnsPromise();
});
Expand All @@ -25,107 +27,76 @@ describe('Search', () => {
});

describe('smoke tests', () => {
it('should exist the search method', () => {
expect(search).to.exist;
});
it('should exist the searchAlbums method', () => {
expect(searchAlbums).to.exist;
expect(spotify.search.albums).to.exist;
});
it('should exist the searchArtists method', () => {
expect(searchArtists).to.exist;
expect(spotify.search.artists).to.exist;
});
it('should exist the searchTracks method', () => {
expect(searchTracks).to.exist;
expect(spotify.search.tracks).to.exist;
});
it('should exist the searchPlaylists method', () => {
expect(searchPlaylists).to.exist;
});
});

describe('Generic Search', () => {
it('should call fetch function', () => {
search();
expect(fetchedStub).to.have.been.calledOnce;
});

it('should receive the correct url to fetch', () => {
context('passing one type', () => {
search('Muse', 'artist');
expect(fetchedStub).to.have.been.calledWith('https://api.spotify.com/v1/search?q=Muse&type=artist');
search('Muse', 'album');
expect(fetchedStub).to.have.been.calledWith('https://api.spotify.com/v1/search?q=Muse&type=album');
});

context('passing more than one type', () => {
search('Muse', ['artist', 'album']);
expect(fetchedStub).to.have.been.calledWith('https://api.spotify.com/v1/search?q=Muse&type=artist,album');
});
});

it('should returns the JSON data from the Promise', () => {
promise.resolves({ body: 'json' });
const artists = search('Muse', 'artist');

expect(artists.resolveValue).to.be.eql({ body: 'json' });
expect(spotify.search.playlists).to.exist;
});
});

describe('searchArtists', () => {
it('should call fetch function', () => {
searchArtists('Muse');
spotify.search.artists('Muse');
expect(fetchedStub).to.have.been.calledOnce;
});

it('should call fetch with the correct url', () => {
searchArtists('Muse');
spotify.search.artists('Muse');
expect(fetchedStub).to.have.been.calledWith('https://api.spotify.com/v1/search?q=Muse&type=artist');

searchArtists('Incubus');
spotify.search.artists('Incubus');
expect(fetchedStub).to.have.been.calledWith('https://api.spotify.com/v1/search?q=Incubus&type=artist');
});
});

describe('searchAlbums', () => {
it('should call fetch function', () => {
searchAlbums('Muse');
spotify.search.albums('Muse');
expect(fetchedStub).to.have.been.calledOnce;
});

it('should call fetch with the correct url', () => {
searchAlbums('Muse');
spotify.search.albums('Muse');
expect(fetchedStub).to.have.been.calledWith('https://api.spotify.com/v1/search?q=Muse&type=album');

searchAlbums('Incubus');
spotify.search.albums('Incubus');
expect(fetchedStub).to.have.been.calledWith('https://api.spotify.com/v1/search?q=Incubus&type=album');
});
});

describe('searchTracks', () => {
it('should call fetch function', () => {
searchTracks('Muse');
spotify.search.tracks('Muse');
expect(fetchedStub).to.have.been.calledOnce;
});

it('should call fetch with the correct url', () => {
searchTracks('Muse');
spotify.search.tracks('Muse');
expect(fetchedStub).to.have.been.calledWith('https://api.spotify.com/v1/search?q=Muse&type=track');

searchTracks('Incubus');
spotify.search.tracks('Incubus');
expect(fetchedStub).to.have.been.calledWith('https://api.spotify.com/v1/search?q=Incubus&type=track');
});
});

describe('searchPlaylists', () => {
it('should call fetch function', () => {
searchPlaylists('Muse');
spotify.search.playlists('Muse');
expect(fetchedStub).to.have.been.calledOnce;
});

it('should call fetch with the correct url', () => {
searchPlaylists('Muse');
spotify.search.playlists('Muse');
expect(fetchedStub).to.have.been.calledWith('https://api.spotify.com/v1/search?q=Muse&type=playlist');

searchPlaylists('Incubus');
spotify.search.playlists('Incubus');
expect(fetchedStub).to.have.been.calledWith('https://api.spotify.com/v1/search?q=Incubus&type=playlist');
});
});
Expand Down

0 comments on commit 6e3252e

Please sign in to comment.