summaryrefslogtreecommitdiff
path: root/spec/frontend/monitoring/components/variables/custom_variable_spec.js
blob: 5a2b26219b61f5ee4bf5bd8873d89707bf3e67e4 (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
import { shallowMount } from '@vue/test-utils';
import { GlDropdown, GlDropdownItem } from '@gitlab/ui';
import CustomVariable from '~/monitoring/components/variables/custom_variable.vue';

describe('Custom variable component', () => {
  let wrapper;
  const propsData = {
    name: 'env',
    label: 'Select environment',
    value: 'Production',
    options: [{ text: 'Production', value: 'prod' }, { text: 'Canary', value: 'canary' }],
  };
  const createShallowWrapper = () => {
    wrapper = shallowMount(CustomVariable, {
      propsData,
    });
  };

  const findDropdown = () => wrapper.find(GlDropdown);
  const findDropdownItems = () => wrapper.findAll(GlDropdownItem);

  it('renders dropdown element when all necessary props are passed', () => {
    createShallowWrapper();

    expect(findDropdown()).toExist();
  });

  it('renders dropdown element with a text', () => {
    createShallowWrapper();

    expect(findDropdown().attributes('text')).toBe(propsData.value);
  });

  it('renders all the dropdown items', () => {
    createShallowWrapper();

    expect(findDropdownItems()).toHaveLength(propsData.options.length);
  });

  it('changing dropdown items triggers update', () => {
    createShallowWrapper();
    jest.spyOn(wrapper.vm, '$emit');

    findDropdownItems()
      .at(1)
      .vm.$emit('click');

    return wrapper.vm.$nextTick(() => {
      expect(wrapper.vm.$emit).toHaveBeenCalledWith('onUpdate', 'env', 'canary');
    });
  });
});