Skip to content

Commit

Permalink
Refactor FirstLastSeen to use useSearchStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
machadoum committed Jul 26, 2022
1 parent bb0365e commit 69d31e8
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,17 @@ import { renderHook } from '@testing-library/react-hooks';

import { Direction } from '../../../../common/search_strategy';
import type { FirstLastSeenProps } from '../../components/first_last_seen/first_last_seen';
import { useKibana } from '../../lib/kibana';
import { useAppToasts } from '../../hooks/use_app_toasts';
import * as i18n from './translations';
import type { UseFirstLastSeen } from './use_first_last_seen';
import { useFirstLastSeen } from './use_first_last_seen';

jest.mock('../../lib/kibana');
jest.mock('../../hooks/use_app_toasts');
import { useSearchStrategy } from '../use_search_strategy';

const firstSeen = '2022-06-03T19:48:36.165Z';
const lastSeen = '2022-06-13T19:48:36.165Z';
jest.mock('../use_search_strategy', () => ({
useSearchStrategy: jest.fn(),
}));

const mockSearchStrategy = jest.fn();

const mockAddError = jest.fn();
const mockAddWarning = jest.fn();

(useAppToasts as jest.Mock).mockReturnValue({
addError: mockAddError,
addWarning: mockAddWarning,
});

const mockKibana = (useKibana as jest.Mock).mockReturnValue({
services: {
data: {
search: {
search: mockSearchStrategy.mockReturnValue({
unsubscribe: jest.fn(),
subscribe: jest.fn(({ next, error }) => {
next({ firstSeen });
return {
unsubscribe: jest.fn(),
};
}),
}),
},
query: jest.fn(),
},
},
});
const mockUseSearchStrategy = useSearchStrategy as jest.Mock;
const mockSearch = jest.fn();

const renderUseFirstLastSeen = (overrides?: Partial<UseFirstLastSeen>) =>
renderHook<FirstLastSeenProps, ReturnType<typeof useFirstLastSeen>>(() =>
Expand All @@ -62,145 +33,103 @@ const renderUseFirstLastSeen = (overrides?: Partial<UseFirstLastSeen>) =>
);

describe('useFistLastSeen', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should return default values', () => {
mockSearchStrategy.mockReturnValueOnce({
subscribe: jest.fn(),
});
mockUseSearchStrategy.mockImplementation(({ initialResult }) => ({
loading: true,
result: initialResult,
search: mockSearch,
refetch: jest.fn(),
inspect: {},
}));

const { result } = renderUseFirstLastSeen();

expect(result.current).toEqual([
true,
{
errorMessage: null,
firstSeen: null,
id: 'firstLastSeenQuery',
lastSeen: null,
order: null,
},
]);
});

it('should return parsed items for first seen', () => {
mockUseSearchStrategy.mockImplementation(() => ({
loading: false,
result: {
firstSeen: '2022-06-03T19:48:36.165Z',
},
search: mockSearch,
refetch: jest.fn(),
inspect: {},
}));

const { result } = renderUseFirstLastSeen();

expect(mockSearchStrategy).toHaveBeenCalledWith(
{
defaultIndex: [],
factoryQueryType: 'firstlastseen',
field: 'host.name',
order: 'asc',
value: 'some-host',
},
{
abortSignal: new AbortController().signal,
strategy: 'securitySolutionSearchStrategy',
}
);
expect(mockSearch).toHaveBeenCalledWith({
defaultIndex: [],
factoryQueryType: 'firstlastseen',
field: 'host.name',
order: 'asc',
value: 'some-host',
});

expect(result.current).toEqual([
false,
{
errorMessage: null,
firstSeen: '2022-06-03T19:48:36.165Z',
id: 'firstLastSeenQuery',
order: null,
},
]);
});

it('should return parsed items for last seen', () => {
mockKibana.mockReturnValueOnce({
services: {
data: {
search: {
search: mockSearchStrategy.mockReturnValue({
unsubscribe: jest.fn(),
subscribe: jest.fn(({ next, error }) => {
next({ lastSeen });
return {
unsubscribe: jest.fn(),
};
}),
}),
},
query: jest.fn(),
},
mockUseSearchStrategy.mockImplementation(() => ({
loading: false,
result: {
lastSeen: '2022-06-13T19:48:36.165Z',
},
});
search: mockSearch,
refetch: jest.fn(),
inspect: {},
}));

const { result } = renderUseFirstLastSeen({ order: Direction.desc });

expect(mockSearchStrategy).toHaveBeenCalledWith(
{
defaultIndex: [],
factoryQueryType: 'firstlastseen',
field: 'host.name',
order: 'desc',
value: 'some-host',
},
{
abortSignal: new AbortController().signal,
strategy: 'securitySolutionSearchStrategy',
}
);
expect(mockSearch).toHaveBeenCalledWith({
defaultIndex: [],
factoryQueryType: 'firstlastseen',
field: 'host.name',
order: 'desc',
value: 'some-host',
});

expect(result.current).toEqual([
false,
{
errorMessage: null,
lastSeen: '2022-06-13T19:48:36.165Z',
id: 'firstLastSeenQuery',
order: null,
},
]);
});
it('should handle a partial, no longer running response', () => {
mockKibana.mockReturnValueOnce({
services: {
data: {
search: {
search: mockSearchStrategy.mockReturnValue({
unsubscribe: jest.fn(),
subscribe: jest.fn(({ next, error }) => {
next({ isRunning: false, isPartial: true });
return {
unsubscribe: jest.fn(),
};
}),
}),
},
query: jest.fn(),
},
},
});

renderUseFirstLastSeen({ order: Direction.desc });
expect(mockAddWarning).toHaveBeenCalledWith(i18n.ERROR_FIRST_LAST_SEEN_HOST);
});

it('should handle an error with search strategy', () => {
const msg = 'What in tarnation!?';
mockKibana.mockReturnValueOnce({
services: {
data: {
search: {
search: mockSearchStrategy.mockReturnValue({
unsubscribe: jest.fn(),
subscribe: jest.fn(({ next, error }) => {
error(msg);
return {
unsubscribe: jest.fn(),
};
}),
}),
},
query: jest.fn(),
},
},
});
mockUseSearchStrategy.mockImplementation(() => ({
loading: false,
result: {},
error: new Error(msg),
search: mockSearch,
refetch: jest.fn(),
inspect: {},
}));

renderUseFirstLastSeen({ order: Direction.desc });
expect(mockAddError).toHaveBeenCalledWith(msg, {
title: i18n.FAIL_FIRST_LAST_SEEN_HOST,
});
const { result } = renderUseFirstLastSeen({ order: Direction.desc });
expect(result.current).toEqual([false, { errorMessage: `Error: ${msg}` }]);
});
});
Loading

0 comments on commit 69d31e8

Please sign in to comment.