summaryrefslogtreecommitdiff
path: root/spec/frontend/jira_connect/components/app_spec.js
blob: a9194cfc883042070c9d0161d03a1ac0aa83155e (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
import { shallowMount } from '@vue/test-utils';
import { GlAlert, GlButton, GlModal } from '@gitlab/ui';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';

import JiraConnectApp from '~/jira_connect/components/app.vue';
import createStore from '~/jira_connect/store';
import { SET_ERROR_MESSAGE } from '~/jira_connect/store/mutation_types';

jest.mock('~/jira_connect/api');

describe('JiraConnectApp', () => {
  let wrapper;
  let store;

  const findAlert = () => wrapper.findComponent(GlAlert);
  const findGlButton = () => wrapper.findComponent(GlButton);
  const findGlModal = () => wrapper.findComponent(GlModal);
  const findHeader = () => wrapper.findByTestId('new-jira-connect-ui-heading');
  const findHeaderText = () => findHeader().text();

  const createComponent = (options = {}) => {
    store = createStore();

    wrapper = extendedWrapper(
      shallowMount(JiraConnectApp, {
        store,
        provide: {
          glFeatures: { newJiraConnectUi: true },
        },
        ...options,
      }),
    );
  };

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

  describe('template', () => {
    it('renders new UI', () => {
      createComponent();

      expect(findHeader().exists()).toBe(true);
      expect(findHeaderText()).toBe('Linked namespaces');
    });

    describe('when user is not logged in', () => {
      beforeEach(() => {
        createComponent({
          provide: {
            glFeatures: { newJiraConnectUi: true },
            usersPath: '/users',
          },
        });
      });

      it('renders "Sign in" button', () => {
        expect(findGlButton().text()).toBe('Sign in to add namespaces');
        expect(findGlModal().exists()).toBe(false);
      });
    });

    describe('when user is logged in', () => {
      beforeEach(() => {
        createComponent();
      });

      it('renders "Add" button and modal', () => {
        expect(findGlButton().text()).toBe('Add namespace');
        expect(findGlModal().exists()).toBe(true);
      });
    });

    describe('newJiraConnectUi is false', () => {
      it('does not render new UI', () => {
        createComponent({
          provide: {
            glFeatures: { newJiraConnectUi: false },
          },
        });

        expect(findHeader().exists()).toBe(false);
      });
    });

    it.each`
      errorMessage    | errorShouldRender
      ${'Test error'} | ${true}
      ${''}           | ${false}
      ${undefined}    | ${false}
    `(
      'renders correct alert when errorMessage is `$errorMessage`',
      async ({ errorMessage, errorShouldRender }) => {
        createComponent();

        store.commit(SET_ERROR_MESSAGE, errorMessage);
        await wrapper.vm.$nextTick();

        expect(findAlert().exists()).toBe(errorShouldRender);
        if (errorShouldRender) {
          expect(findAlert().isVisible()).toBe(errorShouldRender);
          expect(findAlert().html()).toContain(errorMessage);
        }
      },
    );
  });
});