-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Security solution][Endpoint] Add unit tests for fleet event filters/trusted apps cards #101034
Changes from all commits
f6934d2
3864d72
64e9d65
06a8f0c
41c0bd7
0ad0902
662c59b
aca55cd
84d46a4
e42bf61
9f6b620
5b17fd5
39c8133
b0d77b2
9324dc3
0cc6766
29014e8
8993d4e
31538c7
bed96f6
940457d
9086f2f
eaae47d
a13e57e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { ThemeProvider } from 'styled-components'; | ||
import { I18nProvider } from '@kbn/i18n/react'; | ||
import { ExceptionItemsSummary } from './exception_items_summary'; | ||
import * as reactTestingLibrary from '@testing-library/react'; | ||
import { getMockTheme } from '../../../../../../../../public/common/lib/kibana/kibana_react.mock'; | ||
import { GetExceptionSummaryResponse } from '../../../../../../../../common/endpoint/types'; | ||
|
||
const mockTheme = getMockTheme({ | ||
eui: { | ||
paddingSizes: { m: '2' }, | ||
}, | ||
}); | ||
|
||
const getStatValue = (el: reactTestingLibrary.RenderResult, stat: string) => { | ||
return el.getByText(stat)!.nextSibling?.lastChild?.textContent; | ||
}; | ||
|
||
describe('Fleet event filters card', () => { | ||
const renderComponent: ( | ||
stats: GetExceptionSummaryResponse | ||
) => reactTestingLibrary.RenderResult = (stats) => { | ||
const Wrapper: React.FC = ({ children }) => ( | ||
<I18nProvider> | ||
<ThemeProvider theme={mockTheme}>{children}</ThemeProvider> | ||
</I18nProvider> | ||
); | ||
const component = reactTestingLibrary.render(<ExceptionItemsSummary stats={stats} />, { | ||
wrapper: Wrapper, | ||
}); | ||
return component; | ||
}; | ||
it('should renders correctly', () => { | ||
const summary: GetExceptionSummaryResponse = { | ||
windows: 3, | ||
linux: 2, | ||
macos: 2, | ||
total: 7, | ||
}; | ||
const component = renderComponent(summary); | ||
|
||
expect(component.getByText('Windows')).not.toBeNull(); | ||
expect(getStatValue(component, 'Windows')).toEqual(summary.windows.toString()); | ||
|
||
expect(component.getByText('Linux')).not.toBeNull(); | ||
expect(getStatValue(component, 'Linux')).toEqual(summary.linux.toString()); | ||
|
||
expect(component.getByText('Mac')).not.toBeNull(); | ||
expect(getStatValue(component, 'Mac')).toEqual(summary.macos.toString()); | ||
|
||
expect(component.getByText('Total')).not.toBeNull(); | ||
expect(getStatValue(component, 'Total')).toEqual(summary.total.toString()); | ||
}); | ||
it('should renders correctly when missing some stats', () => { | ||
const summary: Partial<GetExceptionSummaryResponse> = { | ||
windows: 3, | ||
total: 3, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need a test case to check if the total is missing? is that a possible case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's possible but in the component each item is parsed in the same way, so if some key is missing, a 0 will be displayed instead |
||
}; | ||
const component = renderComponent(summary as GetExceptionSummaryResponse); | ||
|
||
expect(component.getByText('Windows')).not.toBeNull(); | ||
expect(getStatValue(component, 'Windows')).toEqual('3'); | ||
|
||
expect(component.getByText('Linux')).not.toBeNull(); | ||
expect(getStatValue(component, 'Linux')).toEqual('0'); | ||
|
||
expect(component.getByText('Mac')).not.toBeNull(); | ||
expect(getStatValue(component, 'Mac')).toEqual('0'); | ||
|
||
expect(component.getByText('Total')).not.toBeNull(); | ||
expect(getStatValue(component, 'Total')).toEqual('3'); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { ThemeProvider } from 'styled-components'; | ||
import { I18nProvider } from '@kbn/i18n/react'; | ||
import { FleetEventFiltersCard } from './fleet_event_filters_card'; | ||
import * as reactTestingLibrary from '@testing-library/react'; | ||
import { EventFiltersHttpService } from '../../../../../event_filters/service'; | ||
import { useToasts } from '../../../../../../../common/lib/kibana'; | ||
import { getMockTheme } from '../../../../../../../../public/common/lib/kibana/kibana_react.mock'; | ||
import { GetExceptionSummaryResponse } from '../../../../../../../../common/endpoint/types'; | ||
|
||
jest.mock('./exception_items_summary'); | ||
jest.mock('../../../../../event_filters/service'); | ||
|
||
jest.mock('../../../../../../../../../../../src/plugins/kibana_react/public', () => { | ||
const originalModule = jest.requireActual( | ||
'../../../../../../../../../../../src/plugins/kibana_react/public' | ||
); | ||
const useKibana = jest.fn().mockImplementation(() => ({ | ||
services: { | ||
http: {}, | ||
data: {}, | ||
notifications: {}, | ||
application: { | ||
getUrlForApp: jest.fn(), | ||
}, | ||
}, | ||
})); | ||
|
||
return { | ||
...originalModule, | ||
useKibana, | ||
}; | ||
}); | ||
|
||
jest.mock('../../../../../../../common/lib/kibana'); | ||
|
||
const mockTheme = getMockTheme({ | ||
eui: { | ||
paddingSizes: { m: '2' }, | ||
}, | ||
}); | ||
|
||
const EventFiltersHttpServiceMock = EventFiltersHttpService as jest.Mock; | ||
const useToastsMock = useToasts as jest.Mock; | ||
|
||
const summary: GetExceptionSummaryResponse = { | ||
windows: 3, | ||
linux: 2, | ||
macos: 2, | ||
total: 7, | ||
}; | ||
|
||
describe('Fleet event filters card', () => { | ||
let promise: Promise<GetExceptionSummaryResponse>; | ||
let addDanger: jest.Mock = jest.fn(); | ||
const renderComponent: () => Promise<reactTestingLibrary.RenderResult> = async () => { | ||
const Wrapper: React.FC = ({ children }) => ( | ||
<I18nProvider> | ||
<ThemeProvider theme={mockTheme}>{children}</ThemeProvider> | ||
</I18nProvider> | ||
); | ||
// @ts-ignore | ||
const component = reactTestingLibrary.render(<FleetEventFiltersCard />, { wrapper: Wrapper }); | ||
try { | ||
// @ts-ignore | ||
await reactTestingLibrary.act(() => promise); | ||
} catch (err) { | ||
return component; | ||
} | ||
return component; | ||
}; | ||
beforeAll(() => { | ||
useToastsMock.mockImplementation(() => { | ||
return { | ||
addDanger, | ||
}; | ||
}); | ||
}); | ||
beforeEach(() => { | ||
promise = Promise.resolve(summary); | ||
addDanger = jest.fn(); | ||
}); | ||
afterEach(() => { | ||
EventFiltersHttpServiceMock.mockReset(); | ||
}); | ||
it('should render correctly', async () => { | ||
EventFiltersHttpServiceMock.mockImplementationOnce(() => { | ||
return { | ||
getSummary: () => jest.fn(() => promise), | ||
}; | ||
}); | ||
const component = await renderComponent(); | ||
expect(component.getByText('Event Filters')).not.toBeNull(); | ||
expect(component.getByText('Manage event filters')).not.toBeNull(); | ||
}); | ||
it('should render an error toast when api call fails', async () => { | ||
expect(addDanger).toBeCalledTimes(0); | ||
promise = Promise.reject(new Error('error test')); | ||
EventFiltersHttpServiceMock.mockImplementationOnce(() => { | ||
return { | ||
getSummary: () => promise, | ||
}; | ||
}); | ||
const component = await renderComponent(); | ||
expect(component.getByText('Event Filters')).not.toBeNull(); | ||
expect(component.getByText('Manage event filters')).not.toBeNull(); | ||
await reactTestingLibrary.waitFor(() => expect(addDanger).toBeCalledTimes(1)); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { ThemeProvider } from 'styled-components'; | ||
import { I18nProvider } from '@kbn/i18n/react'; | ||
import { FleetTrustedAppsCard } from './fleet_trusted_apps_card'; | ||
import * as reactTestingLibrary from '@testing-library/react'; | ||
import { TrustedAppsHttpService } from '../../../../../trusted_apps/service'; | ||
import { useToasts } from '../../../../../../../common/lib/kibana'; | ||
import { getMockTheme } from '../../../../../../../../public/common/lib/kibana/kibana_react.mock'; | ||
import { GetExceptionSummaryResponse } from '../../../../../../../../common/endpoint/types'; | ||
|
||
jest.mock('./exception_items_summary'); | ||
jest.mock('../../../../../trusted_apps/service'); | ||
|
||
jest.mock('../../../../../../../../../../../src/plugins/kibana_react/public', () => { | ||
const originalModule = jest.requireActual( | ||
'../../../../../../../../../../../src/plugins/kibana_react/public' | ||
); | ||
const useKibana = jest.fn().mockImplementation(() => ({ | ||
services: { | ||
http: {}, | ||
data: {}, | ||
notifications: {}, | ||
application: { | ||
getUrlForApp: jest.fn(), | ||
}, | ||
}, | ||
})); | ||
|
||
return { | ||
...originalModule, | ||
useKibana, | ||
}; | ||
}); | ||
|
||
jest.mock('../../../../../../../common/lib/kibana'); | ||
|
||
const mockTheme = getMockTheme({ | ||
eui: { | ||
paddingSizes: { m: '2' }, | ||
}, | ||
}); | ||
|
||
const TrustedAppsHttpServiceMock = TrustedAppsHttpService as jest.Mock; | ||
const useToastsMock = useToasts as jest.Mock; | ||
|
||
const summary: GetExceptionSummaryResponse = { | ||
windows: 3, | ||
linux: 2, | ||
macos: 2, | ||
total: 7, | ||
}; | ||
|
||
describe('Fleet trusted apps card', () => { | ||
let promise: Promise<GetExceptionSummaryResponse>; | ||
let addDanger: jest.Mock = jest.fn(); | ||
const renderComponent: () => Promise<reactTestingLibrary.RenderResult> = async () => { | ||
const Wrapper: React.FC = ({ children }) => ( | ||
<I18nProvider> | ||
<ThemeProvider theme={mockTheme}>{children}</ThemeProvider> | ||
</I18nProvider> | ||
); | ||
// @ts-ignore | ||
const component = reactTestingLibrary.render(<FleetTrustedAppsCard />, { wrapper: Wrapper }); | ||
try { | ||
// @ts-ignore | ||
await reactTestingLibrary.act(() => promise); | ||
} catch (err) { | ||
return component; | ||
} | ||
return component; | ||
}; | ||
|
||
beforeAll(() => { | ||
useToastsMock.mockImplementation(() => { | ||
return { | ||
addDanger, | ||
}; | ||
}); | ||
}); | ||
beforeEach(() => { | ||
promise = Promise.resolve(summary); | ||
addDanger = jest.fn(); | ||
}); | ||
afterEach(() => { | ||
TrustedAppsHttpServiceMock.mockReset(); | ||
}); | ||
it('should render correctly', async () => { | ||
TrustedAppsHttpServiceMock.mockImplementationOnce(() => { | ||
return { | ||
getTrustedAppsSummary: () => jest.fn(() => promise), | ||
}; | ||
}); | ||
const component = await renderComponent(); | ||
expect(component.getByText('Trusted Applications')).not.toBeNull(); | ||
expect(component.getByText('Manage trusted applications')).not.toBeNull(); | ||
}); | ||
it('should render an error toast when api call fails', async () => { | ||
expect(addDanger).toBeCalledTimes(0); | ||
promise = Promise.reject(new Error('error test')); | ||
TrustedAppsHttpServiceMock.mockImplementationOnce(() => { | ||
return { | ||
getTrustedAppsSummary: () => promise, | ||
}; | ||
}); | ||
const component = await renderComponent(); | ||
expect(component.getByText('Trusted Applications')).not.toBeNull(); | ||
expect(component.getByText('Manage trusted applications')).not.toBeNull(); | ||
await reactTestingLibrary.waitFor(() => expect(addDanger).toBeCalledTimes(1)); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick:
...render...