Welcome to mirror list, hosted at ThFree Co, Russian Federation.

workload_table_spec.js « components « kubernetes_dashboard « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 369b8f32c2df6fc4a708dafd37ef5cc3e70afc14 (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
121
122
123
124
125
126
127
128
import { mount } from '@vue/test-utils';
import { GlTable, GlBadge, GlPagination } from '@gitlab/ui';
import WorkloadTable from '~/kubernetes_dashboard/components/workload_table.vue';
import { TABLE_HEADING_CLASSES, PAGE_SIZE } from '~/kubernetes_dashboard/constants';
import { mockPodsTableItems } from '../graphql/mock_data';

let wrapper;

const createWrapper = (propsData = {}) => {
  wrapper = mount(WorkloadTable, {
    propsData,
  });
};

const findTable = () => wrapper.findComponent(GlTable);
const findAllRows = () => findTable().find('tbody').findAll('tr');
const findRow = (at) => findAllRows().at(at);
const findAllBadges = () => wrapper.findAllComponents(GlBadge);
const findBadge = (at) => findAllBadges().at(at);
const findPagination = () => wrapper.findComponent(GlPagination);

describe('Workload table component', () => {
  it('renders GlTable component with the default fields if no fields specified in props', () => {
    createWrapper({ items: mockPodsTableItems });
    const defaultFields = [
      {
        key: 'name',
        label: 'Name',
        thClass: TABLE_HEADING_CLASSES,
        sortable: true,
      },
      {
        key: 'status',
        label: 'Status',
        thClass: TABLE_HEADING_CLASSES,
        sortable: true,
      },
      {
        key: 'namespace',
        label: 'Namespace',
        thClass: TABLE_HEADING_CLASSES,
        sortable: true,
      },
      {
        key: 'age',
        label: 'Age',
        thClass: TABLE_HEADING_CLASSES,
        sortable: true,
      },
    ];

    expect(findTable().props('fields')).toEqual(defaultFields);
  });

  it('renders GlTable component fields specified in props', () => {
    const customFields = [
      {
        key: 'field-1',
        label: 'Field-1',
        thClass: TABLE_HEADING_CLASSES,
        sortable: true,
      },
      {
        key: 'field-2',
        label: 'Field-2',
        thClass: TABLE_HEADING_CLASSES,
        sortable: true,
      },
    ];
    createWrapper({ items: mockPodsTableItems, fields: customFields });

    expect(findTable().props('fields')).toEqual(customFields);
  });

  describe('table rows', () => {
    beforeEach(() => {
      createWrapper({ items: mockPodsTableItems });
    });

    it('displays the correct number of rows', () => {
      expect(findAllRows()).toHaveLength(mockPodsTableItems.length);
    });

    it('emits an event on row click', () => {
      mockPodsTableItems.forEach((data, index) => {
        findRow(index).trigger('click');

        expect(wrapper.emitted('select-item')[index]).toEqual([data]);
      });
    });

    it('renders correct data for each row', () => {
      mockPodsTableItems.forEach((data, index) => {
        expect(findRow(index).text()).toContain(data.name);
        expect(findRow(index).text()).toContain(data.namespace);
        expect(findRow(index).text()).toContain(data.status);
        expect(findRow(index).text()).toContain(data.age);
      });
    });

    it('renders a badge for the status', () => {
      expect(findAllBadges()).toHaveLength(mockPodsTableItems.length);
    });

    it.each`
      status         | variant      | index
      ${'Running'}   | ${'info'}    | ${0}
      ${'Running'}   | ${'info'}    | ${1}
      ${'Pending'}   | ${'warning'} | ${2}
      ${'Succeeded'} | ${'success'} | ${3}
      ${'Failed'}    | ${'danger'}  | ${4}
      ${'Failed'}    | ${'danger'}  | ${5}
    `(
      'renders "$variant" badge for status "$status" at index "$index"',
      ({ status, variant, index }) => {
        expect(findBadge(index).text()).toBe(status);
        expect(findBadge(index).props('variant')).toBe(variant);
      },
    );

    it('renders pagination', () => {
      expect(findPagination().props()).toMatchObject({
        totalItems: mockPodsTableItems.length,
        perPage: PAGE_SIZE,
      });
    });
  });
});