Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Jul 11, 2020
1 parent c0c4261 commit 5decd1d
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { mockEventViewerResponse } from './mock';
import { StatefulEventsViewer } from '.';
import { defaultHeaders } from './default_headers';
import { useFetchIndexPatterns } from '../../../detections/containers/detection_engine/rules/fetch_index_patterns';
import { mockBrowserFields } from '../../containers/source/mock';
import { mockBrowserFields, mockDocValueFields } from '../../containers/source/mock';
import { eventsDefaultModel } from './default_model';
import { useMountAppended } from '../../utils/use_mount_appended';

Expand All @@ -25,6 +25,7 @@ mockUseFetchIndexPatterns.mockImplementation(() => [
{
browserFields: mockBrowserFields,
indexPatterns: mockIndexPattern,
docValueFields: mockDocValueFields,
},
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ jest.mock('../../lib/kibana', () => ({
},
},
}),
KibanaServices: {
get: jest.fn(() => ({ uiSettings: { get: () => ({ from: 'now-24h', to: 'now' }) } })),
},
}));

describe('UrlStateContainer', () => {
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
});
describe('handleInitialize', () => {
describe('URL state updates redux', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import {
isRelativeTimeRange,
} from '../../store/inputs/model';

import { getTimeRangeSettings } from '../../utils/default_date_settings';

const getTimeRangeSettingsMock = getTimeRangeSettings as jest.Mock;

jest.mock('../../utils/default_date_settings');
jest.mock('@elastic/datemath', () => ({
parse: (date: string) => {
if (date === 'now') {
Expand All @@ -25,8 +30,15 @@ jest.mock('@elastic/datemath', () => ({
},
}));

getTimeRangeSettingsMock.mockImplementation(() => ({
from: '2020-07-04T08:20:18.966Z',
to: '2020-07-05T08:20:18.966Z',
fromStr: 'now-24h',
toStr: 'now',
}));

describe('#normalizeTimeRange', () => {
test('Absolute time range returns empty strings as empty strings', () => {
test('Absolute time range returns defaults for empty strings', () => {
const dateTimeRange: URLTimeRange = {
kind: 'absolute',
fromStr: undefined,
Expand All @@ -37,8 +49,8 @@ describe('#normalizeTimeRange', () => {
if (isAbsoluteTimeRange(dateTimeRange)) {
const expected: AbsoluteTimeRange = {
kind: 'absolute',
from: '2020-07-07T08:20:18.966Z',
to: '2020-07-08T08:20:18.966Z',
from: '2020-07-04T08:20:18.966Z',
to: '2020-07-05T08:20:18.966Z',
fromStr: undefined,
toStr: undefined,
};
Expand Down Expand Up @@ -120,7 +132,7 @@ describe('#normalizeTimeRange', () => {
}
});

test('Absolute time range returns empty string with from and to when garbage is sent in', () => {
test('Absolute time range returns defaults when garbage is sent in', () => {
const to = 'garbage';
const from = 'garbage';
const dateTimeRange: URLTimeRange = {
Expand All @@ -133,8 +145,8 @@ describe('#normalizeTimeRange', () => {
if (isAbsoluteTimeRange(dateTimeRange)) {
const expected: AbsoluteTimeRange = {
kind: 'absolute',
from: '2020-07-07T08:20:18.966Z',
to: '2020-07-08T08:20:18.966Z',
from: '2020-07-04T08:20:18.966Z',
to: '2020-07-05T08:20:18.966Z',
fromStr: undefined,
toStr: undefined,
};
Expand All @@ -144,7 +156,7 @@ describe('#normalizeTimeRange', () => {
}
});

test('Relative time range returns empty strings as empty strings', () => {
test('Relative time range returns defaults fro empty strings', () => {
const dateTimeRange: URLTimeRange = {
kind: 'relative',
fromStr: '',
Expand All @@ -155,8 +167,8 @@ describe('#normalizeTimeRange', () => {
if (isRelativeTimeRange(dateTimeRange)) {
const expected: RelativeTimeRange = {
kind: 'relative',
from: '2020-07-07T08:20:18.966Z',
to: '2020-07-08T08:20:18.966Z',
from: '2020-07-04T08:20:18.966Z',
to: '2020-07-05T08:20:18.966Z',
fromStr: '',
toStr: '',
};
Expand Down Expand Up @@ -238,7 +250,7 @@ describe('#normalizeTimeRange', () => {
}
});

test('Relative time range returns empty string with from and to when garbage is sent in', () => {
test('Relative time range returns defaults when garbage is sent in', () => {
const to = 'garbage';
const from = 'garbage';
const dateTimeRange: URLTimeRange = {
Expand All @@ -251,8 +263,8 @@ describe('#normalizeTimeRange', () => {
if (isRelativeTimeRange(dateTimeRange)) {
const expected: RelativeTimeRange = {
kind: 'relative',
from: '2020-07-07T08:20:18.966Z',
to: '2020-07-08T08:20:18.966Z',
from: '2020-07-04T08:20:18.966Z',
to: '2020-07-05T08:20:18.966Z',
fromStr: '',
toStr: '',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('Index Fields & Browser Fields', () => {

return expect(initialResult).toEqual({
browserFields: {},
docValueFields: [],
errorMessage: null,
indexPattern: {
fields: [],
Expand Down Expand Up @@ -56,6 +57,16 @@ describe('Index Fields & Browser Fields', () => {
current: {
indicesExist: true,
browserFields: mockBrowserFields,
docValueFields: [
{
field: '@timestamp',
format: 'date_time',
},
{
field: 'event.end',
format: 'date_time',
},
],
indexPattern: {
fields: mockIndexFields,
title:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ export const getdocValueFields = memoizeOne(
fields && fields.length > 0
? fields.reduce<DocValueFields[]>((accumulator: DocValueFields[], field: IndexField) => {
if (field.type === 'date' && accumulator.length < 100) {
return [...accumulator, { field: field.name, format: field.format ?? 'date_time' }];
return [
...accumulator,
{
field: field.name,
format: isEmpty(field.format) ? 'date_time' : field.format ?? 'date_time',
},
];
}
return accumulator;
}, [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { DEFAULT_INDEX_PATTERN } from '../../../../common/constants';

import { BrowserFields } from '.';
import { BrowserFields, DocValueFields } from '.';
import { sourceQuery } from './index.gql_query';

export const mocksSource = [
Expand Down Expand Up @@ -697,3 +697,14 @@ export const mockBrowserFields: BrowserFields = {
},
},
};

export const mockDocValueFields: DocValueFields[] = [
{
field: '@timestamp',
format: 'date_time',
},
{
field: 'event.end',
format: 'date_time',
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,16 @@ describe('useFetchIndexPatterns', () => {
'logs-*',
],
indicesExists: true,
docValueFields: [
{
field: '@timestamp',
format: 'date_time',
},
{
field: 'event.end',
format: 'date_time',
},
],
indexPatterns: {
fields: [
{ name: '@timestamp', searchable: true, type: 'date', aggregatable: true },
Expand Down Expand Up @@ -441,6 +451,7 @@ describe('useFetchIndexPatterns', () => {
expect(result.current).toEqual([
{
browserFields: {},
docValueFields: [],
indexPatterns: {
fields: [],
title: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ describe('helpers', () => {
},
],
dataProviders: [],
dateRange: { start: 'now-24h', end: 'now' },
description: '',
deletedEventIds: [],
eventIdToNoteIds: {},
Expand Down Expand Up @@ -356,6 +357,7 @@ describe('helpers', () => {
},
],
dataProviders: [],
dateRange: { start: 'now-24h', end: 'now' },
description: '',
deletedEventIds: [],
eventIdToNoteIds: {},
Expand Down Expand Up @@ -490,6 +492,7 @@ describe('helpers', () => {
],
version: '1',
dataProviders: [],
dateRange: { start: 'now-24h', end: 'now' },
description: '',
deletedEventIds: [],
eventIdToNoteIds: {},
Expand Down Expand Up @@ -611,6 +614,7 @@ describe('helpers', () => {
},
],
version: '1',
dateRange: { start: 'now-24h', end: 'now' },
dataProviders: [],
description: '',
deletedEventIds: [],
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { SubsetTimelineModel, TimelineModel } from './model';
export const timelineDefaults: SubsetTimelineModel & Pick<TimelineModel, 'filters'> = {
columns: defaultHeaders,
dataProviders: [],
dateRange: { start: 'now-24', end: 'now' },
dateRange: { start: 'now-24h', end: 'now' },
deletedEventIds: [],
description: '',
eventType: 'all',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ interface AddNewTimelineParams {
export const addNewTimeline = ({
columns,
dataProviders = [],
dateRange = { start: 'now-24', end: 'now' },
dateRange = { start: 'now-24h', end: 'now' },
filters = timelineDefaults.filters,
id,
itemsPerPage = timelineDefaults.itemsPerPage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ describe('createOptions', () => {
pagination: {
limit: 5,
},
docValueFields: [
{
field: '@timestamp',
format: 'date_time',
},
{
field: 'event.end',
format: 'date_time',
},
],
timerange: {
from: '2020-07-08T08:00:00.000Z',
to: '2020-07-08T20:00:00.000Z',
Expand Down Expand Up @@ -73,6 +83,16 @@ describe('createOptions', () => {
limit: 5,
},
filterQuery: {},
docValueFields: [
{
field: '@timestamp',
format: 'date_time',
},
{
field: 'event.end',
format: 'date_time',
},
],
fields: [],
timerange: {
from: '2020-07-08T08:00:00.000Z',
Expand Down Expand Up @@ -102,6 +122,47 @@ describe('createOptions', () => {
limit: 5,
},
filterQuery: {},
docValueFields: [
{
field: '@timestamp',
format: 'date_time',
},
{
field: 'event.end',
format: 'date_time',
},
],
fields: [],
timerange: {
from: '2020-07-08T08:00:00.000Z',
to: '2020-07-08T20:00:00.000Z',
interval: '12 hours ago',
},
};
expect(options).toEqual(expected);
});

test('should create options given all input except docValueFields', () => {
const argsWithoutSort: Args = omit('docValueFields', args);
const options = createOptions(source, argsWithoutSort, info);
const expected: RequestOptions = {
defaultIndex: DEFAULT_INDEX_PATTERN,
sourceConfiguration: {
fields: {
host: 'host-1',
container: 'container-1',
message: ['message-1'],
pod: 'pod-1',
tiebreaker: 'tiebreaker',
timestamp: 'timestamp-1',
},
},
sortField: { sortFieldId: 'sort-1', direction: Direction.asc },
pagination: {
limit: 5,
},
filterQuery: {},
docValueFields: [],
fields: [],
timerange: {
from: '2020-07-08T08:00:00.000Z',
Expand Down

0 comments on commit 5decd1d

Please sign in to comment.