summaryrefslogtreecommitdiff
path: root/spec/frontend/ide/components/commit_sidebar/radio_group_spec.js
blob: d899bc4f7d87eab9e8a39aa0b7d28f67bee73c1f (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
import Vue, { nextTick } from 'vue';
import { createComponentWithStore } from 'helpers/vue_mount_component_helper';
import radioGroup from '~/ide/components/commit_sidebar/radio_group.vue';
import { createStore } from '~/ide/stores';

describe('IDE commit sidebar radio group', () => {
  let vm;
  let store;

  beforeEach(async () => {
    store = createStore();

    const Component = Vue.extend(radioGroup);

    store.state.commit.commitAction = '2';

    vm = createComponentWithStore(Component, store, {
      value: '1',
      label: 'test',
      checked: true,
    });

    vm.$mount();

    await nextTick();
  });

  afterEach(() => {
    vm.$destroy();
  });

  it('uses label if present', () => {
    expect(vm.$el.textContent).toContain('test');
  });

  it('uses slot if label is not present', async () => {
    vm.$destroy();

    vm = new Vue({
      components: {
        radioGroup,
      },
      store,
      render: (createElement) =>
        createElement('radio-group', { props: { value: '1' } }, 'Testing slot'),
    });

    vm.$mount();

    await nextTick();
    expect(vm.$el.textContent).toContain('Testing slot');
  });

  it('updates store when changing radio button', async () => {
    vm.$el.querySelector('input').dispatchEvent(new Event('change'));

    await nextTick();
    expect(store.state.commit.commitAction).toBe('1');
  });

  describe('with input', () => {
    beforeEach(async () => {
      vm.$destroy();

      const Component = Vue.extend(radioGroup);

      store.state.commit.commitAction = '1';
      store.state.commit.newBranchName = 'test-123';

      vm = createComponentWithStore(Component, store, {
        value: '1',
        label: 'test',
        checked: true,
        showInput: true,
      });

      vm.$mount();

      await nextTick();
    });

    it('renders input box when commitAction matches value', () => {
      expect(vm.$el.querySelector('.form-control')).not.toBeNull();
    });

    it('hides input when commitAction doesnt match value', async () => {
      store.state.commit.commitAction = '2';

      await nextTick();
      expect(vm.$el.querySelector('.form-control')).toBeNull();
    });

    it('updates branch name in store on input', async () => {
      const input = vm.$el.querySelector('.form-control');
      input.value = 'testing-123';
      input.dispatchEvent(new Event('input'));

      await nextTick();
      expect(store.state.commit.newBranchName).toBe('testing-123');
    });

    it('renders newBranchName if present', () => {
      const input = vm.$el.querySelector('.form-control');

      expect(input.value).toBe('test-123');
    });
  });

  describe('tooltipTitle', () => {
    it('returns title when disabled', () => {
      vm.title = 'test title';
      vm.disabled = true;

      expect(vm.tooltipTitle).toBe('test title');
    });

    it('returns blank when not disabled', () => {
      vm.title = 'test title';

      expect(vm.tooltipTitle).not.toBe('test title');
    });
  });
});