Skip to content

Commit

Permalink
add more test coverage to useCheckPublishCompleted.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ekraffmiller committed Jul 8, 2024
1 parent b1892ca commit ce527a7
Showing 1 changed file with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
/// <reference types="cypress" />

import { DatasetRepository } from '../../../../src/dataset/domain/repositories/DatasetRepository'
import { renderHook } from '@testing-library/react'
import { renderHook, waitFor } from '@testing-library/react'
import useCheckPublishCompleted from '../../../../src/sections/dataset/useCheckPublishCompleted'
import { DatasetMother } from '../../dataset/domain/models/DatasetMother'
import { DatasetMockRepository } from '../../../../src/stories/dataset/DatasetMockRepository'
import { Dataset } from '../../../../src/dataset/domain/models/Dataset'

interface TestComponentProps {
publishInProgress: boolean | undefined
dataset: Dataset | undefined
datasetRepository: DatasetRepository
}
function TestComponent({ publishInProgress, dataset, datasetRepository }: TestComponentProps) {
const publishCompleted = useCheckPublishCompleted(publishInProgress, dataset, datasetRepository)

return <div data-testid="publish-completed">{publishCompleted ? 'true' : 'false'}</div>
}

describe('useCheckPublishCompleted Hook', () => {
beforeEach(() => {
const datasetRepository: DatasetRepository = {} as DatasetRepository
datasetRepository.getLocks = cy.stub().resolves([])
})
it('should not set publishCompleted if publishInProgress is false', () => {
const datasetRepository: DatasetRepository = {} as DatasetRepository
const { result } = renderHook(() =>
Expand All @@ -20,6 +28,18 @@ describe('useCheckPublishCompleted Hook', () => {
cy.wrap(result.current).should('equal', false)
})
})
it('should not set publishCompleted to true when locks are found initially', () => {
const datasetRepository: DatasetMockRepository = new DatasetMockRepository()
cy.stub(datasetRepository, 'getLocks').resolves([{ lockId: 'test-lock' }])

const { result } = renderHook(() =>
useCheckPublishCompleted(true, DatasetMother.create(), datasetRepository)
)

cy.wait(3000).then(() => {
cy.wrap(result.current).should('equal', false)
})
})
it('should set publishCompleted to true when no locks are found initially', () => {
const datasetRepository: DatasetRepository = {} as DatasetRepository
datasetRepository.getLocks = cy.stub().resolves([])
Expand All @@ -34,20 +54,22 @@ describe('useCheckPublishCompleted Hook', () => {

it('should set publishCompleted to true after polling finds no locks', () => {
const datasetRepository: DatasetMockRepository = new DatasetMockRepository()
const getLocksStub = cy.stub(datasetRepository, 'getLocks')

cy.stub(datasetRepository, 'getLocks')
.resolves([{}])
.onFirstCall()
.resolves([])
.callsFake(() => Promise.resolve([]))
// First call finds locks, subsequent calls find no locks
getLocksStub.onFirstCall().resolves([{ lockId: 'test-lock' }])
getLocksStub.callsFake(() => Promise.resolve([]))

const { result } = renderHook(() =>
useCheckPublishCompleted(true, DatasetMother.create(), datasetRepository)
cy.customMount(
<TestComponent
publishInProgress={true}
dataset={DatasetMother.create()}
datasetRepository={datasetRepository}
/>
)

cy.wrap(result.current).should('equal', false)
cy.get('[data-testid="publish-completed"]').should('have.text', 'false')
cy.wait(3000).then(() => {
cy.wrap(result.current).should('equal', true)
cy.get('[data-testid="publish-completed"]').should('have.text', 'true')
})
})
})

0 comments on commit ce527a7

Please sign in to comment.