summaryrefslogtreecommitdiff
path: root/spec/frontend/clusters/components/knative_domain_editor_spec.js
blob: 11ebe1b5d617a7f80999f734e69dcb4e49e0c659 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { shallowMount } from '@vue/test-utils';
import { GlDeprecatedDropdownItem, GlButton } from '@gitlab/ui';
import KnativeDomainEditor from '~/clusters/components/knative_domain_editor.vue';
import { APPLICATION_STATUS } from '~/clusters/constants';

const { UPDATING } = APPLICATION_STATUS;

describe('KnativeDomainEditor', () => {
  let wrapper;
  let knative;

  const createComponent = (props = {}) => {
    wrapper = shallowMount(KnativeDomainEditor, {
      propsData: { ...props },
    });
  };

  beforeEach(() => {
    knative = {
      title: 'Knative',
      hostname: 'example.com',
      installed: true,
    };
  });

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

  describe('knative has an assigned IP address', () => {
    beforeEach(() => {
      knative.externalIp = '1.1.1.1';
      createComponent({ knative });
    });

    it('renders ip address with a clipboard button', () => {
      expect(wrapper.find('.js-knative-endpoint').exists()).toBe(true);
      expect(wrapper.find('.js-knative-endpoint').element.value).toEqual(knative.externalIp);
    });

    it('displays ip address clipboard button', () => {
      expect(wrapper.find('.js-knative-endpoint-clipboard-btn').attributes('text')).toEqual(
        knative.externalIp,
      );
    });

    it('renders domain & allows editing', () => {
      const domainNameInput = wrapper.find('.js-knative-domainname');

      expect(domainNameInput.element.value).toEqual(knative.hostname);
      expect(domainNameInput.attributes('readonly')).toBeFalsy();
    });

    it('renders an update/save Knative domain button', () => {
      expect(wrapper.find('.js-knative-save-domain-button').exists()).toBe(true);
    });
  });

  describe('knative without ip address', () => {
    beforeEach(() => {
      knative.externalIp = null;
      createComponent({ knative });
    });

    it('renders an input text with a loading icon', () => {
      expect(wrapper.find('.js-knative-ip-loading-icon').exists()).toBe(true);
    });

    it('renders message indicating there is not IP address assigned', () => {
      expect(wrapper.find('.js-no-knative-endpoint-message').exists()).toBe(true);
    });
  });

  describe('clicking save changes button', () => {
    beforeEach(() => {
      createComponent({ knative });
    });

    it('triggers save event and pass current knative hostname', () => {
      wrapper.find(GlButton).vm.$emit('click');
      return wrapper.vm.$nextTick().then(() => {
        expect(wrapper.emitted('save').length).toEqual(1);
      });
    });
  });

  describe('when knative domain name was saved successfully', () => {
    beforeEach(() => {
      createComponent({ knative });
    });

    it('displays toast indicating a successful update', () => {
      wrapper.vm.$toast = { show: jest.fn() };
      wrapper.setProps({ knative: { updateSuccessful: true, ...knative } });

      return wrapper.vm.$nextTick(() => {
        expect(wrapper.vm.$toast.show).toHaveBeenCalledWith(
          'Knative domain name was updated successfully.',
        );
      });
    });
  });

  describe('when knative domain name input changes', () => {
    it('emits "set" event with updated domain name', () => {
      const newDomain = {
        id: 4,
        domain: 'newhostname.com',
      };

      createComponent({ knative: { ...knative, availableDomains: [newDomain] } });
      jest.spyOn(wrapper.vm, 'selectDomain');

      wrapper.find(GlDeprecatedDropdownItem).vm.$emit('click');

      return wrapper.vm.$nextTick().then(() => {
        expect(wrapper.vm.selectDomain).toHaveBeenCalledWith(newDomain);
        expect(wrapper.emitted('set')[0]).toEqual([
          {
            domain: newDomain.domain,
            domainId: newDomain.id,
          },
        ]);
      });
    });

    it('emits "set" event with updated custom domain name', () => {
      const newHostname = 'newhostname.com';

      createComponent({ knative });
      jest.spyOn(wrapper.vm, 'selectCustomDomain');

      wrapper.setData({ knativeHostname: newHostname });

      return wrapper.vm.$nextTick().then(() => {
        expect(wrapper.vm.selectCustomDomain).toHaveBeenCalledWith(newHostname);
        expect(wrapper.emitted('set')[0]).toEqual([
          {
            domain: newHostname,
            domainId: null,
          },
        ]);
      });
    });
  });

  describe('when updating knative domain name failed', () => {
    beforeEach(() => {
      createComponent({ knative });
    });

    it('displays an error banner indicating the operation failure', () => {
      wrapper.setProps({ knative: { updateFailed: true, ...knative } });

      return wrapper.vm.$nextTick().then(() => {
        expect(wrapper.find('.js-cluster-knative-domain-name-failure-message').exists()).toBe(true);
      });
    });
  });

  describe(`when knative status is ${UPDATING}`, () => {
    beforeEach(() => {
      createComponent({ knative: { status: UPDATING, ...knative } });
    });

    it('renders loading spinner in save button', () => {
      expect(wrapper.find(GlButton).props('loading')).toBe(true);
    });

    it('renders disabled save button', () => {
      expect(wrapper.find(GlButton).props('disabled')).toBe(true);
    });

    it('renders save button with "Saving" label', () => {
      expect(wrapper.find(GlButton).text()).toBe('Saving');
    });
  });
});