Skip to content

Commit

Permalink
adding requestId
Browse files Browse the repository at this point in the history
  • Loading branch information
cauemarcondes committed Feb 10, 2021
1 parent 90ccb07 commit e90fa6c
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async function renderTooltipAnchor({
}) {
// mock api response
jest.spyOn(hooks, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: { jobs },
status: hooks.FETCH_STATUS.SUCCESS,
refetch: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { render } from '../../utils/test_helper';
describe('SelectableUrlList', () => {
it('it uses search term value from url', () => {
jest.spyOn(fetcherHook, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: {},
status: fetcherHook.FETCH_STATUS.SUCCESS,
refetch: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { KeyUXMetrics } from './KeyUXMetrics';
describe('KeyUXMetrics', () => {
it('renders metrics with correct formats', () => {
jest.spyOn(fetcherHook, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: {
noOfLongTasks: 3.0009765625,
sumOfLongTasks: 520.4375,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ describe('ServiceMap', () => {
describe('with an empty response', () => {
it('renders the empty banner', async () => {
jest.spyOn(useFetcherModule, 'useFetcher').mockReturnValueOnce({
requestId: 'foo',
data: { elements: [] },
refetch: () => {},
status: useFetcherModule.FETCH_STATUS.SUCCESS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ storiesOf(
<AgentConfigurationCreateEdit
pageStep="choose-settings-step"
existingConfigResult={{
requestId: 'foo',
status: FETCH_STATUS.SUCCESS,
data: {
service: { name: 'opbeans-node', environment: 'production' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { MockApmPluginContextWrapper } from '../../../../context/apm_plugin/mock
describe('ApmIndices', () => {
it('should not get stuck in infinite loop', () => {
const spy = jest.spyOn(hooks, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: undefined,
status: hooks.FETCH_STATUS.LOADING,
refetch: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('CustomLink', () => {
describe('empty prompt', () => {
beforeAll(() => {
jest.spyOn(hooks, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: [],
status: hooks.FETCH_STATUS.SUCCESS,
refetch: jest.fn(),
Expand All @@ -93,6 +94,7 @@ describe('CustomLink', () => {
describe('overview', () => {
beforeAll(() => {
jest.spyOn(hooks, 'useFetcher').mockReturnValue({
requestId: 'foo',
data,
status: hooks.FETCH_STATUS.SUCCESS,
refetch: jest.fn(),
Expand Down Expand Up @@ -172,6 +174,7 @@ describe('CustomLink', () => {
beforeAll(() => {
saveCustomLinkSpy = jest.spyOn(saveCustomLink, 'saveCustomLink');
jest.spyOn(hooks, 'useFetcher').mockReturnValue({
requestId: 'foo',
data,
status: hooks.FETCH_STATUS.SUCCESS,
refetch,
Expand Down Expand Up @@ -290,6 +293,7 @@ describe('CustomLink', () => {
describe('invalid license', () => {
beforeAll(() => {
jest.spyOn(hooks, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: [],
status: hooks.FETCH_STATUS.SUCCESS,
refetch: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ describe('TraceLink', () => {
},
});
jest.spyOn(hooks, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: { transaction: undefined },
status: hooks.FETCH_STATUS.SUCCESS,
refetch: jest.fn(),
Expand Down Expand Up @@ -111,6 +112,7 @@ describe('TraceLink', () => {
trace: { id: 123 },
};
jest.spyOn(hooks, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: { transaction },
status: hooks.FETCH_STATUS.SUCCESS,
refetch: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('ServiceIcons', () => {
describe('icons', () => {
it('Shows loading spinner while fetching data', () => {
jest.spyOn(fetcherHook, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: undefined,
status: fetcherHook.FETCH_STATUS.LOADING,
refetch: jest.fn(),
Expand All @@ -70,6 +71,7 @@ describe('ServiceIcons', () => {
});
it("doesn't show any icons", () => {
jest.spyOn(fetcherHook, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: {},
status: fetcherHook.FETCH_STATUS.SUCCESS,
refetch: jest.fn(),
Expand All @@ -87,6 +89,7 @@ describe('ServiceIcons', () => {
});
it('shows service icon', () => {
jest.spyOn(fetcherHook, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: {
agentName: 'java',
},
Expand All @@ -106,6 +109,7 @@ describe('ServiceIcons', () => {
});
it('shows service and container icons', () => {
jest.spyOn(fetcherHook, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: {
agentName: 'java',
containerType: 'Kubernetes',
Expand All @@ -126,6 +130,7 @@ describe('ServiceIcons', () => {
});
it('shows service, container and cloud icons', () => {
jest.spyOn(fetcherHook, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: {
agentName: 'java',
containerType: 'Kubernetes',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function ServiceOverviewTransactionsTable({ serviceName }: Props) {
urlParams: { start, end, latencyAggregationType },
} = useUrlParams();

const { data = INITIAL_STATE, status } = useFetcher(
const { data = INITIAL_STATE, status, requestId } = useFetcher(
(callApmApi) => {
if (!start || !end || !latencyAggregationType || !transactionType) {
return;
Expand Down Expand Up @@ -130,9 +130,9 @@ export function ServiceOverviewTransactionsTable({ serviceName }: Props) {
});
}
},
// only fetches statistics when transaction names changes
// only fetches statistics when transaction names or requestId changes
// eslint-disable-next-line react-hooks/exhaustive-deps
[transactionNames],
[requestId, transactionNames],
{ preservePreviousData: false }
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const transaction = ({
describe('Custom links', () => {
it('shows empty message when no custom link is available', () => {
jest.spyOn(useFetcher, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: [],
status: useFetcher.FETCH_STATUS.SUCCESS,
refetch: jest.fn(),
Expand All @@ -58,6 +59,7 @@ describe('Custom links', () => {

it('shows loading while custom links are fetched', () => {
jest.spyOn(useFetcher, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: [],
status: useFetcher.FETCH_STATUS.LOADING,
refetch: jest.fn(),
Expand All @@ -79,6 +81,7 @@ describe('Custom links', () => {
] as CustomLinkType[];

jest.spyOn(useFetcher, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: customLinks,
status: useFetcher.FETCH_STATUS.SUCCESS,
refetch: jest.fn(),
Expand All @@ -101,6 +104,7 @@ describe('Custom links', () => {
] as CustomLinkType[];

jest.spyOn(useFetcher, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: customLinks,
status: useFetcher.FETCH_STATUS.SUCCESS,
refetch: jest.fn(),
Expand All @@ -125,6 +129,7 @@ describe('Custom links', () => {
describe('create custom link buttons', () => {
it('shows create button below empty message', () => {
jest.spyOn(useFetcher, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: [],
status: useFetcher.FETCH_STATUS.SUCCESS,
refetch: jest.fn(),
Expand All @@ -148,6 +153,7 @@ describe('Custom links', () => {
] as CustomLinkType[];

jest.spyOn(useFetcher, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: customLinks,
status: useFetcher.FETCH_STATUS.SUCCESS,
refetch: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const renderTransaction = async (transaction: Record<string, any>) => {
describe('TransactionActionMenu component', () => {
beforeAll(() => {
jest.spyOn(hooks, 'useFetcher').mockReturnValue({
requestId: 'foo',
data: [],
status: hooks.FETCH_STATUS.SUCCESS,
refetch: jest.fn(),
Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/apm/public/hooks/use_fetcher.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('useFetcher', () => {

it('should have loading spinner initally', async () => {
expect(hook.result.current).toEqual({
requestId: '',
data: undefined,
error: undefined,
refetch: expect.any(Function),
Expand All @@ -44,6 +45,7 @@ describe('useFetcher', () => {
jest.advanceTimersByTime(100);

expect(hook.result.current).toEqual({
requestId: '',
data: undefined,
error: undefined,
refetch: expect.any(Function),
Expand All @@ -56,6 +58,7 @@ describe('useFetcher', () => {
await hook.waitForNextUpdate();

expect(hook.result.current).toEqual({
requestId: expect.any(String),
data: 'response from hook',
error: undefined,
refetch: expect.any(Function),
Expand All @@ -82,6 +85,7 @@ describe('useFetcher', () => {

it('should have loading spinner initally', async () => {
expect(hook.result.current).toEqual({
requestId: '',
data: undefined,
error: undefined,
refetch: expect.any(Function),
Expand All @@ -93,6 +97,7 @@ describe('useFetcher', () => {
jest.advanceTimersByTime(100);

expect(hook.result.current).toEqual({
requestId: '',
data: undefined,
error: undefined,
refetch: expect.any(Function),
Expand All @@ -105,6 +110,7 @@ describe('useFetcher', () => {
await hook.waitForNextUpdate();

expect(hook.result.current).toEqual({
requestId: expect.any(String),
data: undefined,
error: expect.any(Error),
refetch: expect.any(Function),
Expand All @@ -129,6 +135,7 @@ describe('useFetcher', () => {
}
);
expect(hook.result.current).toEqual({
requestId: expect.any(String),
data: undefined,
error: undefined,
refetch: expect.any(Function),
Expand All @@ -139,6 +146,7 @@ describe('useFetcher', () => {

// assert: first response has loaded and should be rendered
expect(hook.result.current).toEqual({
requestId: expect.any(String),
data: 'first response',
error: undefined,
refetch: expect.any(Function),
Expand All @@ -158,6 +166,7 @@ describe('useFetcher', () => {

// assert: while loading new data the previous data should still be rendered
expect(hook.result.current).toEqual({
requestId: expect.any(String),
data: 'first response',
error: undefined,
refetch: expect.any(Function),
Expand All @@ -169,6 +178,7 @@ describe('useFetcher', () => {

// assert: "second response" has loaded and should be rendered
expect(hook.result.current).toEqual({
requestId: expect.any(String),
data: 'second response',
error: undefined,
refetch: expect.any(Function),
Expand Down
7 changes: 7 additions & 0 deletions x-pack/plugins/apm/public/hooks/use_fetcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { i18n } from '@kbn/i18n';
import React, { useEffect, useMemo, useState } from 'react';
import { IHttpFetchError } from 'src/core/public';
import uuid from 'uuid';
import { toMountPoint } from '../../../../../src/plugins/kibana_react/public';
import {
callApmApi,
Expand All @@ -24,6 +25,7 @@ export enum FETCH_STATUS {
}

export interface FetcherResult<Data> {
requestId: string;
data?: Data;
status: FETCH_STATUS;
error?: IHttpFetchError;
Expand Down Expand Up @@ -75,6 +77,7 @@ export function useFetcher<TReturn>(
>({
data: undefined,
status: FETCH_STATUS.NOT_INITIATED,
requestId: '',
});
const [counter, setCounter] = useState(0);
const { rangeId } = useUrlParams();
Expand All @@ -83,6 +86,7 @@ export function useFetcher<TReturn>(
let controller: AbortController = new AbortController();

async function doFetch() {
const requestId = uuid();
controller.abort();

controller = new AbortController();
Expand All @@ -98,6 +102,7 @@ export function useFetcher<TReturn>(
}

setResult((prevResult) => ({
requestId: prevResult.requestId,
data: preservePreviousData ? prevResult.data : undefined, // preserve data from previous state while loading next state
status: FETCH_STATUS.LOADING,
error: undefined,
Expand All @@ -111,6 +116,7 @@ export function useFetcher<TReturn>(
// aborted before updating the result.
if (!signal.aborted) {
setResult({
requestId,
data,
status: FETCH_STATUS.SUCCESS,
error: undefined,
Expand Down Expand Up @@ -143,6 +149,7 @@ export function useFetcher<TReturn>(
});
}
setResult({
requestId,
data: undefined,
status: FETCH_STATUS.FAILURE,
error: e,
Expand Down

0 comments on commit e90fa6c

Please sign in to comment.