ElectronicBabylonianLiterature/ebl-frontend

View on GitHub
src/afo-register/ui/AfoRegisterSearch.test.tsx

Summary

Maintainability
A
0 mins
Test Coverage
import React from 'react'
import { render, screen, waitFor, act } from '@testing-library/react'
import AfoRegisterService from 'afo-register/application/AfoRegisterService'
import AfoRegisterSearch from 'afo-register/ui/AfoRegisterSearch'
import { waitForSpinnerToBeRemoved } from 'test-support/waitForSpinnerToBeRemoved'
import FragmentService from 'fragmentarium/application/FragmentService'
import Bluebird from 'bluebird'
import { afoRegisterRecordFactory } from 'test-support/afo-register-fixtures'

jest.mock('afo-register/application/AfoRegisterService')
jest.mock('react-router-dom', () => ({
  ...jest.requireActual('react-router-dom'),
  useHistory: jest.fn(),
}))

const afoRegisterService = new (AfoRegisterService as jest.Mock<
  jest.Mocked<AfoRegisterService>
>)()

const fragmentService = new (FragmentService as jest.Mock<
  jest.Mocked<FragmentService>
>)()

describe('AfO-Register search display', () => {
  const mockQuery = { text: 'testText', textNumber: 'testNumber' }
  let record

  beforeEach(() => {
    jest.clearAllMocks()
    record = afoRegisterRecordFactory.build()
  })

  it('renders correctly with initial state', async () => {
    afoRegisterService.search.mockReturnValue(Bluebird.resolve([record]))
    await act(async () => {
      render(
        <AfoRegisterSearch
          query={mockQuery}
          afoRegisterService={afoRegisterService}
          fragmentService={fragmentService}
        />
      )
      await waitForSpinnerToBeRemoved(screen)
      await waitFor(() => {
        const item = screen.getByRole('listitem')
        expect(item).toBeVisible()
        expect(item).toHaveTextContent(record.text)
      })
    })
  })

  it('renders correctly with empty result', async () => {
    afoRegisterService.search.mockReturnValue(Bluebird.resolve([]))
    await act(async () => {
      render(
        <AfoRegisterSearch
          query={mockQuery}
          afoRegisterService={afoRegisterService}
          fragmentService={fragmentService}
        />
      )
      await waitForSpinnerToBeRemoved(screen)
      await waitFor(() => {
        expect(screen.getByText('No records found')).toBeVisible()
      })
    })
  })
})