summaryrefslogtreecommitdiff
path: root/spec/javascripts/pipelines/async_button_spec.js
blob: 28c9c7ab2829c800e6adfa7101da62641702e690 (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
import Vue from 'vue';
import asyncButtonComp from '~/pipelines/components/async_button.vue';

describe('Pipelines Async Button', () => {
  let component;
  let spy;
  let AsyncButtonComponent;

  beforeEach(() => {
    AsyncButtonComponent = Vue.extend(asyncButtonComp);

    spy = jasmine.createSpy('spy').and.returnValue(Promise.resolve());

    component = new AsyncButtonComponent({
      propsData: {
        endpoint: '/foo',
        title: 'Foo',
        icon: 'fa fa-foo',
        cssClass: 'bar',
        service: {
          postAction: spy,
        },
      },
    }).$mount();
  });

  it('should render a button', () => {
    expect(component.$el.tagName).toEqual('BUTTON');
  });

  it('should render the provided icon', () => {
    expect(component.$el.querySelector('i').getAttribute('class')).toContain('fa fa-foo');
  });

  it('should render the provided title', () => {
    expect(component.$el.getAttribute('title')).toContain('Foo');
    expect(component.$el.getAttribute('aria-label')).toContain('Foo');
  });

  it('should render the provided cssClass', () => {
    expect(component.$el.getAttribute('class')).toContain('bar');
  });

  it('should call the service when it is clicked with the provided endpoint', () => {
    component.$el.click();
    expect(spy).toHaveBeenCalledWith('/foo');
  });

  it('should hide loading if request fails', () => {
    spy = jasmine.createSpy('spy').and.returnValue(Promise.reject());

    component = new AsyncButtonComponent({
      propsData: {
        endpoint: '/foo',
        title: 'Foo',
        icon: 'fa fa-foo',
        cssClass: 'bar',
        dataAttributes: {
          'data-foo': 'foo',
        },
        service: {
          postAction: spy,
        },
      },
    }).$mount();

    component.$el.click();
    expect(component.$el.querySelector('.fa-spinner')).toBe(null);
  });

  describe('With confirm dialog', () => {
    it('should call the service when confimation is positive', () => {
      spyOn(window, 'confirm').and.returnValue(true);
      spy = jasmine.createSpy('spy').and.returnValue(Promise.resolve());

      component = new AsyncButtonComponent({
        propsData: {
          endpoint: '/foo',
          title: 'Foo',
          icon: 'fa fa-foo',
          cssClass: 'bar',
          service: {
            postAction: spy,
          },
          confirmActionMessage: 'bar',
        },
      }).$mount();

      component.$el.click();
      expect(spy).toHaveBeenCalledWith('/foo');
    });
  });
});