-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for hooks and minor cleanup
- Loading branch information
Showing
7 changed files
with
295 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
x-pack/plugins/apm/public/hooks/useFetcher.integration.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { render } from 'react-testing-library'; | ||
import { delay, tick } from '../utils/testHelpers'; | ||
import { useFetcher } from './useFetcher'; | ||
|
||
// Suppress warnings about "act" until async/await syntax is supported: https://github.com/facebook/react/issues/14769 | ||
/* tslint:disable:no-console */ | ||
const originalError = console.error; | ||
beforeAll(() => { | ||
console.error = jest.fn(); | ||
}); | ||
afterAll(() => { | ||
console.error = originalError; | ||
}); | ||
|
||
async function asyncFn(name: string, ms: number) { | ||
await delay(ms); | ||
return `Hello from ${name}`; | ||
} | ||
|
||
describe('when simulating race condition', () => { | ||
let requestCallOrder: Array<[string, string, number]>; | ||
let renderSpy: jest.Mock; | ||
|
||
beforeEach(async () => { | ||
jest.useFakeTimers(); | ||
jest | ||
.spyOn(window, 'requestAnimationFrame') | ||
.mockImplementation(cb => cb(0) as any); | ||
|
||
renderSpy = jest.fn(); | ||
requestCallOrder = []; | ||
|
||
function MyComponent({ | ||
name, | ||
ms, | ||
renderFn | ||
}: { | ||
name: string; | ||
ms: number; | ||
renderFn: any; | ||
}) { | ||
const { data, status, error } = useFetcher( | ||
async () => { | ||
requestCallOrder.push(['request', name, ms]); | ||
const res = await asyncFn(name, ms); | ||
requestCallOrder.push(['response', name, ms]); | ||
return res; | ||
}, | ||
[name, ms] | ||
); | ||
renderFn({ data, status, error }); | ||
return null; | ||
} | ||
|
||
const { rerender } = render( | ||
<MyComponent name="John" ms={500} renderFn={renderSpy} /> | ||
); | ||
|
||
rerender(<MyComponent name="Peter" ms={100} renderFn={renderSpy} />); | ||
}); | ||
|
||
it('should render initially render loading state', async () => { | ||
expect(renderSpy).lastCalledWith({ | ||
data: undefined, | ||
error: undefined, | ||
status: 'loading' | ||
}); | ||
}); | ||
|
||
it('should render "Hello from Peter" after 200ms', async () => { | ||
jest.advanceTimersByTime(200); | ||
await tick(); | ||
|
||
expect(renderSpy).lastCalledWith({ | ||
data: 'Hello from Peter', | ||
error: undefined, | ||
status: 'success' | ||
}); | ||
}); | ||
|
||
it('should render "Hello from Peter" after 600ms', async () => { | ||
jest.advanceTimersByTime(600); | ||
await tick(); | ||
|
||
expect(renderSpy).lastCalledWith({ | ||
data: 'Hello from Peter', | ||
error: undefined, | ||
status: 'success' | ||
}); | ||
}); | ||
|
||
it('should should NOT have rendered "Hello from John" at any point', async () => { | ||
jest.advanceTimersByTime(600); | ||
await tick(); | ||
|
||
expect(renderSpy).not.toHaveBeenCalledWith({ | ||
data: 'Hello from John', | ||
error: undefined, | ||
status: 'success' | ||
}); | ||
}); | ||
|
||
it('should send and receive calls in the right order', async () => { | ||
jest.advanceTimersByTime(600); | ||
await tick(); | ||
|
||
expect(requestCallOrder).toEqual([ | ||
['request', 'John', 500], | ||
['request', 'Peter', 100], | ||
['response', 'Peter', 100], | ||
['response', 'John', 500] | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { cleanup, renderHook } from 'react-hooks-testing-library'; | ||
import { delay } from '../utils/testHelpers'; | ||
import { useFetcher } from './useFetcher'; | ||
|
||
afterEach(cleanup); | ||
|
||
// Suppress warnings about "act" until async/await syntax is supported: https://github.com/facebook/react/issues/14769 | ||
/* tslint:disable:no-console */ | ||
const originalError = console.error; | ||
beforeAll(() => { | ||
console.error = jest.fn(); | ||
}); | ||
afterAll(() => { | ||
console.error = originalError; | ||
}); | ||
|
||
describe('useFetcher', () => { | ||
let output: ReturnType<typeof renderHook>; | ||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
async function fn() { | ||
await delay(500); | ||
return 'response from hook'; | ||
} | ||
output = renderHook(() => useFetcher(() => fn(), [])); | ||
}); | ||
|
||
it('should initially be empty', async () => { | ||
expect(output.result.current).toEqual({ | ||
data: undefined, | ||
error: undefined, | ||
status: undefined | ||
}); | ||
}); | ||
|
||
it('should show loading spinner', async () => { | ||
await output.waitForNextUpdate(); | ||
expect(output.result.current).toEqual({ | ||
data: undefined, | ||
error: undefined, | ||
status: 'loading' | ||
}); | ||
}); | ||
|
||
it('should show success after 1 second', async () => { | ||
jest.advanceTimersByTime(1000); | ||
await output.waitForNextUpdate(); | ||
|
||
expect(output.result.current).toEqual({ | ||
data: 'response from hook', | ||
error: undefined, | ||
status: 'success' | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.