mirror of
https://github.com/oceanprotocol/status
synced 2025-02-14 21:10:36 +01:00
test coverage
This commit is contained in:
parent
d7a5526b84
commit
19dee496e5
@ -52,7 +52,7 @@ export default function Network({ network }) {
|
|||||||
return () => {
|
return () => {
|
||||||
clearInterval(timer)
|
clearInterval(timer)
|
||||||
}
|
}
|
||||||
}, [network.url])
|
}, [network])
|
||||||
|
|
||||||
const isOnline = status === 'Online'
|
const isOnline = status === 'Online'
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { render, waitForElement } from '@testing-library/react'
|
import { render, wait } from '@testing-library/react'
|
||||||
import mockAxios from 'axios'
|
import mockAxios from 'axios'
|
||||||
import Network from './Network'
|
import Network from './Network'
|
||||||
|
|
||||||
@ -11,6 +11,7 @@ const mockResponse = {
|
|||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
mockAxios.reset()
|
mockAxios.reset()
|
||||||
|
jest.clearAllTimers()
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Network', () => {
|
describe('Network', () => {
|
||||||
@ -24,11 +25,26 @@ describe('Network', () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
it('renders without crashing', async () => {
|
it('renders without crashing', async () => {
|
||||||
mockAxios.post.mockResolvedValueOnce(mockResponse)
|
mockAxios.post.mockResolvedValue(mockResponse)
|
||||||
|
const { container } = render(<Network network={network} />)
|
||||||
const { container, getByTitle } = render(<Network network={network} />)
|
|
||||||
expect(container.firstChild).toBeInTheDocument()
|
expect(container.firstChild).toBeInTheDocument()
|
||||||
await waitForElement(() => getByTitle('Current block number'))
|
await wait()
|
||||||
expect(mockAxios.post).toHaveBeenCalledTimes(2)
|
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)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,12 +1,22 @@
|
|||||||
import { act } from '@testing-library/react'
|
import ReactDOM from 'react-dom'
|
||||||
import { renderToDOM } from '.'
|
import { renderToDOM } from '.'
|
||||||
|
|
||||||
describe('index', () => {
|
describe('test ReactDOM.render', () => {
|
||||||
it('should be able to run tests', () => {
|
const originalRender = ReactDOM.render
|
||||||
expect(1 + 2).toEqual(3)
|
const originalGetElement = global.document.getElementById
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
global.document.getElementById = () => true
|
||||||
|
ReactDOM.render = jest.fn()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('renders without crashing', () => {
|
afterAll(() => {
|
||||||
act(() => renderToDOM())
|
global.document.getElementById = originalGetElement
|
||||||
|
ReactDOM.render = originalRender
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should call ReactDOM.render', () => {
|
||||||
|
renderToDOM()
|
||||||
|
expect(ReactDOM.render).toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
34
src/rpc.js
34
src/rpc.js
@ -1,5 +1,22 @@
|
|||||||
import axios from 'axios'
|
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`
|
// Measure response time and deliver as `response.duration`
|
||||||
axios.interceptors.request.use(
|
axios.interceptors.request.use(
|
||||||
config => {
|
config => {
|
||||||
@ -23,20 +40,3 @@ axios.interceptors.response.use(
|
|||||||
return Promise.reject(error)
|
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 }
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user