1
0
mirror of https://github.com/oceanprotocol/status synced 2024-11-22 01:36:59 +01:00

test coverage

This commit is contained in:
Matthias Kretschmann 2019-09-17 02:19:06 +02:00
parent d7a5526b84
commit 19dee496e5
Signed by: m
GPG Key ID: 606EEEF3C479A91F
4 changed files with 55 additions and 29 deletions

View File

@ -52,7 +52,7 @@ export default function Network({ network }) {
return () => {
clearInterval(timer)
}
}, [network.url])
}, [network])
const isOnline = status === 'Online'

View File

@ -1,5 +1,5 @@
import React from 'react'
import { render, waitForElement } from '@testing-library/react'
import { render, wait } from '@testing-library/react'
import mockAxios from 'axios'
import Network from './Network'
@ -11,6 +11,7 @@ const mockResponse = {
afterEach(() => {
mockAxios.reset()
jest.clearAllTimers()
})
describe('Network', () => {
@ -24,11 +25,26 @@ describe('Network', () => {
}
it('renders without crashing', async () => {
mockAxios.post.mockResolvedValueOnce(mockResponse)
const { container, getByTitle } = render(<Network network={network} />)
mockAxios.post.mockResolvedValue(mockResponse)
const { container } = render(<Network network={network} />)
expect(container.firstChild).toBeInTheDocument()
await waitForElement(() => getByTitle('Current block number'))
await wait()
expect(mockAxios.post).toHaveBeenCalledTimes(2)
})
it('renders without response', async () => {
mockAxios.post.mockResolvedValue(undefined)
const { container } = render(<Network network={network} />)
await wait()
expect(container.firstChild).toBeInTheDocument()
})
it('re-fetches after 5 sec.', async () => {
jest.useFakeTimers()
mockAxios.post.mockResolvedValue(mockResponse)
render(<Network network={network} />)
jest.advanceTimersByTime(6000)
await wait()
expect(setInterval).toHaveBeenCalledTimes(1)
})
})

View File

@ -1,12 +1,22 @@
import { act } from '@testing-library/react'
import ReactDOM from 'react-dom'
import { renderToDOM } from '.'
describe('index', () => {
it('should be able to run tests', () => {
expect(1 + 2).toEqual(3)
describe('test ReactDOM.render', () => {
const originalRender = ReactDOM.render
const originalGetElement = global.document.getElementById
beforeEach(() => {
global.document.getElementById = () => true
ReactDOM.render = jest.fn()
})
it('renders without crashing', () => {
act(() => renderToDOM())
afterAll(() => {
global.document.getElementById = originalGetElement
ReactDOM.render = originalRender
})
it('should call ReactDOM.render', () => {
renderToDOM()
expect(ReactDOM.render).toHaveBeenCalled()
})
})

View File

@ -1,5 +1,22 @@
import axios from 'axios'
async function axiosRpcRequest(url, method) {
try {
const response = await axios.post(url, {
method,
params: [],
id: 1,
jsonrpc: '2.0'
})
return response
} catch (error) {
return error
}
}
export { axiosRpcRequest }
// Measure response time and deliver as `response.duration`
axios.interceptors.request.use(
config => {
@ -23,20 +40,3 @@ axios.interceptors.response.use(
return Promise.reject(error)
}
)
async function axiosRpcRequest(url, method) {
try {
const response = await axios.post(url, {
method,
params: [],
id: 1,
jsonrpc: '2.0'
})
return response
} catch (error) {
return error
}
}
export { axiosRpcRequest }