Skip to content

Commit

Permalink
happy merging back to legacy (elastic#39166)
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierM authored Jun 25, 2019
1 parent dc38f2a commit af21c66
Show file tree
Hide file tree
Showing 30 changed files with 971 additions and 741 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,9 @@
import { mount } from 'enzyme';
import * as React from 'react';

import { HookWrapper } from '../../mock/hook_wrapper';
import { globalNode, HookWrapper } from '../../mock';
import { scrollToTop } from '.';

interface Global extends NodeJS.Global {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
window?: any;
}

const globalNode: Global = global;

const spyScroll = jest.fn();
const spyScrollTo = jest.fn();

Expand Down

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

197 changes: 139 additions & 58 deletions x-pack/legacy/plugins/siem/public/components/url_state/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,34 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { mount, shallow } from 'enzyme';
import { Location } from 'history';
import { mount } from 'enzyme';
import toJson from 'enzyme-to-json';
import * as React from 'react';
import { Router } from 'react-router-dom';
import { MockedProvider } from 'react-apollo/test-utils';
import { StaticIndexPattern } from 'ui/index_patterns';

import { UrlStateContainer, UrlStateContainerLifecycle } from './';
import { UrlStateContainerPropTypes } from './types';
import {
apolloClientObservable,
globalNode,
HookWrapper,
mockGlobalState,
TestProviders,
} from '../../mock';
import { createStore, State } from '../../store';

import { UseUrlState } from './';
import {
defaultProps,
getMockPropsObj,
mockHistory,
serializedFilterQuery,
testCases,
} from './test_dependencies';
import { apolloClientObservable, mockGlobalState, TestProviders } from '../../mock';
import { createStore, State } from '../../store';
import { UrlStateContainerPropTypes } from './types';
import { useUrlStateHooks, initializeLocation } from './use_url_state';
import { wait } from 'react-testing-library';

let mockProps: UrlStateContainerPropTypes;

Expand Down Expand Up @@ -52,7 +61,9 @@ describe('UrlStateContainer', () => {
<MockedProvider>
<TestProviders store={store}>
<Router history={mockHistory}>
<UrlStateContainer indexPattern={indexPattern} />
<UseUrlState indexPattern={indexPattern}>
{({ isInitializing }) => <span>{isInitializing}</span>}
</UseUrlState>
</Router>
</TestProviders>
</MockedProvider>
Expand All @@ -68,59 +79,67 @@ describe('UrlStateContainer', () => {
test.each(testCases)('%o', (page, namespaceLower, namespaceUpper, examplePath, type) => {
mockProps = getMockPropsObj({ page, examplePath, namespaceLower, type })
.relativeTimeSearch.undefinedQuery;
shallow(<UrlStateContainerLifecycle {...mockProps} />);
// @ts-ignore property mock does not exists
expect(defaultProps.setRelativeTimerange.mock.calls[0][0]).toEqual({
from: 1558591200000,
fromStr: 'now-1d/d',
kind: 'relative',
to: 1558677599999,
toStr: 'now-1d/d',
id: 'global',
});
// @ts-ignore property mock does not exists
expect(defaultProps.setRelativeTimerange.mock.calls[1][0]).toEqual({
from: 1558732849370,
fromStr: 'now-15m',
kind: 'relative',
to: 1558733749370,
toStr: 'now',
id: 'timeline',
mount(<HookWrapper hook={() => useUrlStateHooks(mockProps)} />);

wait(() => {
// @ts-ignore property mock does not exists
expect(defaultProps.setRelativeTimerange.mock.calls[0][0]).toEqual({
from: 1558591200000,
fromStr: 'now-1d/d',
kind: 'relative',
to: 1558677599999,
toStr: 'now-1d/d',
id: 'global',
});
// @ts-ignore property mock does not exists
expect(defaultProps.setRelativeTimerange.mock.calls[1][0]).toEqual({
from: 1558732849370,
fromStr: 'now-15m',
kind: 'relative',
to: 1558733749370,
toStr: 'now',
id: 'timeline',
});
});
});
});
describe('absolute timerange actions are called with correct data on component mount', () => {
test.each(testCases)('%o', (page, namespaceLower, namespaceUpper, examplePath, type) => {
mockProps = getMockPropsObj({ page, examplePath, namespaceLower, type })
.absoluteTimeSearch.undefinedQuery;
shallow(<UrlStateContainerLifecycle {...mockProps} />);
// @ts-ignore property mock does not exists
expect(defaultProps.setAbsoluteTimerange.mock.calls[0][0]).toEqual({
from: 1556736012685,
kind: 'absolute',
to: 1556822416082,
id: 'global',
});
// @ts-ignore property mock does not exists
expect(defaultProps.setAbsoluteTimerange.mock.calls[1][0]).toEqual({
from: 1556736012685,
kind: 'absolute',
to: 1556822416082,
id: 'timeline',
mount(<HookWrapper hook={() => useUrlStateHooks(mockProps)} />);

wait(() => {
// @ts-ignore property mock does not exists
expect(defaultProps.setAbsoluteTimerange.mock.calls[0][0]).toEqual({
from: 1556736012685,
kind: 'absolute',
to: 1556822416082,
id: 'global',
});
// @ts-ignore property mock does not exists
expect(defaultProps.setAbsoluteTimerange.mock.calls[1][0]).toEqual({
from: 1556736012685,
kind: 'absolute',
to: 1556822416082,
id: 'timeline',
});
});
});
});
describe('kqlQuery action is called with correct data on component mount', () => {
test.each(testCases)(' %o', (page, namespaceLower, namespaceUpper, examplePath, type) => {
mockProps = getMockPropsObj({ page, examplePath, namespaceLower, type })
.relativeTimeSearch.undefinedQuery;
shallow(<UrlStateContainerLifecycle {...mockProps} />);
const functionName =
namespaceUpper === 'Network' ? defaultProps.setNetworkKql : defaultProps.setHostsKql;
// @ts-ignore property mock does not exists
expect(functionName.mock.calls[0][0]).toEqual({
filterQuery: serializedFilterQuery,
[`${namespaceLower}Type`]: type,
mount(<HookWrapper hook={() => useUrlStateHooks(mockProps)} />);
wait(() => {
const functionName =
namespaceUpper === 'Network' ? defaultProps.setNetworkKql : defaultProps.setHostsKql;
// @ts-ignore property mock does not exists
expect(functionName.mock.calls[0][0]).toEqual({
filterQuery: serializedFilterQuery,
[`${namespaceLower}Type`]: type,
});
});
});
});
Expand All @@ -130,11 +149,15 @@ describe('UrlStateContainer', () => {
async (page, namespaceLower, namespaceUpper, examplePath, type) => {
mockProps = getMockPropsObj({ page, examplePath, namespaceLower, type })
.oppositeQueryLocationSearch.undefinedQuery;
shallow(<UrlStateContainerLifecycle {...mockProps} />);
const functionName =
namespaceUpper === 'Network' ? defaultProps.setNetworkKql : defaultProps.setHostsKql;
// @ts-ignore property mock does not exists
expect(functionName.mock.calls.length).toEqual(0);
mount(<HookWrapper hook={() => useUrlStateHooks(mockProps)} />);
wait(() => {
const functionName =
namespaceUpper === 'Network'
? defaultProps.setNetworkKql
: defaultProps.setHostsKql;
// @ts-ignore property mock does not exists
expect(functionName.mock.calls.length).toEqual(0);
});
}
);
});
Expand All @@ -147,18 +170,76 @@ describe('UrlStateContainer', () => {
async (page, namespaceLower, namespaceUpper, examplePath, type) => {
mockProps = getMockPropsObj({ page, examplePath, namespaceLower, type }).noSearch
.definedQuery;
shallow(<UrlStateContainerLifecycle {...mockProps} />);
mount(<HookWrapper hook={() => useUrlStateHooks(mockProps)} />);

// @ts-ignore property mock does not exists
expect(mockHistory.replace.mock.calls[0][0]).toEqual({
hash: '',
pathname: examplePath,
search: `?_g=()&kqlQuery=(filterQuery:(expression:'host.name:%22siem-es%22',kind:kuery),queryLocation:${page},type:${type})`,
state: '',
wait(() => {
// @ts-ignore property mock does not exists
expect(mockHistory.replace.mock.calls[0][0]).toEqual({
hash: '',
pathname: examplePath,
search: `?_g=()&kqlQuery=(filterQuery:(expression:'host.name:%22siem-es%22',kind:kuery),queryLocation:${page},type:${type})`,
state: '',
});
});
}
);
});
});
});

describe('initializeLocation', () => {
test('basic functionality with no search', () => {
Object.defineProperty(globalNode.window, 'location', {
value: {
href: 'http://localhost:5601/app/siem#/hosts?_g=()',
},
writable: true,
});
const location: Location = {
hash: '',
pathname: '/hosts',
search: '?_g=()',
state: null,
};
expect(initializeLocation(location).search).toEqual('?_g=()');
});
test('basic functionality with search', () => {
Object.defineProperty(globalNode.window, 'location', {
value: {
href:
"http://localhost:5601/app/siem#/hosts?_g=()&kqlQuery=(filterQuery:(expression:'%20host.name:%20%22beats-ci-immutable-ubuntu-1604-1560801145745062645%22%20and%20process.name:*',kind:kuery),queryLocation:hosts.page,type:page)&timerange=(global:(linkTo:!(timeline),timerange:(from:1560714985274,fromStr:now-24h,kind:relative,to:1560801385274,toStr:now)),timeline:(linkTo:!(global),timerange:(from:1560714985274,fromStr:now-24h,kind:relative,to:1560801385274,toStr:now)))",
},
writable: true,
});
const location: Location = {
hash: '',
pathname: '/hosts',
search:
"?_g=()&kqlQuery=(filterQuery:(expression:'%2Bhost.name:%2B%22beats-ci-immutable-ubuntu-1604-1560801145745062645%22%2Band%2Bprocess.name:*',kind:kuery),queryLocation:hosts.page,type:page)&timerange=(global:(linkTo:!(timeline),timerange:(from:1560714985274,fromStr:now-24h,kind:relative,to:1560801385274,toStr:now)),timeline:(linkTo:!(global),timerange:(from:1560714985274,fromStr:now-24h,kind:relative,to:1560801385274,toStr:now)))",
state: null,
};
expect(initializeLocation(location).search).toEqual(
"?_g=()&kqlQuery=(filterQuery:(expression:'%20host.name:%20%22beats-ci-immutable-ubuntu-1604-1560801145745062645%22%20and%20process.name:*',kind:kuery),queryLocation:hosts.page,type:page)&timerange=(global:(linkTo:!(timeline),timerange:(from:1560714985274,fromStr:now-24h,kind:relative,to:1560801385274,toStr:now)),timeline:(linkTo:!(global),timerange:(from:1560714985274,fromStr:now-24h,kind:relative,to:1560801385274,toStr:now)))"
);
});
test('If hash and pathname do not match href from the hash, do not do anything', () => {
Object.defineProperty(globalNode.window, 'location', {
value: {
href:
"http://localhost:5601/app/siem#/hosts?_g=()&kqlQuery=(filterQuery:(expression:'%20host.name:%20%22beats-ci-immutable-ubuntu-1604-1560801145745062645%22%20and%20process.name:*',kind:kuery),queryLocation:hosts.page,type:page)&timerange=(global:(linkTo:!(timeline),timerange:(from:1560714985274,fromStr:now-24h,kind:relative,to:1560801385274,toStr:now)),timeline:(linkTo:!(global),timerange:(from:1560714985274,fromStr:now-24h,kind:relative,to:1560801385274,toStr:now)))",
},
writable: true,
});
const location: Location = {
hash: '',
pathname: '/network',
search:
"?_g=()&kqlQuery=(filterQuery:(expression:'%2Bhost.name:%2B%22beats-ci-immutable-ubuntu-1604-1560801145745062645%22%2Band%2Bprocess.name:*',kind:kuery),queryLocation:hosts.page,type:page)&timerange=(global:(linkTo:!(timeline),timerange:(from:1560714985274,fromStr:now-24h,kind:relative,to:1560801385274,toStr:now)),timeline:(linkTo:!(global),timerange:(from:1560714985274,fromStr:now-24h,kind:relative,to:1560801385274,toStr:now)))",
state: null,
};
expect(initializeLocation(location).search).toEqual(
"?_g=()&kqlQuery=(filterQuery:(expression:'%2Bhost.name:%2B%22beats-ci-immutable-ubuntu-1604-1560801145745062645%22%2Band%2Bprocess.name:*',kind:kuery),queryLocation:hosts.page,type:page)&timerange=(global:(linkTo:!(timeline),timerange:(from:1560714985274,fromStr:now-24h,kind:relative,to:1560801385274,toStr:now)),timeline:(linkTo:!(global),timerange:(from:1560714985274,fromStr:now-24h,kind:relative,to:1560801385274,toStr:now)))"
);
});
});
});
Loading

0 comments on commit af21c66

Please sign in to comment.