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

json_table_spec.js « components « behaviors « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ae62d28d6c0d1bc203f627691e64de6d738c41fa (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { GlTable, GlFormInput } from '@gitlab/ui';
import { nextTick } from 'vue';
import { merge } from 'lodash';
import { shallowMountExtended, mountExtended } from 'helpers/vue_test_utils_helper';
import { stubComponent, RENDER_ALL_SLOTS_TEMPLATE } from 'helpers/stub_component';
import JSONTable from '~/behaviors/components/json_table.vue';

const TEST_FIELDS = [
  'A',
  {
    key: 'B',
    label: 'Second',
    sortable: true,
    other: 'foo',
    class: 'someClass',
  },
  {
    key: 'C',
    label: 'Third',
  },
  'D',
];
const TEST_ITEMS = [
  { A: 1, B: 'lorem', C: 2, D: null, E: 'dne' },
  { A: 2, B: 'ipsum', C: 2, D: null, E: 'dne' },
  { A: 3, B: 'dolar', C: 2, D: null, E: 'dne' },
];

describe('behaviors/components/json_table', () => {
  let wrapper;

  const buildWrapper = ({
    fields = [],
    items = [],
    filter = undefined,
    caption = undefined,
  } = {}) => {
    wrapper = shallowMountExtended(JSONTable, {
      propsData: {
        fields,
        items,
        hasFilter: filter,
        caption,
      },
      stubs: {
        GlTable: merge(stubComponent(GlTable), {
          props: {
            fields: {
              type: Array,
              required: true,
            },
            items: {
              type: Array,
              required: true,
            },
          },
          template: RENDER_ALL_SLOTS_TEMPLATE,
        }),
      },
    });
  };

  const findTable = () => wrapper.findComponent(GlTable);
  const findTableCaption = () => wrapper.findByTestId('slot-table-caption');
  const findFilterInput = () => wrapper.findComponent(GlFormInput);

  describe('default', () => {
    beforeEach(() => {
      buildWrapper();
    });

    it('renders gltable', () => {
      expect(findTable().props()).toEqual({
        fields: [],
        items: [],
      });
      expect(findTable().attributes()).toMatchObject({
        filter: '',
        'show-empty': '',
      });
    });

    it('does not render filter input', () => {
      expect(findFilterInput().exists()).toBe(false);
    });

    it('renders caption', () => {
      expect(findTableCaption().text()).toBe('Generated with JSON data');
    });
  });

  describe('with filter', () => {
    beforeEach(() => {
      buildWrapper({
        filter: true,
      });
    });

    it('renders filter input', () => {
      expect(findFilterInput().attributes()).toMatchObject({
        value: '',
        placeholder: 'Type to search',
      });
    });

    it('when input is changed, updates table filter', async () => {
      findFilterInput().vm.$emit('input', 'New value!');

      await nextTick();

      expect(findTable().attributes('filter')).toBe('New value!');
    });
  });

  describe('with fields', () => {
    beforeEach(() => {
      buildWrapper({
        fields: TEST_FIELDS,
        items: TEST_ITEMS,
      });
    });

    it('passes cleaned fields and items to table', () => {
      expect(findTable().props()).toEqual({
        fields: [
          'A',
          {
            key: 'B',
            label: 'Second',
            sortable: true,
            class: 'someClass',
          },
          {
            key: 'C',
            label: 'Third',
            sortable: false,
            class: [],
          },
          'D',
        ],
        items: TEST_ITEMS,
      });
    });
  });

  describe('with full mount', () => {
    beforeEach(() => {
      wrapper = mountExtended(JSONTable, {
        propsData: {
          fields: [],
          items: [],
        },
      });
    });

    // We want to make sure all the props are passed down nicely in integration
    it('renders table without errors', () => {
      expect(findTable().exists()).toBe(true);
    });
  });
});