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-09-09 23:38:12 +02:00
parent be7020bceb
commit 3d1d81ffe9
Signed by: m
GPG Key ID: 606EEEF3C479A91F
6 changed files with 113 additions and 8 deletions

View File

@ -0,0 +1,15 @@
import React from 'react'
import { render } from '@testing-library/react'
import Spinner from './Spinner'
describe('Spinner', () => {
it('renders without crashing', () => {
const { container } = render(<Spinner />)
expect(container.firstChild).toBeInTheDocument()
})
it('renders small variant', () => {
const { container } = render(<Spinner small />)
expect(container.firstChild).toBeInTheDocument()
})
})

View File

@ -7,7 +7,7 @@ import { userMockConnected } from '../../../__mocks__/user-mock'
import { marketMock } from '../../../__mocks__/market-mock' import { marketMock } from '../../../__mocks__/market-mock'
describe('WalletSelector', () => { describe('WalletSelector', () => {
it('renders without crashing', async () => { it('renders without crashing', () => {
ReactModal.setAppElement(document.createElement('div')) ReactModal.setAppElement(document.createElement('div'))
const { container, getByText } = render( const { container, getByText } = render(
@ -20,11 +20,11 @@ describe('WalletSelector', () => {
expect(container.firstChild).toBeInTheDocument() expect(container.firstChild).toBeInTheDocument()
fireEvent.click(getByText('Select wallet')) fireEvent.click(getByText('Select wallet'))
const burnerButton = await getByText('Burner Wallet') const burnerButton = getByText('Burner Wallet')
fireEvent.click(burnerButton) fireEvent.click(burnerButton)
fireEvent.click(getByText('Select wallet')) fireEvent.click(getByText('Select wallet'))
// const metamaskButton = await getByText('MetaMask') // const metamaskButton = getByText('MetaMask')
// fireEvent.click(metamaskButton) // fireEvent.click(metamaskButton)
}) })
}) })

View File

@ -34,7 +34,30 @@ describe('Report', () => {
expect(comment).toHaveTextContent('Plants') expect(comment).toHaveTextContent('Plants')
fireEvent.click(getByTestId('report')) fireEvent.click(getByTestId('report'))
mockAxios.mockResponse(mockResponse) mockAxios.mockResponse(mockResponse)
// expect(mockAxios.post).toHaveBeenCalled() expect(mockAxios).toHaveBeenCalled()
// close modal
fireEvent.click(getByTestId('closeModal'))
})
it('catches response error', async () => {
ReactModal.setAppElement(document.createElement('div'))
const { getByText, getByLabelText, getByTestId } = render(
<Report did="did:xxx" title="Hello" />
)
// open modal
fireEvent.click(getByText('Report Data Set'))
await wait(() => expect(getByText('did:xxx')).toBeInTheDocument())
// add comment
const comment = getByLabelText('Comment')
fireEvent.change(comment, {
target: { value: 'Plants' }
})
expect(comment).toHaveTextContent('Plants')
fireEvent.click(getByTestId('report'))
mockAxios.mockError({ message: 'Error catch' })
// close modal // close modal
fireEvent.click(getByTestId('closeModal')) fireEvent.click(getByTestId('closeModal'))

View File

@ -1,5 +1,5 @@
import React from 'react' import React from 'react'
import { render } from '@testing-library/react' import { render, fireEvent, act } from '@testing-library/react'
import Ipfs from '.' import Ipfs from '.'
const addFile = jest.fn() const addFile = jest.fn()
@ -12,6 +12,6 @@ describe('Ipfs', () => {
expect(container.firstChild).toBeInTheDocument() expect(container.firstChild).toBeInTheDocument()
// wait for IPFS node // wait for IPFS node
await findByText(/IPFS /) await findByText(/Connected to /)
}) })
}) })

View File

@ -1,4 +1,20 @@
import { formatBytes } from './utils' import mockAxios from 'jest-mock-axios'
import { formatBytes, pingUrl } from './utils'
const mockResponse = {
status: 200,
data: {}
}
const mockResponseFaulty = {
status: 404,
statusText: 'Not Found',
data: {}
}
afterEach(() => {
mockAxios.reset()
})
describe('utils', () => { describe('utils', () => {
it('formatBytes outputs as expected', () => { it('formatBytes outputs as expected', () => {
@ -6,4 +22,26 @@ describe('utils', () => {
const output = formatBytes(number, 0) const output = formatBytes(number, 0)
expect(output).toBe('1 KB') expect(output).toBe('1 KB')
}) })
it('formatBytes 0 conversion', () => {
const number = 0
const output = formatBytes(number, 0)
expect(output).toBe('0 Bytes')
})
it('pingUrl can be called', () => {
pingUrl('https://oceanprotocol.com')
mockAxios.mockResponse(mockResponse)
expect(mockAxios).toHaveBeenCalled()
})
it('pingUrl can be called with non 200 response', () => {
pingUrl('https://oceanprotocol.com')
mockAxios.mockResponse(mockResponseFaulty)
})
it('pingUrl error catch', () => {
pingUrl('https://oceanprotocol.com')
mockAxios.mockError({ message: 'Error catch' })
})
}) })

View File

@ -34,6 +34,35 @@ const mockResponse = {
} }
} }
function flushPromises(ui: any, container: any) {
return new Promise(resolve =>
setImmediate(() => {
render(ui, { container })
resolve(container)
})
)
}
function dispatchEvt(node: any, type: string, data: any) {
const event = new Event(type, { bubbles: true })
Object.assign(event, data)
fireEvent(node, event)
}
function mockData(files: any) {
return {
dataTransfer: {
files,
items: files.map((file: any) => ({
kind: 'file',
type: file.type,
getAsFile: () => file
})),
types: ['Files']
}
}
}
const renderComponent = () => const renderComponent = () =>
render( render(
<Files <Files
@ -75,7 +104,7 @@ describe('Files', () => {
expect(container.querySelector('.ipfsForm')).toBeInTheDocument() expect(container.querySelector('.ipfsForm')).toBeInTheDocument()
// wait for IPFS node // wait for IPFS node
await findByText(/IPFS /) await findByText(/Connected to /)
// close // close
fireEvent.click(getByText('- Cancel')) fireEvent.click(getByText('- Cancel'))