1
0
mirror of https://github.com/oceanprotocol/commons.git synced 2023-03-15 18:03:00 +01:00

update tests

This commit is contained in:
Matthias Kretschmann 2019-06-11 22:15:14 +02:00
parent 1b336e2e80
commit 264a066874
Signed by: m
GPG Key ID: 606EEEF3C479A91F
4 changed files with 128 additions and 21 deletions

View File

@ -1,29 +1,124 @@
import React from 'react' import React from 'react'
import { render, waitForElement } from '@testing-library/react' import { render, waitForElement } from '@testing-library/react'
import mockAxios from 'jest-mock-axios' import mockAxios from 'jest-mock-axios'
import VersionNumbers from '.' import { StateMock } from '@react-mock/state'
import { version as versionSquid } from '@oceanprotocol/squid/package.json'
import VersionNumbers, { commonsVersion } from '.'
afterEach(() => { afterEach(() => {
mockAxios.reset() mockAxios.reset()
}) })
const stateMock = {
commons: { software: 'Commons', version: commonsVersion },
squidJs: {
software: 'Squid-js',
version: versionSquid
},
aquarius: {
isLoading: false,
software: 'Aquarius',
version: ''
},
brizo: {
isLoading: false,
software: 'Brizo',
version: '',
contracts: {},
network: '',
'keeper-version': '0.0.0',
'keeper-url': ''
},
keeperContracts: {
isLoading: false,
software: 'Keeper Contracts',
version: '',
contracts: {},
network: ''
},
faucet: {
isLoading: false,
software: 'Faucet',
version: ''
}
}
const stateMockIncomplete = {
commons: { software: 'Commons', version: commonsVersion },
squidJs: {
software: 'Squid-js',
version: versionSquid
},
aquarius: {
isLoading: false,
software: 'Aquarius',
version: undefined
},
brizo: {
isLoading: false,
software: 'Brizo',
version: undefined,
contracts: undefined,
network: undefined,
'keeper-version': undefined,
'keeper-url': undefined
},
keeperContracts: {
isLoading: false,
software: 'Keeper Contracts',
version: undefined,
contracts: undefined,
network: undefined
},
faucet: {
isLoading: false,
software: 'Faucet',
version: undefined
}
}
const mockResponse = { const mockResponse = {
data: { data: {
software: 'Brizo', software: 'Brizo',
version: '6.6.6', version: '6.6.6',
contracts: { Hello: 'Hello', Another: 'Hello' }, contracts: { Hello: 'Hello', Another: 'Hello' },
network: 'hello' network: 'hello',
'keeper-url': 'https://squid.com',
'keeper-version': '6.6.6'
} }
} }
const mockResponseFaulty = {
status: 404,
statusText: 'Not Found',
data: {}
}
describe('VersionNumbers', () => { describe('VersionNumbers', () => {
it('renders without crashing', () => { it('renders without crashing', () => {
const { container } = render(<VersionNumbers />) const { container } = render(
<StateMock state={stateMock}>
<VersionNumbers />
</StateMock>
)
mockAxios.mockResponse(mockResponse) mockAxios.mockResponse(mockResponse)
expect(mockAxios.get).toHaveBeenCalled() expect(mockAxios.get).toHaveBeenCalled()
expect(container.firstChild).toBeInTheDocument() expect(container.firstChild).toBeInTheDocument()
}) })
it('renders without proper component response', () => {
const { container } = render(
<StateMock state={stateMockIncomplete}>
<VersionNumbers />
</StateMock>
)
mockAxios.mockResponse(mockResponseFaulty)
expect(mockAxios.get).toHaveBeenCalled()
expect(container.querySelector('table')).toHaveTextContent(
'Could not get version'
)
})
it('minimal component versions in link title, prefixed with `v`', async () => { it('minimal component versions in link title, prefixed with `v`', async () => {
const { getByTitle } = render(<VersionNumbers minimal />) const { getByTitle } = render(<VersionNumbers minimal />)
mockAxios.mockResponse(mockResponse) mockAxios.mockResponse(mockResponse)

View File

@ -18,8 +18,9 @@ import {
} from '../../../config' } from '../../../config'
import VersionTable from './VersionTable' import VersionTable from './VersionTable'
import { isJsonString } from './utils'
const commonsVersion = export const commonsVersion =
process.env.NODE_ENV === 'production' ? version : `${version}-dev` process.env.NODE_ENV === 'production' ? version : `${version}-dev`
interface VersionNumbersProps { interface VersionNumbersProps {
@ -120,7 +121,8 @@ export default class VersionNumbers extends PureComponent<
aquariusHost, aquariusHost,
aquariusPort aquariusPort
) )
aquarius.version !== undefined && aquarius &&
aquarius.version !== undefined &&
this.setState({ aquarius: { isLoading: false, ...aquarius } }) this.setState({ aquarius: { isLoading: false, ...aquarius } })
} }
@ -133,7 +135,8 @@ export default class VersionNumbers extends PureComponent<
brizo['keeper-url'] && brizo['keeper-url'] &&
new URL(brizo['keeper-url']).hostname.split('.')[0] new URL(brizo['keeper-url']).hostname.split('.')[0]
brizo.version !== undefined && brizo &&
brizo.version !== undefined &&
this.setState({ this.setState({
brizo: { brizo: {
isLoading: false, isLoading: false,
@ -153,22 +156,14 @@ export default class VersionNumbers extends PureComponent<
const faucet = await this.getData(faucetScheme, faucetHost, faucetPort) const faucet = await this.getData(faucetScheme, faucetHost, faucetPort)
// backwards compatibility // backwards compatibility
function IsJsonString(str: string) { isJsonString(faucet) === false &&
try {
JSON.parse(str)
} catch (e) {
return false
}
return true
}
IsJsonString(faucet) === false &&
this.setState({ this.setState({
faucet: { ...this.state.faucet, isLoading: false } faucet: { ...this.state.faucet, isLoading: false }
}) })
// the new thing // the new thing
faucet.version !== undefined && faucet &&
faucet.version !== undefined &&
this.setState({ faucet: { isLoading: false, ...faucet } }) this.setState({ faucet: { isLoading: false, ...faucet } })
} }
@ -179,10 +174,8 @@ export default class VersionNumbers extends PureComponent<
cancelToken: this.signal.token cancelToken: this.signal.token
}) })
if (response.status !== 200) { // fail silently
Logger.error(response.statusText) if (response.status !== 200) return
return
}
return response.data return response.data
} catch (error) { } catch (error) {

View File

@ -0,0 +1,11 @@
import { isJsonString } from './utils'
describe('isJsonString', () => {
it('detects json correctly', () => {
const testJson = isJsonString('{ "hello": "squid" }')
expect(testJson).toBeTruthy()
const testNonJson = isJsonString('<strong>')
expect(testNonJson).toBeFalsy()
})
})

View File

@ -0,0 +1,8 @@
export function isJsonString(str: string) {
try {
JSON.parse(str)
} catch (e) {
return false
}
return true
}