summaryrefslogtreecommitdiff
path: root/spec/javascripts/ide/components/error_message_spec.js
blob: 80d6c7fd5644e81d532cbd3c91d851ba2cd109de (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
import Vue from 'vue';
import store from '~/ide/stores';
import ErrorMessage from '~/ide/components/error_message.vue';
import { createComponentWithStore } from '../../helpers/vue_mount_component_helper';
import { resetStore } from '../helpers';

describe('IDE error message component', () => {
  const Component = Vue.extend(ErrorMessage);
  let vm;

  beforeEach(() => {
    vm = createComponentWithStore(Component, store, {
      message: {
        text: 'error message',
        action: null,
        actionText: null,
      },
    }).$mount();
  });

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

  it('renders error message', () => {
    expect(vm.$el.textContent).toContain('error message');
  });

  it('clears error message on click', () => {
    spyOn(vm, 'setErrorMessage');

    vm.$el.click();

    expect(vm.setErrorMessage).toHaveBeenCalledWith(null);
  });

  describe('with action', () => {
    let actionSpy;

    beforeEach(done => {
      actionSpy = jasmine.createSpy('action').and.returnValue(Promise.resolve());

      vm.message.action = actionSpy;
      vm.message.actionText = 'test action';
      vm.message.actionPayload = 'testActionPayload';

      vm.$nextTick(done);
    });

    it('renders action button', () => {
      expect(vm.$el.querySelector('.flash-action')).not.toBe(null);
      expect(vm.$el.textContent).toContain('test action');
    });

    it('does not clear error message on click', () => {
      spyOn(vm, 'setErrorMessage');

      vm.$el.click();

      expect(vm.setErrorMessage).not.toHaveBeenCalled();
    });

    it('dispatches action', done => {
      vm.$el.querySelector('.flash-action').click();

      vm.$nextTick(() => {
        expect(actionSpy).toHaveBeenCalledWith('testActionPayload');

        done();
      });
    });

    it('does not dispatch action when already loading', () => {
      vm.isLoading = true;

      vm.$el.querySelector('.flash-action').click();

      expect(actionSpy).not.toHaveBeenCalledWith();
    });

    it('resets isLoading after click', done => {
      vm.$el.querySelector('.flash-action').click();

      expect(vm.isLoading).toBe(true);

      setTimeout(() => {
        expect(vm.isLoading).toBe(false);

        done();
      });
    });

    it('shows loading icon when isLoading is true', done => {
      expect(vm.$el.querySelector('.loading-container').style.display).not.toBe('');

      vm.isLoading = true;

      vm.$nextTick(() => {
        expect(vm.$el.querySelector('.loading-container').style.display).toBe('');

        done();
      });
    });
  });
});