|
import { mount } from 'enzyme'; |
|
import fetchMock from 'jest-fetch-mock' |
|
import React from 'react'; |
|
import { act } from 'react-dom/test-utils'; |
|
import { getCountStatus } from '../../services/__mocks__/counts.service'; |
|
import CountingBanner from '../counting-banner'; |
|
|
|
jest.mock('../../services/api/counts.service', () => ({ |
|
getCountStatus() { |
|
return jest.fn(); |
|
}, |
|
__esModule: true, |
|
})); |
|
|
|
describe( '<CountingBanner/>', () => { |
|
let wrapper = { |
|
// Mock Wrapper until it gets set |
|
text() {new Error( 'Wrapper was not set!' )}, |
|
update() { this.text() } |
|
}; |
|
|
|
beforeAll( () => { |
|
jest.useFakeTimers(); |
|
} ); |
|
|
|
afterEach( () => { |
|
jest.clearAllTimers(); |
|
fetchMock.resetMocks(); |
|
} ); |
|
|
|
afterAll( () => { |
|
jest.useRealTimers(); |
|
} ); |
|
|
|
test( 'default state where it checks for count', () => { |
|
// @ts-ignore |
|
getCountStatus.mockReturnValue(Promise.resolve({status: 201})); |
|
act( () => { |
|
wrapper = mount( <CountingBanner/> ); |
|
} ); |
|
expect( wrapper.text() ).toBe( 'Checking for count...' ) |
|
} ); |
|
|
|
test( 'count is in progress', async () => { |
|
// @ts-ignore |
|
getCountStatus.mockResolvedValue({status: 201}); |
|
act( () => { |
|
wrapper = mount( <CountingBanner/> ); |
|
} ) |
|
expect( wrapper.text() ).toBe( 'Checking for count...' ); |
|
|
|
await act( async () => { |
|
jest.advanceTimersByTime( 1000 ); |
|
wrapper.update(); |
|
await Promise.resolve(); |
|
} ); |
|
|
|
expect( wrapper.text() ).toBe( 'Count in Progress...' ); |
|
} ); |
|
|
|
test( 'count is done', async () => { |
|
// @ts-ignore |
|
getCountStatus.mockResolvedValue({status: 200}); |
|
act( () => { |
|
wrapper = mount( <CountingBanner/> ); |
|
} ) |
|
|
|
expect( wrapper.text() ).toBe( 'Checking for count...' ); |
|
await act( async () => { |
|
jest.advanceTimersByTime( 1000 ); |
|
wrapper.update(); |
|
await Promise.resolve(); |
|
} ); |
|
|
|
expect( wrapper.text() ).toBe( 'Counting finished!' ); |
|
} ); |
|
|
|
// FIXME Write this test once the above issue is resolved. |
|
test.skip( 'count had an error', async () => { |
|
// @ts-ignore |
|
getCountStatus.mockRejectedValue('any error'); |
|
act( () => { |
|
wrapper = mount( <CountingBanner/> ); |
|
} ) |
|
expect( wrapper.text() ).toBe( 'Checking for count...' ); |
|
|
|
act( () => { |
|
// FIXME cannot get status in counting-banner to equal what body is here. Am I missing something? |
|
// https://github.com/jefflau/jest-fetch-mock#simple-mock-and-assert |
|
fetchMock.mockReject( new Error( 'any error' ) ); |
|
jest.advanceTimersByTime( 1000 ); |
|
wrapper.update(); |
|
} ); |
|
|
|
await Promise.resolve(); |
|
|
|
expect( wrapper.text() ).toBe( 'Could not determine count doneness!' ); |
|
} ) |
|
} ); |