Skip to content

Commit

Permalink
added unit test for related()
Browse files Browse the repository at this point in the history
  • Loading branch information
zackradisic committed Aug 24, 2020
1 parent 84d8d9a commit eda0a89
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
25 changes: 24 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ var SCDL = /** @class */ (function () {
};
/**
* Searches for tracks/playlists for the given query
* @param type - The type of resource, one of: 'tracks', 'people', 'albums', 'sets', 'all'
* @param type - The type of resource, one of: 'tracks', 'people', 'albums', 'playlists', 'all'
* @param query - The keywords for the search
* @param clientID - A Soundcloud Client ID, will find one if not provided
* @returns SearchResponse
Expand All @@ -231,6 +231,29 @@ var SCDL = /** @class */ (function () {
});
});
};
/**
* Finds related tracks/playlists/albums to the given track/playlist/album specified by ID
* @param type - 'tracks', 'people', 'albums', 'playlists'
* @param id - The ID of the resource
* @param limit - The number of results to return
* @param offset - Used for pagination, set to 0 if you will not use this feature.
* @param clientID - A Soundcloud Client ID, will find one if not provided
*/
SCDL.prototype.related = function (type, id, limit, offset, clientID) {
if (offset === void 0) { offset = 0; }
return __awaiter(this, void 0, void 0, function () {
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_a = search_1.related;
_b = [type, id, limit, offset];
return [4 /*yield*/, this._assignClientID(clientID)];
case 1: return [2 /*return*/, _a.apply(void 0, _b.concat([_c.sent()]))];
}
});
});
};
/**
* Returns whether or not the given URL is a valid Soundcloud URL
* @param url - URL of the Soundcloud track
Expand Down
16 changes: 14 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import isValidURL from './is-url'

import STREAMING_PROTOCOLS, { _PROTOCOLS } from './protocols'
import FORMATS, { _FORMATS } from './formats'
import { search, SoundcloudResource } from './search'
import { search, related, SoundcloudResource } from './search'

/** @internal */
const download = async (url: string, clientID: string) => {
Expand Down Expand Up @@ -95,7 +95,7 @@ export class SCDL {

/**
* Searches for tracks/playlists for the given query
* @param type - The type of resource, one of: 'tracks', 'people', 'albums', 'sets', 'all'
* @param type - The type of resource, one of: 'tracks', 'people', 'albums', 'playlists', 'all'
* @param query - The keywords for the search
* @param clientID - A Soundcloud Client ID, will find one if not provided
* @returns SearchResponse
Expand All @@ -104,6 +104,18 @@ export class SCDL {
return search(type, query, await this._assignClientID(clientID))
}

/**
* Finds related tracks/playlists/albums to the given track/playlist/album specified by ID
* @param type - 'tracks', 'people', 'albums', 'playlists'
* @param id - The ID of the resource
* @param limit - The number of results to return
* @param offset - Used for pagination, set to 0 if you will not use this feature.
* @param clientID - A Soundcloud Client ID, will find one if not provided
*/
async related (type: SoundcloudResource, id: number, limit: number, offset = 0, clientID?: string) {
return related(type, id, limit, offset, await this._assignClientID(clientID))
}

/**
* Returns whether or not the given URL is a valid Soundcloud URL
* @param url - URL of the Soundcloud track
Expand Down
11 changes: 9 additions & 2 deletions src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ export type SearchResponse<T> = {
query_urn: string
}

export type RelatedResponse<T> = {
collection: T[]
next_href: string,
query_urn: string,
variant: string
}

export type SearchResponseAll = SearchResponse<User | SetInfo | TrackInfo>

export type SoundcloudResource = 'tracks' | 'people' | 'albums' | 'playlists'
Expand All @@ -23,7 +30,7 @@ export const search = async (type: SoundcloudResource | 'all', query: string, cl
}

/** @internal */
export const related = async <T extends TrackInfo | User | SetInfo> (type: SoundcloudResource, id: number, limit = 10, offset = 0, clientID: string): Promise<SearchResponse<T>> => {
export const related = async <T extends TrackInfo | User | SetInfo> (type: SoundcloudResource, id: number, limit = 10, offset = 0, clientID: string): Promise<RelatedResponse<T>> => {
const { data } = await axios.get(`https://api-v2.soundcloud.com/${type}/${id}/related?client_id=${clientID}&offset=${offset}&limit=${limit}`)
return data as SearchResponse<T>
return data as RelatedResponse<T>
}
34 changes: 34 additions & 0 deletions tests/related.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @jest-environment node
*/

import scdl, { search } from '../'

describe('related()', () => {
const limit = 10
let searchResponse

beforeAll(async () => {
try {
searchResponse = await scdl.related('tracks', 170286204, limit, 0)
} catch (err) {
console.log(err)
process.exit(1)
}
})

it('returns a valid SearchResponse object', () => {
const keys = ['collection', 'next_href', 'variant', 'query_urn'].forEach(key => expect(searchResponse[key]).toBeDefined())
})

it('resource count returned is equal to limit', () => {
expect(searchResponse.collection.length).toEqual(limit)
})

it('resource returned corresponds to type parameter', () => {
searchResponse.collection.forEach(track => {
expect(track.title).toBeDefined()
expect(track.media).toBeDefined()
})
})
})

0 comments on commit eda0a89

Please sign in to comment.