Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
refactor: use nock to mock tests (#1291)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbarnsley authored and faustbrian committed Jun 17, 2019
1 parent d2c786d commit e165d03
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 151 deletions.
15 changes: 6 additions & 9 deletions __tests__/unit/services/crypto-compare.spec.js
@@ -1,5 +1,4 @@
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
import nock from 'nock'
import { MARKET } from '@config'
import { keys } from 'lodash'
import cryptoCompare from '@/services/crypto-compare'
Expand All @@ -10,14 +9,12 @@ describe('CryptoCompare', () => {
const baseUrl = MARKET.source.baseUrl

beforeEach(() => {
const historicalRegex = new RegExp(`^${baseUrl}/data/histo.*`)
const mock = new MockAdapter(axios)

mock
.onGet(`${baseUrl}/data/pricemultifull`)
nock(baseUrl)
.get('/data/pricemultifull')
.query(true)
.reply(200, require('../__fixtures__/services/crypto-compare-market.json'))
mock
.onGet(historicalRegex)
nock(baseUrl)
.get(/\/data\/histo.+/)
.reply(200, require('../__fixtures__/services/crypto-compare-historical.json'))
})

Expand Down
22 changes: 14 additions & 8 deletions __tests__/unit/services/release.spec.js
@@ -1,16 +1,20 @@
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
import nock from 'nock'
import errorCapturer from '../__utils__/error-capturer'

import release from '@/services/release'

const releaseApiUrl = 'https://api.github.com/repos/ArkEcosystem/desktop-wallet/releases/latest'
const githubApiBase = 'https://api.github.com'
const releaseApiUrl = '/repos/ArkEcosystem/desktop-wallet/releases/latest'
const releaseUrl = 'https://github.com/ArkEcosystem/desktop-wallet/releases/latest'

beforeEach(() => {
nock.cleanAll()
})

describe('Services > Release', () => {
describe('latestReleaseApiUrl', () => {
it('returns the URL of the latest release API endpoint', () => {
expect(release.latestReleaseApiUrl).toEqual(releaseApiUrl)
expect(release.latestReleaseApiUrl).toEqual(`${githubApiBase}${releaseApiUrl}`)
})
})

Expand All @@ -24,16 +28,18 @@ describe('Services > Release', () => {
it('should fetch the latest published release', async () => {
const data = { tag_name: '2.0.0' }

const mock = new MockAdapter(axios)
mock.onGet(releaseApiUrl).reply(200, data)
nock(githubApiBase)
.get(releaseApiUrl)
.reply(200, data)

expect(await release.fetchLatestRelease()).toEqual(data)
})

describe('when the request or parsing fails', () => {
it('should throw the Error', async () => {
const mock = new MockAdapter(axios)
mock.onGet(releaseApiUrl).reply(500)
nock(githubApiBase)
.get(releaseApiUrl)
.reply(500)

expect(await errorCapturer(release.fetchLatestRelease())).toThrow(/500/)
})
Expand Down
15 changes: 7 additions & 8 deletions __tests__/unit/store/modules/delegate.spec.js
@@ -1,5 +1,4 @@
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
import nock from 'nock'
import Vue from 'vue'
import Vuex from 'vuex'
import apiClient, { client as clientService } from '@/plugins/api-client'
Expand All @@ -11,8 +10,6 @@ import { profile1 } from '../../__fixtures__/store/profile'
Vue.use(Vuex)
Vue.use(apiClient)

const axiosMock = new MockAdapter(axios)

beforeAll(() => {
clientService.version = 1
clientService.host = 'http://127.0.0.1'
Expand Down Expand Up @@ -86,8 +83,9 @@ describe('delegate store module', () => {
}
})

axiosMock
.onGet(`http://127.0.0.1/api/delegates`, { params: { offset: 0, limit: 51, orderBy: 'rank:asc' } })
nock('http://127.0.0.1')
.get('/api/delegates')
.query({ offset: 0, limit: 51, orderBy: 'rank:asc' })
.reply(200, {
totalCount: 2,
delegates: v1DelegateData
Expand All @@ -98,8 +96,9 @@ describe('delegate store module', () => {

clientService.version = 2

axiosMock
.onGet(`http://127.0.0.1/api/delegates`, { params: { page: 1, limit: 100, orderBy: 'rank:asc' } })
nock('http://127.0.0.1')
.get('/api/delegates')
.query({ page: 1, limit: 100, orderBy: 'rank:asc' })
.reply(200, {
data: v2DelegateData,
meta: {
Expand Down
31 changes: 17 additions & 14 deletions __tests__/unit/store/modules/ledger.spec.js
@@ -1,5 +1,4 @@
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
import nock from 'nock'
import Vue from 'vue'
import Vuex from 'vuex'
import { crypto } from '@arkecosystem/crypto'
Expand All @@ -12,7 +11,6 @@ import logger from 'electron-log'
Vue.use(Vuex)
Vue.use(apiClient)

const axiosMock = new MockAdapter(axios)
logger.error = jest.fn()

ClientService.host = 'http://127.0.0.1'
Expand Down Expand Up @@ -77,7 +75,7 @@ beforeEach(async () => {
store.replaceState(JSON.parse(JSON.stringify(initialState)))
ClientService.capabilities = '2.0.0'
ledgerNameByAddress = () => null
axiosMock.reset()
nock.cleanAll()
})
describe('ledger store module', () => {
it('should init ledger service', () => {
Expand Down Expand Up @@ -224,8 +222,9 @@ describe('ledger store module', () => {
})

it('should load 10 wallets', async () => {
axiosMock
.onGet(new RegExp(`http://127.0.0.1/api/wallets/*`))
nock('http://127.0.0.1')
.persist()
.get(/\/api\/wallets\/.+/)
.reply(404, {
statusCode: 404,
error: 'Not Found',
Expand All @@ -247,15 +246,17 @@ describe('ledger store module', () => {
continue
}

axiosMock
.onGet(`http://127.0.0.1/api/wallets/${wallet.address}`)
nock('http://127.0.0.1')
.persist()
.get(`/api/wallets/${wallet.address.replace(' ', '%20')}`)
.reply(200, {
data: wallet
})
}

axiosMock
.onGet(`http://127.0.0.1/api/wallets/address 10`)
nock('http://127.0.0.1')
.persist()
.get('/api/wallets/address%2010')
.reply(404, {
statusCode: 404,
error: 'Not Found',
Expand All @@ -269,8 +270,9 @@ describe('ledger store module', () => {
it('should load all wallets with multi-wallet search', async () => {
ClientService.capabilities = '2.1.0'

axiosMock
.onPost(`http://127.0.0.1/api/wallets/search`)
nock('http://127.0.0.1')
.persist()
.post('/api/wallets/search')
.reply(200, {
data: ledgerWallets.slice(0, 9)
})
Expand All @@ -283,8 +285,9 @@ describe('ledger store module', () => {
ClientService.capabilities = '2.1.0'
ledgerNameByAddress = (address) => address

axiosMock
.onPost(`http://127.0.0.1/api/wallets/search`)
nock('http://127.0.0.1')
.persist()
.post('/api/wallets/search')
.reply(200, {
data: ledgerWallets.slice(0, 9)
})
Expand Down

0 comments on commit e165d03

Please sign in to comment.