Commit 1ee4e65f authored by Ezekiel Kigbo's avatar Ezekiel Kigbo

Merge branch 'improve-threat-monitoring-section-tests' into 'master'

Refactor threat monitoring section tests

See merge request gitlab-org/gitlab!63901
parents 2dbb5047 94db3338
......@@ -120,19 +120,19 @@ export default {
<template>
<div class="my-3">
<h4 ref="chartTitle" class="h4">{{ title }}</h4>
<h4 data-testid="chartTitle" class="h4">{{ title }}</h4>
<loading-skeleton v-if="isLoading" class="mt-3" />
<template v-else-if="hasHistory">
<h5 ref="chartSubtitle" class="h5">{{ subtitle }}</h5>
<h5 data-testid="chartSubtitle" class="h5">{{ subtitle }}</h5>
<statistics-summary class="mt-3" :data="summary" />
<statistics-history class="mt-3" :data="chart" :y-legend="yLegend" />
</template>
<gl-empty-state
v-else
ref="chartEmptyState"
data-testid="chartEmptyState"
:title="chartEmptyStateTitle"
:description="chartEmptyStateText"
:svg-path="chartEmptyStateSvgPath"
......
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ThreatMonitoringSection component given the statistics are loading shows the loading skeleton 1`] = `
<loading-skeleton-stub
class="mt-3"
/>
`;
exports[`ThreatMonitoringSection component given there is a default environment with no data to display shows the chart empty state 1`] = `
<gl-empty-state-stub
compact="true"
description="Empty Text"
primarybuttonlink="documentation_path#anchor"
primarybuttontext="Learn more"
svgpath="svg_path"
title="Empty Title"
/>
`;
import { shallowMount } from '@vue/test-utils';
import LoadingSkeleton from 'ee/threat_monitoring/components/loading_skeleton.vue';
import StatisticsHistory from 'ee/threat_monitoring/components/statistics_history.vue';
import StatisticsSummary from 'ee/threat_monitoring/components/statistics_summary.vue';
import ThreatMonitoringSection from 'ee/threat_monitoring/components/threat_monitoring_section.vue';
import createStore from 'ee/threat_monitoring/store';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import { mockNominalHistory, mockAnomalousHistory } from '../mocks/mock_data';
......@@ -11,6 +11,8 @@ describe('ThreatMonitoringSection component', () => {
let store;
let wrapper;
const title = 'Test Title';
const timeRange = {
from: new Date(Date.UTC(2020, 2, 6)).toISOString(),
to: new Date(Date.UTC(2020, 2, 13)).toISOString(),
......@@ -34,10 +36,10 @@ describe('ThreatMonitoringSection component', () => {
jest.spyOn(store, 'dispatch').mockImplementation(() => Promise.resolve());
wrapper = shallowMount(ThreatMonitoringSection, {
wrapper = shallowMountExtended(ThreatMonitoringSection, {
propsData: {
storeNamespace: 'threatMonitoringNetworkPolicy',
title: 'Container Network Policy',
title,
subtitle: 'Requests',
nominalTitle: 'Total Requests',
anomalousTitle: 'Anomalous Requests',
......@@ -53,81 +55,84 @@ describe('ThreatMonitoringSection component', () => {
});
};
const findLoadingSkeleton = () => wrapper.find(LoadingSkeleton);
const findStatisticsHistory = () => wrapper.find(StatisticsHistory);
const findStatisticsSummary = () => wrapper.find(StatisticsSummary);
const findChartEmptyState = () => wrapper.find({ ref: 'chartEmptyState' });
const findChartTitle = () => wrapper.find({ ref: 'chartTitle' });
const findChartSubtitle = () => wrapper.find({ ref: 'chartSubtitle' });
beforeEach(() => {
factory({});
});
const findLoadingSkeleton = () => wrapper.findComponent(LoadingSkeleton);
const findStatisticsHistory = () => wrapper.findComponent(StatisticsHistory);
const findStatisticsSummary = () => wrapper.findComponent(StatisticsSummary);
const findChartEmptyState = () => wrapper.findByTestId('chartEmptyState');
const findChartTitle = () => wrapper.findByTestId('chartTitle');
const findChartSubtitle = () => wrapper.findByTestId('chartSubtitle');
afterEach(() => {
wrapper.destroy();
});
it('does not show the loading skeleton', () => {
expect(findLoadingSkeleton().exists()).toBe(false);
});
it('sets data to the summary', () => {
const summary = findStatisticsSummary();
expect(summary.exists()).toBe(true);
expect(summary.props('data')).toStrictEqual({
anomalous: {
title: 'Anomalous Requests',
value: 0.2,
},
nominal: {
title: 'Total Requests',
value: 100,
},
describe('given there is data to display', () => {
beforeEach(() => {
factory({});
});
});
it('sets data to the chart', () => {
const chart = findStatisticsHistory();
expect(chart.exists()).toBe(true);
expect(chart.props('data')).toStrictEqual({
anomalous: { title: 'Anomalous Requests', values: mockAnomalousHistory },
nominal: { title: 'Total Requests', values: mockNominalHistory },
...timeRange,
it('shows the chart title', () => {
const chartTitle = findChartTitle();
expect(chartTitle.exists()).toBe(true);
expect(chartTitle.text()).toBe(title);
});
it.each`
component | status | findComponent | state
${'loading skeleton'} | ${'does not display'} | ${findLoadingSkeleton} | ${false}
${'chart subtitle'} | ${'does display'} | ${findChartSubtitle} | ${true}
${'statistics summary'} | ${'does display'} | ${findStatisticsSummary} | ${true}
${'statistics history'} | ${'does display'} | ${findStatisticsHistory} | ${true}
${'chart empty state'} | ${'does not display'} | ${findChartEmptyState} | ${false}
`('$status the $component', async ({ findComponent, state }) => {
expect(findComponent().exists()).toBe(state);
});
it('sets data to the summary', () => {
const summary = findStatisticsSummary();
expect(summary.exists()).toBe(true);
expect(summary.props('data')).toStrictEqual({
anomalous: {
title: 'Anomalous Requests',
value: 0.2,
},
nominal: {
title: 'Total Requests',
value: 100,
},
});
});
expect(chart.props('yLegend')).toEqual('Requests');
});
it('shows the chart title', () => {
expect(findChartTitle().exists()).toBe(true);
});
it('sets data to the chart', () => {
const chart = findStatisticsHistory();
expect(chart.exists()).toBe(true);
it('shows the chart subtitle', () => {
expect(findChartSubtitle().exists()).toBe(true);
});
it('does not show the chart empty state', () => {
expect(findChartEmptyState().exists()).toBe(false);
});
expect(chart.props('data')).toStrictEqual({
anomalous: { title: 'Anomalous Requests', values: mockAnomalousHistory },
nominal: { title: 'Total Requests', values: mockNominalHistory },
...timeRange,
});
expect(chart.props('yLegend')).toEqual('Requests');
});
it('fetches statistics', () => {
expect(store.dispatch).toHaveBeenCalledWith('threatMonitoringNetworkPolicy/fetchStatistics');
});
it('fetches statistics', () => {
expect(store.dispatch).toHaveBeenCalledWith('threatMonitoringNetworkPolicy/fetchStatistics');
});
it('fetches statistics on environment change', async () => {
store.dispatch.mockReset();
await store.commit('threatMonitoring/SET_CURRENT_ENVIRONMENT_ID', 2);
it('fetches statistics on environment change', async () => {
store.dispatch.mockReset();
await store.commit('threatMonitoring/SET_CURRENT_ENVIRONMENT_ID', 2);
expect(store.dispatch).toHaveBeenCalledWith('threatMonitoringNetworkPolicy/fetchStatistics');
});
expect(store.dispatch).toHaveBeenCalledWith('threatMonitoringNetworkPolicy/fetchStatistics');
});
it('fetches statistics on time window change', async () => {
store.dispatch.mockReset();
await store.commit('threatMonitoring/SET_CURRENT_TIME_WINDOW', 'hour');
it('fetches statistics on time window change', async () => {
store.dispatch.mockReset();
await store.commit('threatMonitoring/SET_CURRENT_TIME_WINDOW', 'hour');
expect(store.dispatch).toHaveBeenCalledWith('threatMonitoringNetworkPolicy/fetchStatistics');
expect(store.dispatch).toHaveBeenCalledWith('threatMonitoringNetworkPolicy/fetchStatistics');
});
});
describe('given the statistics are loading', () => {
......@@ -137,25 +142,15 @@ describe('ThreatMonitoringSection component', () => {
});
});
it('shows the loading skeleton', () => {
expect(findLoadingSkeleton().element).toMatchSnapshot();
});
it('does not show the summary or history statistics', () => {
expect(findStatisticsSummary().exists()).toBe(false);
expect(findStatisticsHistory().exists()).toBe(false);
});
it('shows the chart title', () => {
expect(findChartTitle().exists()).toBe(true);
});
it('does not show the chart subtitle', () => {
expect(findChartSubtitle().exists()).toBe(false);
});
it('does not show the chart empty state', () => {
expect(findChartEmptyState().exists()).toBe(false);
it.each`
component | status | findComponent | state
${'loading skeleton'} | ${'does display'} | ${findLoadingSkeleton} | ${true}
${'chart subtitle'} | ${'does not display'} | ${findChartSubtitle} | ${false}
${'statistics summary'} | ${'does not display'} | ${findStatisticsSummary} | ${false}
${'statistics history'} | ${'does not display'} | ${findStatisticsHistory} | ${false}
${'chart empty state'} | ${'does not display'} | ${findChartEmptyState} | ${false}
`('$status the $component', async ({ findComponent, state }) => {
expect(findComponent().exists()).toBe(state);
});
});
......@@ -172,25 +167,15 @@ describe('ThreatMonitoringSection component', () => {
});
});
it('does not show the loading skeleton', () => {
expect(findLoadingSkeleton().exists()).toBe(false);
});
it('does not show the summary or history statistics', () => {
expect(findStatisticsSummary().exists()).toBe(false);
expect(findStatisticsHistory().exists()).toBe(false);
});
it('shows the chart title', () => {
expect(findChartTitle().exists()).toBe(true);
});
it('does not show the chart subtitle', () => {
expect(findChartSubtitle().exists()).toBe(false);
});
it('shows the chart empty state', () => {
expect(findChartEmptyState().element).toMatchSnapshot();
it.each`
component | status | findComponent | state
${'loading skeleton'} | ${'does not display'} | ${findLoadingSkeleton} | ${false}
${'chart subtitle'} | ${'does not display'} | ${findChartSubtitle} | ${false}
${'statistics summary'} | ${'does not display'} | ${findStatisticsSummary} | ${false}
${'statistics history'} | ${'does not display'} | ${findStatisticsHistory} | ${false}
${'chart empty state'} | ${'does not display'} | ${findChartEmptyState} | ${true}
`('$status the $component', async ({ findComponent, state }) => {
expect(findComponent().exists()).toBe(state);
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment