summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/alert_details_table_spec.js
blob: ef7815f9e9eb8c30a0e0fcbf2edb2d897889a35a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { GlLoadingIcon, GlTable } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import AlertDetailsTable from '~/vue_shared/components/alert_details_table.vue';

const mockAlert = {
  iid: '1527542',
  title: 'SyntaxError: Invalid or unexpected token',
  severity: 'CRITICAL',
  eventCount: 7,
  createdAt: '2020-04-17T23:18:14.996Z',
  startedAt: '2020-04-17T23:18:14.996Z',
  endedAt: '2020-04-17T23:18:14.996Z',
  status: 'TRIGGERED',
  assignees: { nodes: [] },
  notes: { nodes: [] },
  todos: { nodes: [] },
  hosts: ['host1', 'host2'],
  __typename: 'AlertManagementAlert',
};

const environmentName = 'Production';
const environmentPath = '/fake/path';

describe('AlertDetails', () => {
  let environmentData = { name: environmentName, path: environmentPath };
  let wrapper;

  function mountComponent(propsData = {}) {
    wrapper = mount(AlertDetailsTable, {
      propsData: {
        alert: {
          ...mockAlert,
          environment: environmentData,
        },
        loading: false,
        ...propsData,
      },
    });
  }

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
  });

  const findTableComponent = () => wrapper.find(GlTable);
  const findTableKeys = () => findTableComponent().findAll('tbody td:first-child');
  const findTableFieldValueByKey = fieldKey =>
    findTableComponent()
      .findAll('tbody tr')
      .filter(row => row.text().includes(fieldKey))
      .at(0)
      .find('td:nth-child(2)');
  const findTableField = (fields, fieldName) => fields.filter(row => row.text() === fieldName);

  describe('Alert details', () => {
    describe('empty state', () => {
      beforeEach(() => {
        mountComponent({ alert: null });
      });

      it('shows an empty state when no alert is provided', () => {
        expect(wrapper.text()).toContain('No alert data to display.');
      });
    });

    describe('loading state', () => {
      beforeEach(() => {
        mountComponent({ loading: true });
      });

      it('displays a loading state when loading', () => {
        expect(wrapper.find(GlLoadingIcon).exists()).toBe(true);
      });
    });

    describe('with table data', () => {
      beforeEach(mountComponent);

      it('renders a table', () => {
        expect(findTableComponent().exists()).toBe(true);
      });

      it('renders a cell based on alert data', () => {
        expect(findTableComponent().text()).toContain('SyntaxError: Invalid or unexpected token');
      });

      it('should show allowed alert fields', () => {
        const fields = findTableKeys();

        expect(findTableField(fields, 'Iid').exists()).toBe(true);
        expect(findTableField(fields, 'Title').exists()).toBe(true);
        expect(findTableField(fields, 'Severity').exists()).toBe(true);
        expect(findTableField(fields, 'Status').exists()).toBe(true);
        expect(findTableField(fields, 'Hosts').exists()).toBe(true);
        expect(findTableField(fields, 'Environment').exists()).toBe(true);
      });

      it('should not show disallowed alert fields', () => {
        const fields = findTableKeys();

        expect(findTableField(fields, 'Typename').exists()).toBe(false);
        expect(findTableField(fields, 'Todos').exists()).toBe(false);
        expect(findTableField(fields, 'Notes').exists()).toBe(false);
        expect(findTableField(fields, 'Assignees').exists()).toBe(false);
      });

      it('should display only the name for the environment', () => {
        expect(findTableFieldValueByKey('Environment').text()).toBe(environmentName);
      });

      it('should not display the environment row if there is not data', () => {
        environmentData = { name: null, path: null };
        mountComponent();

        expect(findTableFieldValueByKey('Environment').text()).toBeFalsy();
      });
    });
  });
});