debian-mirror-gitlab/spec/frontend/jira_connect/subscriptions/components/app_spec.js

131 lines
3.9 KiB
JavaScript
Raw Normal View History

2021-04-17 20:07:23 +05:30
import { GlAlert, GlButton, GlModal, GlLink } from '@gitlab/ui';
import { mount, shallowMount } from '@vue/test-utils';
2021-03-11 19:13:27 +05:30
2021-10-27 15:23:28 +05:30
import JiraConnectApp from '~/jira_connect/subscriptions/components/app.vue';
import createStore from '~/jira_connect/subscriptions/store';
import { SET_ALERT } from '~/jira_connect/subscriptions/store/mutation_types';
2021-04-17 20:07:23 +05:30
import { __ } from '~/locale';
2021-03-08 18:12:59 +05:30
2021-10-27 15:23:28 +05:30
jest.mock('~/jira_connect/subscriptions/utils', () => ({
2021-04-29 21:17:54 +05:30
retrieveAlert: jest.fn().mockReturnValue({ message: 'error message' }),
getLocation: jest.fn(),
}));
2021-03-08 18:12:59 +05:30
describe('JiraConnectApp', () => {
let wrapper;
let store;
const findAlert = () => wrapper.findComponent(GlAlert);
2021-04-29 21:17:54 +05:30
const findAlertLink = () => findAlert().findComponent(GlLink);
2021-03-11 19:13:27 +05:30
const findGlButton = () => wrapper.findComponent(GlButton);
const findGlModal = () => wrapper.findComponent(GlModal);
2021-03-08 18:12:59 +05:30
2021-04-17 20:07:23 +05:30
const createComponent = ({ provide, mountFn = shallowMount } = {}) => {
2021-03-08 18:12:59 +05:30
store = createStore();
2021-04-29 21:17:54 +05:30
wrapper = mountFn(JiraConnectApp, {
store,
provide,
});
2021-03-08 18:12:59 +05:30
};
afterEach(() => {
wrapper.destroy();
});
describe('template', () => {
2021-03-11 19:13:27 +05:30
describe('when user is not logged in', () => {
beforeEach(() => {
createComponent({
provide: {
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);
});
});
2021-04-17 20:07:23 +05:30
describe('alert', () => {
it.each`
message | variant | alertShouldRender
${'Test error'} | ${'danger'} | ${true}
${'Test notice'} | ${'info'} | ${true}
${''} | ${undefined} | ${false}
${undefined} | ${undefined} | ${false}
`(
'renders correct alert when message is `$message` and variant is `$variant`',
async ({ message, alertShouldRender, variant }) => {
createComponent();
store.commit(SET_ALERT, { message, variant });
await wrapper.vm.$nextTick();
const alert = findAlert();
expect(alert.exists()).toBe(alertShouldRender);
if (alertShouldRender) {
expect(alert.isVisible()).toBe(alertShouldRender);
expect(alert.html()).toContain(message);
expect(alert.props('variant')).toBe(variant);
expect(findAlertLink().exists()).toBe(false);
}
},
);
it('hides alert on @dismiss event', async () => {
createComponent();
store.commit(SET_ALERT, { message: 'test message' });
await wrapper.vm.$nextTick();
findAlert().vm.$emit('dismiss');
await wrapper.vm.$nextTick();
2021-03-08 18:12:59 +05:30
2021-04-17 20:07:23 +05:30
expect(findAlert().exists()).toBe(false);
2021-03-08 18:12:59 +05:30
});
2021-04-17 20:07:23 +05:30
it('renders link when `linkUrl` is set', async () => {
createComponent({ mountFn: mount });
2021-03-08 18:12:59 +05:30
2021-04-17 20:07:23 +05:30
store.commit(SET_ALERT, {
message: __('test message %{linkStart}test link%{linkEnd}'),
linkUrl: 'https://gitlab.com',
});
2021-03-08 18:12:59 +05:30
await wrapper.vm.$nextTick();
2021-04-17 20:07:23 +05:30
const alertLink = findAlertLink();
expect(alertLink.exists()).toBe(true);
expect(alertLink.text()).toContain('test link');
expect(alertLink.attributes('href')).toBe('https://gitlab.com');
});
describe('when alert is set in localStoage', () => {
it('renders alert on mount', () => {
createComponent();
const alert = findAlert();
expect(alert.exists()).toBe(true);
expect(alert.html()).toContain('error message');
});
});
});
2021-03-08 18:12:59 +05:30
});
});