Skip to content

Commit

Permalink
refactor(test): move "fetch" to global object just for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ivandotv committed Feb 8, 2021
1 parent 9cac01d commit 4776917
Showing 1 changed file with 38 additions and 39 deletions.
77 changes: 38 additions & 39 deletions tests/radioBrowser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import { StationSearchType } from '../src/constants'
import { RadioBrowserApi } from '../src/radioBrowser'
import { getMockStation, getMockResponse } from './utils/mockStation'

const globalTest = {
fetch: (nodeFetch as unknown) as typeof fetch
}
global.fetch = (nodeFetch as unknown) as typeof fetch

const baseUrl = 'https://fr1.api.radio-browser.info'

describe('Radio Browser', () => {
Expand All @@ -30,7 +29,7 @@ describe('Radio Browser', () => {
.get('/json/servers')
.reply(200, mockResult)

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

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

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

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

const appName = 'test'
const api = new RadioBrowserApi(globalTest.fetch, appName)
const api = new RadioBrowserApi(fetch, appName)
expect.assertions(1)
try {
await api.resolveBaseUrl()
Expand All @@ -94,8 +93,8 @@ describe('Radio Browser', () => {
.reply(200, [{ name: '', ip: '' }])

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

await api.resolveBaseUrl()

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

0 comments on commit 4776917

Please sign in to comment.