Skip to content

Commit

Permalink
refactor: expect fetch to be available
Browse files Browse the repository at this point in the history
fetch implementation should be available in the environment

BREAKING CHANGE: fetch is no longer passed in via constructor. It should be available in the
environment
  • Loading branch information
ivandotv committed Jun 1, 2021
1 parent 66f5205 commit f1e628d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 41 deletions.
9 changes: 2 additions & 7 deletions src/radioBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,10 @@ export class RadioBrowserApi {

/**
* Creates an instance of radio browser api.
* @param fetchImpl - Fetch API
* @param appName - App name to be used as user agent header to indentify the calls to the API
* @param hideBroken - Hide broken stations for all future API calls
*/
constructor(
protected fetchImpl: typeof fetch,
protected appName: string,
protected hideBroken = true
) {
constructor(protected appName: string, protected hideBroken = true) {
if (!appName) {
throw new Error('appName is requred')
}
Expand Down Expand Up @@ -505,7 +500,7 @@ export class RadioBrowserApi {
const finalConfig = Object.assign({}, this.fetchConfig, fetchConfig)
finalConfig.headers = headers

const response = await this.fetchImpl(url, finalConfig)
const response = await fetch(url, finalConfig)

if (response.ok) {
return response.json()
Expand Down
68 changes: 34 additions & 34 deletions tests/radioBrowser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Radio Browser', () => {
.get('/json/servers')
.reply(200, mockResult)

const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)
const result = await api.resolveBaseUrl()

expect(result).toEqual(mockResult)
Expand All @@ -38,7 +38,7 @@ describe('Radio Browser', () => {

test('Resolve base url but do not set it', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)
const defaultBaseUrl = api.getBaseUrl()
const mockResult = [
{
Expand All @@ -64,7 +64,7 @@ describe('Radio Browser', () => {
test('Manually set base url', () => {
const url = '1.2.3.4'
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)
api.setBaseUrl(url)

expect(api.getBaseUrl()).toBe(url)
Expand All @@ -77,7 +77,7 @@ describe('Radio Browser', () => {
.reply(500, errorText)

const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)
expect.assertions(1)
try {
await api.resolveBaseUrl()
Expand All @@ -94,7 +94,7 @@ describe('Radio Browser', () => {

const agent = 'test'
const spy = jest.spyOn(global, 'fetch')
const api = new RadioBrowserApi(fetch, agent)
const api = new RadioBrowserApi(agent)

await api.resolveBaseUrl()

Expand All @@ -112,7 +112,7 @@ describe('Radio Browser', () => {
.reply(200, [{ name: '', ip: '' }])

const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)
const spy = jest.spyOn(global, 'fetch')

const headerName = 'x-jest-test'
Expand All @@ -134,7 +134,7 @@ describe('Radio Browser', () => {

test('get countries', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -164,7 +164,7 @@ describe('Radio Browser', () => {

test('get country by country code', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -197,7 +197,7 @@ describe('Radio Browser', () => {

test('get codecs', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -229,7 +229,7 @@ describe('Radio Browser', () => {

test('get country states', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -262,7 +262,7 @@ describe('Radio Browser', () => {

test('get languages', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -295,7 +295,7 @@ describe('Radio Browser', () => {

test('get tags', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -328,7 +328,7 @@ describe('Radio Browser', () => {
describe('Get stations by', () => {
test('by language', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -366,7 +366,7 @@ describe('Radio Browser', () => {

test('by tag', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -405,7 +405,7 @@ describe('Radio Browser', () => {
expect.assertions(1)

const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)
try {
await api.getStationsBy(
// @ts-ignore
Expand All @@ -420,7 +420,7 @@ describe('Radio Browser', () => {

test('get all stations', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -452,7 +452,7 @@ describe('Radio Browser', () => {

test('send station click', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand All @@ -478,7 +478,7 @@ describe('Radio Browser', () => {

test('vote for station', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -506,7 +506,7 @@ describe('Radio Browser', () => {

test('get stations by votes', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -540,7 +540,7 @@ describe('Radio Browser', () => {
// advanced station search
test('by tag list', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -581,7 +581,7 @@ describe('Radio Browser', () => {
// advanced station search
test('hide broken stations by default', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -621,7 +621,7 @@ describe('Radio Browser', () => {

test('hide broken stations explicitly', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -658,7 +658,7 @@ describe('Radio Browser', () => {
})
test('Show broken stations', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -695,7 +695,7 @@ describe('Radio Browser', () => {
})
test('Remove stations with the same ids', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)
const headerName = 'x-jest-test'
const headerValue = '1'
const mockResult = [getMockResponse(), getMockResponse()]
Expand Down Expand Up @@ -736,7 +736,7 @@ describe('Radio Browser', () => {

test('Remove duplicated tags', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)
const tag = 'jazz'
const mockResponse = getMockResponse()
const duplicatedTags = mockResponse.tags.split(',')
Expand All @@ -755,7 +755,7 @@ describe('Radio Browser', () => {

test('Remove tags over 10 characters long', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)
const tag = 'jazz'
const mockResponse = getMockResponse()
mockResponse.tags = mockResponse.tags.concat(',tag with over 10 characters')
Expand All @@ -773,7 +773,7 @@ describe('Radio Browser', () => {
describe('Get top stations by clicks', () => {
test('get all', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand All @@ -799,7 +799,7 @@ describe('Radio Browser', () => {
})
test('get top 5', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const count = 5
const headerName = 'x-jest-test'
Expand Down Expand Up @@ -829,7 +829,7 @@ describe('Radio Browser', () => {
describe('Get stations by votes', () => {
test('get all', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand All @@ -855,7 +855,7 @@ describe('Radio Browser', () => {
})
test('get top 5', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const count = 5
const headerName = 'x-jest-test'
Expand Down Expand Up @@ -885,7 +885,7 @@ describe('Radio Browser', () => {
describe('Get stations by recent clicks', () => {
test('get all', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand All @@ -911,7 +911,7 @@ describe('Radio Browser', () => {
})
test('get top 5', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const count = 5
const headerName = 'x-jest-test'
Expand Down Expand Up @@ -940,7 +940,7 @@ describe('Radio Browser', () => {

test('get stations by Id', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)
const stationIds = ['1', '2', '3']

const headerName = 'x-jest-test'
Expand All @@ -967,7 +967,7 @@ describe('Radio Browser', () => {
})
test('get stations by url', async () => {
const appName = 'test'
const api = new RadioBrowserApi(fetch, appName)
const api = new RadioBrowserApi(appName)

const url = 'stationUrl'
const headerName = 'x-jest-test'
Expand Down

0 comments on commit f1e628d

Please sign in to comment.