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

173 lines
6 KiB
JavaScript
Raw Normal View History

2021-12-11 22:18:48 +05:30
import { GlAlert, GlLink, GlEmptyState } from '@gitlab/ui';
2021-04-17 20:07:23 +05:30
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';
2021-12-11 22:18:48 +05:30
import AddNamespaceButton from '~/jira_connect/subscriptions/components/add_namespace_button.vue';
import SignInButton from '~/jira_connect/subscriptions/components/sign_in_button.vue';
import SubscriptionsList from '~/jira_connect/subscriptions/components/subscriptions_list.vue';
2022-01-26 12:08:38 +05:30
import UserLink from '~/jira_connect/subscriptions/components/user_link.vue';
2021-10-27 15:23:28 +05:30
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-12-11 22:18:48 +05:30
import { mockSubscription } from '../mock_data';
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' }),
2022-01-26 12:08:38 +05:30
getGitlabSignInURL: jest.fn(),
2021-04-29 21:17:54 +05:30
}));
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-12-11 22:18:48 +05:30
const findSignInButton = () => wrapper.findComponent(SignInButton);
const findAddNamespaceButton = () => wrapper.findComponent(AddNamespaceButton);
const findSubscriptionsList = () => wrapper.findComponent(SubscriptionsList);
const findEmptyState = () => wrapper.findComponent(GlEmptyState);
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-12-11 22:18:48 +05:30
describe.each`
scenario | usersPath | subscriptions | expectSignInButton | expectEmptyState | expectNamespaceButton | expectSubscriptionsList
${'user is not signed in with subscriptions'} | ${'/users'} | ${[mockSubscription]} | ${true} | ${false} | ${false} | ${true}
${'user is not signed in without subscriptions'} | ${'/users'} | ${undefined} | ${true} | ${false} | ${false} | ${false}
${'user is signed in with subscriptions'} | ${undefined} | ${[mockSubscription]} | ${false} | ${false} | ${true} | ${true}
${'user is signed in without subscriptions'} | ${undefined} | ${undefined} | ${false} | ${true} | ${false} | ${false}
`(
'when $scenario',
({
usersPath,
expectSignInButton,
subscriptions,
expectEmptyState,
expectNamespaceButton,
expectSubscriptionsList,
}) => {
beforeEach(() => {
createComponent({
provide: {
usersPath,
subscriptions,
},
});
2021-03-11 19:13:27 +05:30
});
2021-12-11 22:18:48 +05:30
it(`${expectSignInButton ? 'renders' : 'does not render'} sign in button`, () => {
expect(findSignInButton().exists()).toBe(expectSignInButton);
});
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
it(`${expectEmptyState ? 'renders' : 'does not render'} empty state`, () => {
expect(findEmptyState().exists()).toBe(expectEmptyState);
});
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +05:30
it(`${
expectNamespaceButton ? 'renders' : 'does not render'
} button to add namespace`, () => {
expect(findAddNamespaceButton().exists()).toBe(expectNamespaceButton);
});
it(`${expectSubscriptionsList ? 'renders' : 'does not render'} subscriptions list`, () => {
expect(findSubscriptionsList().exists()).toBe(expectSubscriptionsList);
});
},
);
2022-01-26 12:08:38 +05:30
it('renders UserLink component', () => {
createComponent({
provide: {
usersPath: '/user',
subscriptions: [],
},
});
const userLink = wrapper.findComponent(UserLink);
expect(userLink.exists()).toBe(true);
expect(userLink.props()).toEqual({
hasSubscriptions: false,
userSignedIn: false,
});
});
2021-12-11 22:18:48 +05:30
});
2021-03-11 19:13:27 +05:30
2021-12-11 22:18:48 +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 }) => {
2021-04-17 20:07:23 +05:30
createComponent();
2021-12-11 22:18:48 +05:30
store.commit(SET_ALERT, { message, variant });
2021-04-17 20:07:23 +05:30
await wrapper.vm.$nextTick();
2021-12-11 22:18:48 +05:30
const alert = findAlert();
2021-03-08 18:12:59 +05:30
2021-12-11 22:18:48 +05:30
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);
}
},
);
2021-03-08 18:12:59 +05:30
2021-12-11 22:18:48 +05:30
it('hides alert on @dismiss event', async () => {
createComponent();
2021-03-08 18:12:59 +05:30
2021-12-11 22:18:48 +05:30
store.commit(SET_ALERT, { message: 'test message' });
await wrapper.vm.$nextTick();
findAlert().vm.$emit('dismiss');
await wrapper.vm.$nextTick();
expect(findAlert().exists()).toBe(false);
});
2021-03-08 18:12:59 +05:30
2021-12-11 22:18:48 +05:30
it('renders link when `linkUrl` is set', async () => {
createComponent({ mountFn: mount });
2021-04-17 20:07:23 +05:30
2021-12-11 22:18:48 +05:30
store.commit(SET_ALERT, {
message: __('test message %{linkStart}test link%{linkEnd}'),
linkUrl: 'https://gitlab.com',
2021-04-17 20:07:23 +05:30
});
2021-12-11 22:18:48 +05:30
await wrapper.vm.$nextTick();
2021-04-17 20:07:23 +05:30
2021-12-11 22:18:48 +05:30
const alertLink = findAlertLink();
2021-04-17 20:07:23 +05:30
2021-12-11 22:18:48 +05:30
expect(alertLink.exists()).toBe(true);
expect(alertLink.text()).toContain('test link');
expect(alertLink.attributes('href')).toBe('https://gitlab.com');
});
2021-04-17 20:07:23 +05:30
2021-12-11 22:18:48 +05:30
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-04-17 20:07:23 +05:30
});
});
2021-03-08 18:12:59 +05:30
});
});