1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-11-15 01:34:57 +01:00
market/tests/unit/pages/search.test.tsx

81 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-05-07 08:03:30 +02:00
import React from 'react'
import { render, fireEvent } from '@testing-library/react'
import { NextPageContext } from 'next'
import Search from '../../../src/pages/search'
import ddo from '../__fixtures__/ddo'
import { DDO } from '@oceanprotocol/squid'
import { QueryResult } from '@oceanprotocol/squid/dist/node/aquarius/Aquarius'
describe('Search', () => {
it('renders without crashing', async () => {
const queryResult = {
results: [new DDO(ddo), new DDO(ddo)],
page: 1,
totalPages: 10,
totalResults: 200
}
const { container } = render(
2020-05-19 11:20:18 +02:00
<Search text="Hello" tag="Hello" queryResult={queryResult} />
2020-05-07 08:03:30 +02:00
)
expect(container.firstChild).toBeInTheDocument()
// interact with search bar
const form = container.querySelector('form')
const input = container.querySelector('form input')
const button = container.querySelector('form button')
input &&
fireEvent.change(input, {
target: {
value: 'Changed Hello'
}
})
button && fireEvent.click(button)
form && fireEvent.submit(form)
// interact with pagination
const pageItem = container.querySelector(
'.pagination li:nth-child(3) .number'
)
pageItem && fireEvent.click(pageItem)
})
it('renders empty state', async () => {
const queryResult: QueryResult = {
results: [],
page: 1,
totalPages: 1,
totalResults: 0
}
const { container } = render(
2020-05-19 11:20:18 +02:00
<Search text="Hello" tag="Hello" queryResult={queryResult} />
2020-05-07 08:03:30 +02:00
)
expect(container.querySelector('.empty')).toBeInTheDocument()
})
})
describe('Search.getInitialProps', () => {
it('returns the corresponding props', async () => {
const getInitialProps = Search.getInitialProps as (
context: Partial<NextPageContext>
) => {
text: string
tag: string
queryResult: QueryResult
}
const context = {
query: {
text: 'text',
tag: 'tag',
page: '1',
offset: '1'
}
}
const props = await getInitialProps(context)
expect(props.text).toEqual('text')
expect(props.tag).toEqual('tag')
})
})