summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/segmented_control_button_group_spec.js
blob: 88445b6684ce8c8d88a2d33e437bbf8775b5907a (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
import { GlButtonGroup, GlButton } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import SegmentedControlButtonGroup from '~/vue_shared/components/segmented_control_button_group.vue';

const DEFAULT_OPTIONS = [
  { text: 'Lorem', value: 'abc' },
  { text: 'Ipsum', value: 'def' },
  { text: 'Foo', value: 'x', disabled: true },
  { text: 'Dolar', value: 'ghi' },
];

describe('~/vue_shared/components/segmented_control_button_group.vue', () => {
  let wrapper;

  const createComponent = (props = {}, scopedSlots = {}) => {
    wrapper = shallowMount(SegmentedControlButtonGroup, {
      propsData: {
        value: DEFAULT_OPTIONS[0].value,
        options: DEFAULT_OPTIONS,
        ...props,
      },
      scopedSlots,
    });
  };

  const findButtonGroup = () => wrapper.findComponent(GlButtonGroup);
  const findButtons = () => findButtonGroup().findAllComponents(GlButton);
  const findButtonsData = () =>
    findButtons().wrappers.map((x) => ({
      selected: x.props('selected'),
      text: x.text(),
      disabled: x.props('disabled'),
    }));
  const findButtonWithText = (text) => findButtons().wrappers.find((x) => x.text() === text);

  const optionsAsButtonData = (options) =>
    options.map(({ text, disabled = false }) => ({
      selected: false,
      text,
      disabled,
    }));

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

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

    it('renders button group', () => {
      expect(findButtonGroup().exists()).toBe(true);
    });

    it('renders buttons', () => {
      const expectation = optionsAsButtonData(DEFAULT_OPTIONS);
      expectation[0].selected = true;

      expect(findButtonsData()).toEqual(expectation);
    });

    describe.each(DEFAULT_OPTIONS.filter((x) => !x.disabled))(
      'when button clicked %p',
      ({ text, value }) => {
        it('emits input with value', () => {
          expect(wrapper.emitted('input')).toBeUndefined();

          findButtonWithText(text).vm.$emit('click');

          expect(wrapper.emitted('input')).toEqual([[value]]);
        });
      },
    );
  });

  const VALUE_TEST_CASES = [0, 1, 3].map((index) => [DEFAULT_OPTIONS[index].value, index]);

  describe.each(VALUE_TEST_CASES)('with value=%s', (value, index) => {
    it(`renders selected button at ${index}`, () => {
      createComponent({ value });

      const expectation = optionsAsButtonData(DEFAULT_OPTIONS);
      expectation[index].selected = true;

      expect(findButtonsData()).toEqual(expectation);
    });
  });

  describe('with button-content slot', () => {
    it('renders button content based on slot', () => {
      createComponent(
        {},
        {
          'button-content': `<template #button-content="{ text }">In a slot - {{ text }}</template>`,
        },
      );

      expect(findButtonsData().map((x) => x.text)).toEqual(
        DEFAULT_OPTIONS.map((x) => `In a slot - ${x.text}`),
      );
    });
  });
});