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

loading_button_spec.js « components « vue_shared « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8bcb80d140ef80cb1ae28d1d1ed4fa69a3808ba6 (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
import { shallowMount } from '@vue/test-utils';
import LoadingButton from '~/vue_shared/components/loading_button.vue';

const LABEL = 'Hello';

describe('LoadingButton', () => {
  let wrapper;

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

  const buildWrapper = (propsData = {}) => {
    wrapper = shallowMount(LoadingButton, {
      propsData,
    });
  };
  const findButtonLabel = () => wrapper.find('.js-loading-button-label');
  const findButtonIcon = () => wrapper.find('.js-loading-button-icon');

  describe('loading spinner', () => {
    it('shown when loading', () => {
      buildWrapper({ loading: true });

      expect(findButtonIcon().exists()).toBe(true);
    });
  });

  describe('disabled state', () => {
    it('disabled when loading', () => {
      buildWrapper({ loading: true });
      expect(wrapper.attributes('disabled')).toBe('disabled');
    });

    it('not disabled when normal', () => {
      buildWrapper({ loading: false });

      expect(wrapper.attributes('disabled')).toBe(undefined);
    });
  });

  describe('label', () => {
    it('shown when normal', () => {
      buildWrapper({ loading: false, label: LABEL });
      expect(findButtonLabel().text()).toBe(LABEL);
    });

    it('shown when loading', () => {
      buildWrapper({ loading: false, label: LABEL });
      expect(findButtonLabel().text()).toBe(LABEL);
    });
  });

  describe('container class', () => {
    it('should default to btn btn-align-content', () => {
      buildWrapper();

      expect(wrapper.classes()).toContain('btn');
      expect(wrapper.classes()).toContain('btn-align-content');
    });

    it('should be configurable through props', () => {
      const containerClass = 'test-class';

      buildWrapper({
        containerClass,
      });

      expect(wrapper.classes()).not.toContain('btn');
      expect(wrapper.classes()).not.toContain('btn-align-content');
      expect(wrapper.classes()).toContain(containerClass);
    });
  });

  describe('click callback prop', () => {
    it('calls given callback when normal', () => {
      buildWrapper({
        loading: false,
      });

      wrapper.trigger('click');

      return wrapper.vm.$nextTick().then(() => {
        expect(wrapper.emitted('click')).toBeTruthy();
      });
    });

    it('does not call given callback when disabled because of loading', () => {
      buildWrapper({
        loading: true,
      });

      wrapper.trigger('click');

      return wrapper.vm.$nextTick().then(() => {
        expect(wrapper.emitted('click')).toBeFalsy();
      });
    });
  });
});