Commit 94db3338 authored by Alexander Turinske's avatar Alexander Turinske

Refactor threat monitoring section tests

- make them more readable and organized
- remove useless snapshot tests
parent 97d26284
...@@ -120,19 +120,19 @@ export default { ...@@ -120,19 +120,19 @@ export default {
<template> <template>
<div class="my-3"> <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" /> <loading-skeleton v-if="isLoading" class="mt-3" />
<template v-else-if="hasHistory"> <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-summary class="mt-3" :data="summary" />
<statistics-history class="mt-3" :data="chart" :y-legend="yLegend" /> <statistics-history class="mt-3" :data="chart" :y-legend="yLegend" />
</template> </template>
<gl-empty-state <gl-empty-state
v-else v-else
ref="chartEmptyState" data-testid="chartEmptyState"
:title="chartEmptyStateTitle" :title="chartEmptyStateTitle"
:description="chartEmptyStateText" :description="chartEmptyStateText"
:svg-path="chartEmptyStateSvgPath" :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 LoadingSkeleton from 'ee/threat_monitoring/components/loading_skeleton.vue';
import StatisticsHistory from 'ee/threat_monitoring/components/statistics_history.vue'; import StatisticsHistory from 'ee/threat_monitoring/components/statistics_history.vue';
import StatisticsSummary from 'ee/threat_monitoring/components/statistics_summary.vue'; import StatisticsSummary from 'ee/threat_monitoring/components/statistics_summary.vue';
import ThreatMonitoringSection from 'ee/threat_monitoring/components/threat_monitoring_section.vue'; import ThreatMonitoringSection from 'ee/threat_monitoring/components/threat_monitoring_section.vue';
import createStore from 'ee/threat_monitoring/store'; import createStore from 'ee/threat_monitoring/store';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import { mockNominalHistory, mockAnomalousHistory } from '../mocks/mock_data'; import { mockNominalHistory, mockAnomalousHistory } from '../mocks/mock_data';
...@@ -11,6 +11,8 @@ describe('ThreatMonitoringSection component', () => { ...@@ -11,6 +11,8 @@ describe('ThreatMonitoringSection component', () => {
let store; let store;
let wrapper; let wrapper;
const title = 'Test Title';
const timeRange = { const timeRange = {
from: new Date(Date.UTC(2020, 2, 6)).toISOString(), from: new Date(Date.UTC(2020, 2, 6)).toISOString(),
to: new Date(Date.UTC(2020, 2, 13)).toISOString(), to: new Date(Date.UTC(2020, 2, 13)).toISOString(),
...@@ -34,10 +36,10 @@ describe('ThreatMonitoringSection component', () => { ...@@ -34,10 +36,10 @@ describe('ThreatMonitoringSection component', () => {
jest.spyOn(store, 'dispatch').mockImplementation(() => Promise.resolve()); jest.spyOn(store, 'dispatch').mockImplementation(() => Promise.resolve());
wrapper = shallowMount(ThreatMonitoringSection, { wrapper = shallowMountExtended(ThreatMonitoringSection, {
propsData: { propsData: {
storeNamespace: 'threatMonitoringNetworkPolicy', storeNamespace: 'threatMonitoringNetworkPolicy',
title: 'Container Network Policy', title,
subtitle: 'Requests', subtitle: 'Requests',
nominalTitle: 'Total Requests', nominalTitle: 'Total Requests',
anomalousTitle: 'Anomalous Requests', anomalousTitle: 'Anomalous Requests',
...@@ -53,81 +55,84 @@ describe('ThreatMonitoringSection component', () => { ...@@ -53,81 +55,84 @@ describe('ThreatMonitoringSection component', () => {
}); });
}; };
const findLoadingSkeleton = () => wrapper.find(LoadingSkeleton); const findLoadingSkeleton = () => wrapper.findComponent(LoadingSkeleton);
const findStatisticsHistory = () => wrapper.find(StatisticsHistory); const findStatisticsHistory = () => wrapper.findComponent(StatisticsHistory);
const findStatisticsSummary = () => wrapper.find(StatisticsSummary); const findStatisticsSummary = () => wrapper.findComponent(StatisticsSummary);
const findChartEmptyState = () => wrapper.find({ ref: 'chartEmptyState' }); const findChartEmptyState = () => wrapper.findByTestId('chartEmptyState');
const findChartTitle = () => wrapper.find({ ref: 'chartTitle' }); const findChartTitle = () => wrapper.findByTestId('chartTitle');
const findChartSubtitle = () => wrapper.find({ ref: 'chartSubtitle' }); const findChartSubtitle = () => wrapper.findByTestId('chartSubtitle');
beforeEach(() => {
factory({});
});
afterEach(() => { afterEach(() => {
wrapper.destroy(); wrapper.destroy();
}); });
it('does not show the loading skeleton', () => { describe('given there is data to display', () => {
expect(findLoadingSkeleton().exists()).toBe(false); beforeEach(() => {
}); factory({});
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,
},
}); });
});
it('sets data to the chart', () => {
const chart = findStatisticsHistory();
expect(chart.exists()).toBe(true);
expect(chart.props('data')).toStrictEqual({ it('shows the chart title', () => {
anomalous: { title: 'Anomalous Requests', values: mockAnomalousHistory }, const chartTitle = findChartTitle();
nominal: { title: 'Total Requests', values: mockNominalHistory }, expect(chartTitle.exists()).toBe(true);
...timeRange, 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', () => { it('sets data to the chart', () => {
expect(findChartTitle().exists()).toBe(true); const chart = findStatisticsHistory();
}); expect(chart.exists()).toBe(true);
it('shows the chart subtitle', () => { expect(chart.props('data')).toStrictEqual({
expect(findChartSubtitle().exists()).toBe(true); anomalous: { title: 'Anomalous Requests', values: mockAnomalousHistory },
}); nominal: { title: 'Total Requests', values: mockNominalHistory },
...timeRange,
it('does not show the chart empty state', () => { });
expect(findChartEmptyState().exists()).toBe(false); expect(chart.props('yLegend')).toEqual('Requests');
}); });
it('fetches statistics', () => { it('fetches statistics', () => {
expect(store.dispatch).toHaveBeenCalledWith('threatMonitoringNetworkPolicy/fetchStatistics'); expect(store.dispatch).toHaveBeenCalledWith('threatMonitoringNetworkPolicy/fetchStatistics');
}); });
it('fetches statistics on environment change', async () => { it('fetches statistics on environment change', async () => {
store.dispatch.mockReset(); store.dispatch.mockReset();
await store.commit('threatMonitoring/SET_CURRENT_ENVIRONMENT_ID', 2); 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 () => { it('fetches statistics on time window change', async () => {
store.dispatch.mockReset(); store.dispatch.mockReset();
await store.commit('threatMonitoring/SET_CURRENT_TIME_WINDOW', 'hour'); 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', () => { describe('given the statistics are loading', () => {
...@@ -137,25 +142,15 @@ describe('ThreatMonitoringSection component', () => { ...@@ -137,25 +142,15 @@ describe('ThreatMonitoringSection component', () => {
}); });
}); });
it('shows the loading skeleton', () => { it.each`
expect(findLoadingSkeleton().element).toMatchSnapshot(); component | status | findComponent | state
}); ${'loading skeleton'} | ${'does display'} | ${findLoadingSkeleton} | ${true}
${'chart subtitle'} | ${'does not display'} | ${findChartSubtitle} | ${false}
it('does not show the summary or history statistics', () => { ${'statistics summary'} | ${'does not display'} | ${findStatisticsSummary} | ${false}
expect(findStatisticsSummary().exists()).toBe(false); ${'statistics history'} | ${'does not display'} | ${findStatisticsHistory} | ${false}
expect(findStatisticsHistory().exists()).toBe(false); ${'chart empty state'} | ${'does not display'} | ${findChartEmptyState} | ${false}
}); `('$status the $component', async ({ findComponent, state }) => {
expect(findComponent().exists()).toBe(state);
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);
}); });
}); });
...@@ -172,25 +167,15 @@ describe('ThreatMonitoringSection component', () => { ...@@ -172,25 +167,15 @@ describe('ThreatMonitoringSection component', () => {
}); });
}); });
it('does not show the loading skeleton', () => { it.each`
expect(findLoadingSkeleton().exists()).toBe(false); component | status | findComponent | state
}); ${'loading skeleton'} | ${'does not display'} | ${findLoadingSkeleton} | ${false}
${'chart subtitle'} | ${'does not display'} | ${findChartSubtitle} | ${false}
it('does not show the summary or history statistics', () => { ${'statistics summary'} | ${'does not display'} | ${findStatisticsSummary} | ${false}
expect(findStatisticsSummary().exists()).toBe(false); ${'statistics history'} | ${'does not display'} | ${findStatisticsHistory} | ${false}
expect(findStatisticsHistory().exists()).toBe(false); ${'chart empty state'} | ${'does not display'} | ${findChartEmptyState} | ${true}
}); `('$status the $component', async ({ findComponent, state }) => {
expect(findComponent().exists()).toBe(state);
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();
}); });
}); });
}); });
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