Skip to content

Commit

Permalink
Add more unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
wenbinf committed May 2, 2021
1 parent 841648b commit 6ce1e35
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ Simple and no-nonsense podcast search & directory API. Search the meta data of a

**Note**: We don't recommend using Listen API in client-side JavaScript in the browser, because it'll leak your API key in the code.

If you have any questions, please contact [[email protected]]([email protected]?subject=Questions+about+the+JS+SDK+of+Listen+API)

<a href="https://www.listennotes.com/api/"><img src="https://raw.githubusercontent.com/ListenNotes/ListenApiDemo/master/web/src/powered_by_listennotes.png" width="300" /></a>


Table of Contents
**Table of Contents**
- [Podcast API JavaScript Library](#podcast-api-javascript-library)
- [Installation](#installation)
- [Requirements](#requirements)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "podcast-api",
"version": "1.0.6",
"version": "1.0.7",
"description": "JavaScript bindings for the Listen Notes Podcast API",
"main": "src/PodcastApiClient.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/PodcastApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const Client = (config = {}) => {

this.deletePodcast = (params) => {
const { id, reason } = params;
return this.httpClient.delete(`/podcasts/${id}?reason=${reason}`);
return this.httpClient.delete(`/podcasts/${id}?reason=${reason || ''}`);
};

return this;
Expand Down
266 changes: 264 additions & 2 deletions tests/PodcastApiTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ const { Client } = require('podcast-api');

test('Test search endpoint with mock', () => {
const client = Client();
const term = 'elon musk';
return client.search({
q: 'elon musk',
q: term,
}).then((response) => {
expect(response.data.count).toBe(10);
expect(response.config.params.q).toBe(term);
expect(response.config.url).toBe('/search');
expect(response.config.method).toBe('get');
expect(response.data.count > 0).toBe(true);
}).catch(() => {
fail('Failed!');
});
});

Expand All @@ -32,3 +38,259 @@ test('Test search endpoint with authentication error', () => {
}
});
});

test('Test typeahead with mock', () => {
const client = Client();
const term = 'elon musk2';
return client.typeahead({
q: term,
show_podcasts: 1,
}).then((response) => {
expect(response.config.params.q).toBe(term);
expect(response.config.params.show_podcasts).toBe(1);
expect(response.config.url).toBe('/typeahead');
expect(response.config.method).toBe('get');
expect(response.data.terms.length > 0).toBe(true);
}).catch(() => {
fail('Failed!');
});
});

test('Test fetchBestPodcasts with mock', () => {
const client = Client();
const genreId = 123;
return client.fetchBestPodcasts({
genre_id: genreId,
}).then((response) => {
expect(response.config.params.genre_id).toBe(genreId);
expect(response.config.url).toBe('/best_podcasts');
expect(response.config.method).toBe('get');
expect(response.data.total > 0).toBe(true);
}).catch(() => {
fail('Failed!');
});
});

test('Test fetchPodcastById with mock', () => {
const client = Client();
const podcastId = 'abcde';
return client.fetchPodcastById({
id: podcastId,
}).then((response) => {
expect(response.config.url).toBe(`/podcasts/${podcastId}`);
expect(response.config.method).toBe('get');
expect(response.data.episodes.length > 0).toBe(true);
}).catch(() => {
fail('Failed!');
});
});

test('Test fetchEpisodeById with mock', () => {
const client = Client();
const episodeId = 'abc222de';
return client.fetchEpisodeById({
id: episodeId,
}).then((response) => {
expect(response.config.url).toBe(`/episodes/${episodeId}`);
expect(response.config.method).toBe('get');
expect(response.data.podcast.rss.length > 0).toBe(true);
}).catch(() => {
fail('Failed!');
});
});

test('Test batchFetchPodcasts with mock', () => {
const client = Client();
const ids = '996,777,888,1000';
return client.batchFetchPodcasts({
ids,
}).then((response) => {
expect(response.config.url).toBe('/podcasts');
expect(response.config.method).toBe('post');
expect(JSON.parse(response.config.data).ids).toBe(ids);
expect(response.data.podcasts.length > 0).toBe(true);
}).catch(() => {
fail('Failed!');
});
});

test('Test batchFetchEpisodes with mock', () => {
const client = Client();
const ids = '996,777,222,888,1000';
return client.batchFetchEpisodes({
ids,
}).then((response) => {
expect(response.config.url).toBe('/episodes');
expect(response.config.method).toBe('post');
expect(JSON.parse(response.config.data).ids).toBe(ids);
expect(response.data.episodes.length > 0).toBe(true);
}).catch(() => {
fail('Failed!');
});
});

test('Test fetchCuratedPodcastsListById with mock', () => {
const client = Client();
const curatedListId = '23232';
return client.fetchCuratedPodcastsListById({
id: curatedListId,
}).then((response) => {
expect(response.config.url).toBe(`/curated_podcasts/${curatedListId}`);
expect(response.config.method).toBe('get');
expect(response.data.podcasts.length > 0).toBe(true);
}).catch(() => {
fail('Failed!');
});
});

test('Test fetchCuratedPodcastsLists with mock', () => {
const client = Client();
const page = 2;
return client.fetchCuratedPodcastsLists({
page: 2,
}).then((response) => {
expect(response.config.url).toBe('/curated_podcasts');
expect(response.config.params.page).toBe(page);
expect(response.config.method).toBe('get');
expect(response.data.total > 0).toBe(true);
}).catch(() => {
fail('Failed!');
});
});

test('Test fetchPodcastGenres with mock', () => {
const client = Client();
const topLevelOnly = 1;
return client.fetchPodcastGenres({
top_level_only: topLevelOnly,
}).then((response) => {
expect(response.config.url).toBe('/genres');
expect(response.config.params.top_level_only).toBe(topLevelOnly);
expect(response.config.method).toBe('get');
expect(response.data.genres.length > 0).toBe(true);
}).catch(() => {
fail('Failed!');
});
});

test('Test fetchPodcastRegions with mock', () => {
const client = Client();
return client.fetchPodcastRegions({
}).then((response) => {
expect(response.config.url).toBe('/regions');
expect(response.config.method).toBe('get');
expect(response.data.regions).not.toBeNull();
}).catch(() => {
fail('Failed!');
});
});

test('Test fetchPodcastLanguages with mock', () => {
const client = Client();
return client.fetchPodcastLanguages({
}).then((response) => {
expect(response.config.url).toBe('/languages');
expect(response.config.method).toBe('get');
expect(response.data.languages.length > 0).toBe(true);
}).catch(() => {
fail('Failed!');
});
});

test('Test justListen with mock', () => {
const client = Client();
return client.justListen({
}).then((response) => {
expect(response.config.url).toBe('/just_listen');
expect(response.config.method).toBe('get');
expect(response.data.audio_length_sec > 0).toBe(true);
}).catch(() => {
fail('Failed!');
});
});

test('Test fetchRecommendationsForPodcast with mock', () => {
const client = Client();
const podcastId = 'abcde';
return client.fetchRecommendationsForPodcast({
id: podcastId,
}).then((response) => {
expect(response.config.url).toBe(`/podcasts/${podcastId}/recommendations`);
expect(response.config.method).toBe('get');
expect(response.data.recommendations.length > 0).toBe(true);
}).catch(() => {
fail('Failed!');
});
});

test('Test fetchRecommendationsForEpisode with mock', () => {
const client = Client();
const episodeId = 'abc222de';
return client.fetchRecommendationsForEpisode({
id: episodeId,
}).then((response) => {
expect(response.config.url).toBe(`/episodes/${episodeId}/recommendations`);
expect(response.config.method).toBe('get');
expect(response.data.recommendations.length > 0).toBe(true);
}).catch(() => {
fail('Failed!');
});
});

test('Test fetchPlaylistById with mock', () => {
const client = Client();
const playlistId = 'abddc222de';
return client.fetchPlaylistById({
id: playlistId,
}).then((response) => {
expect(response.config.url).toBe(`/playlists/${playlistId}`);
expect(response.config.method).toBe('get');
expect(response.data.items.length > 0).toBe(true);
}).catch(() => {
fail('Failed!');
});
});

test('Test fetchMyPlaylists with mock', () => {
const client = Client();
return client.fetchMyPlaylists({
}).then((response) => {
expect(response.config.url).toBe('/playlists');
expect(response.config.method).toBe('get');
expect(response.data.total > 0).toBe(true);
}).catch(() => {
fail('Failed!');
});
});

test('Test submitPodcast with mock', () => {
const client = Client();
const rss = 'http://myrss.com/rss';
return client.submitPodcast({
rss,
}).then((response) => {
expect(response.config.url).toBe('/podcasts/submit');
expect(response.config.method).toBe('post');
expect(JSON.parse(response.config.data).rss).toBe(rss);
expect(response.data.status.length > 0).toBe(true);
}).catch(() => {
fail('Failed!');
});
});

test('Test deletePodcast with mock', () => {
const client = Client();
const podcastId = 'asdfasdf';
const reason = 'abv';
return client.deletePodcast({
id: podcastId,
reason,
}).then((response) => {
expect(response.config.url).toBe(`/podcasts/${podcastId}?reason=${reason}`);
expect(response.config.method).toBe('delete');
expect(response.data.status.length > 0).toBe(true);
}).catch((error) => {
console.log(error);
fail('Failed!');
});
});

0 comments on commit 6ce1e35

Please sign in to comment.