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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

235 lines
8.1 KiB
JavaScript
Raw Normal View History

2022-04-04 11:22:00 +05:30
import { GlLink } from '@gitlab/ui';
import { nextTick } from 'vue';
import { mountExtended, shallowMountExtended } from 'helpers/vue_test_utils_helper';
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';
2022-07-16 23:28:13 +05:30
import SignInPage from '~/jira_connect/subscriptions/pages/sign_in/sign_in_page.vue';
import SubscriptionsPage from '~/jira_connect/subscriptions/pages/subscriptions_page.vue';
2022-01-26 12:08:38 +05:30
import UserLink from '~/jira_connect/subscriptions/components/user_link.vue';
2022-06-21 17:19:12 +05:30
import BrowserSupportAlert from '~/jira_connect/subscriptions/components/browser_support_alert.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';
2022-05-07 20:08:51 +05:30
import { I18N_DEFAULT_SIGN_IN_ERROR_MESSAGE } from '~/jira_connect/subscriptions/constants';
2021-04-17 20:07:23 +05:30
import { __ } from '~/locale';
2022-06-21 17:19:12 +05:30
import AccessorUtilities from '~/lib/utils/accessor';
2022-07-16 23:28:13 +05:30
import * as api from '~/jira_connect/subscriptions/api';
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;
2022-04-04 11:22:00 +05:30
const findAlert = () => wrapper.findByTestId('jira-connect-persisted-alert');
2021-04-29 21:17:54 +05:30
const findAlertLink = () => findAlert().findComponent(GlLink);
2022-04-04 11:22:00 +05:30
const findSignInPage = () => wrapper.findComponent(SignInPage);
const findSubscriptionsPage = () => wrapper.findComponent(SubscriptionsPage);
2022-05-07 20:08:51 +05:30
const findUserLink = () => wrapper.findComponent(UserLink);
2022-06-21 17:19:12 +05:30
const findBrowserSupportAlert = () => wrapper.findComponent(BrowserSupportAlert);
2021-03-08 18:12:59 +05:30
2022-10-11 01:57:18 +05:30
const createComponent = ({ provide, mountFn = shallowMountExtended, initialState = {} } = {}) => {
store = createStore({ ...initialState, subscriptions: [mockSubscription] });
2022-07-16 23:28:13 +05:30
jest.spyOn(store, 'dispatch').mockImplementation();
2021-03-08 18:12:59 +05:30
2021-04-29 21:17:54 +05:30
wrapper = mountFn(JiraConnectApp, {
store,
provide,
});
2021-03-08 18:12:59 +05:30
};
describe('template', () => {
2021-12-11 22:18:48 +05:30
describe.each`
2022-04-04 11:22:00 +05:30
scenario | usersPath | shouldRenderSignInPage | shouldRenderSubscriptionsPage
${'user is not signed in'} | ${'/users'} | ${true} | ${false}
${'user is signed in'} | ${undefined} | ${false} | ${true}
`('when $scenario', ({ usersPath, shouldRenderSignInPage, shouldRenderSubscriptionsPage }) => {
beforeEach(() => {
createComponent({
provide: {
usersPath,
},
2021-12-11 22:18:48 +05:30
});
2022-04-04 11:22:00 +05:30
});
2021-03-11 19:13:27 +05:30
2022-04-04 11:22:00 +05:30
it(`${shouldRenderSignInPage ? 'renders' : 'does not render'} sign in page`, () => {
2022-10-11 01:57:18 +05:30
expect(findSignInPage().isVisible()).toBe(shouldRenderSignInPage);
2022-04-04 11:22:00 +05:30
if (shouldRenderSignInPage) {
expect(findSignInPage().props('hasSubscriptions')).toBe(true);
}
});
2021-12-11 22:18:48 +05:30
2022-04-04 11:22:00 +05:30
it(`${
shouldRenderSubscriptionsPage ? 'renders' : 'does not render'
} subscriptions page`, () => {
expect(findSubscriptionsPage().exists()).toBe(shouldRenderSubscriptionsPage);
if (shouldRenderSubscriptionsPage) {
expect(findSubscriptionsPage().props('hasSubscriptions')).toBe(true);
}
});
});
2022-01-26 12:08:38 +05:30
it('renders UserLink component', () => {
createComponent({
provide: {
usersPath: '/user',
},
});
2022-05-07 20:08:51 +05:30
const userLink = findUserLink();
2022-01-26 12:08:38 +05:30
expect(userLink.exists()).toBe(true);
expect(userLink.props()).toEqual({
2022-07-16 23:28:13 +05:30
hasSubscriptions: true,
2022-05-07 20:08:51 +05:30
user: null,
2022-01-26 12:08:38 +05:30
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 });
2022-04-04 11:22:00 +05:30
await nextTick();
2021-04-17 20:07:23 +05:30
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' });
2022-04-04 11:22:00 +05:30
await nextTick();
2021-12-11 22:18:48 +05:30
findAlert().vm.$emit('dismiss');
2022-04-04 11:22:00 +05:30
await nextTick();
2021-12-11 22:18:48 +05:30
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 () => {
2022-10-11 01:57:18 +05:30
createComponent({ provide: { usersPath: '' }, mountFn: mountExtended });
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
});
2022-04-04 11:22:00 +05:30
await 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
});
2022-05-07 20:08:51 +05:30
describe('when user signed out', () => {
describe('when sign in page emits `error` event', () => {
beforeEach(async () => {
createComponent({
provide: {
usersPath: '/mock',
},
});
findSignInPage().vm.$emit('error');
await nextTick();
});
it('displays alert', () => {
const alert = findAlert();
expect(alert.exists()).toBe(true);
expect(alert.html()).toContain(I18N_DEFAULT_SIGN_IN_ERROR_MESSAGE);
expect(alert.props('variant')).toBe('danger');
});
});
});
2022-06-21 17:19:12 +05:30
describe.each`
jiraConnectOauthEnabled | canUseCrypto | shouldShowAlert
${false} | ${false} | ${false}
${false} | ${true} | ${false}
${true} | ${false} | ${true}
${true} | ${true} | ${false}
`(
'when `jiraConnectOauth` feature flag is $jiraConnectOauthEnabled and `AccessorUtilities.canUseCrypto` returns $canUseCrypto',
({ jiraConnectOauthEnabled, canUseCrypto, shouldShowAlert }) => {
beforeEach(() => {
jest.spyOn(AccessorUtilities, 'canUseCrypto').mockReturnValue(canUseCrypto);
createComponent({ provide: { glFeatures: { jiraConnectOauth: jiraConnectOauthEnabled } } });
});
it(`does ${shouldShowAlert ? '' : 'not'} render BrowserSupportAlert component`, () => {
expect(findBrowserSupportAlert().exists()).toBe(shouldShowAlert);
});
it(`does ${!shouldShowAlert ? '' : 'not'} render the main Jira Connect app template`, () => {
expect(wrapper.findByTestId('jira-connect-app').exists()).toBe(!shouldShowAlert);
});
},
);
2022-07-16 23:28:13 +05:30
describe('when `jiraConnectOauth` feature flag is enabled', () => {
const mockSubscriptionsPath = '/mockSubscriptionsPath';
2022-10-11 01:57:18 +05:30
beforeEach(async () => {
2022-07-16 23:28:13 +05:30
jest.spyOn(api, 'fetchSubscriptions').mockResolvedValue({ data: { subscriptions: [] } });
2022-10-11 01:57:18 +05:30
jest.spyOn(AccessorUtilities, 'canUseCrypto').mockReturnValue(true);
2022-07-16 23:28:13 +05:30
createComponent({
2022-10-11 01:57:18 +05:30
initialState: {
currentUser: { name: 'root' },
},
2022-07-16 23:28:13 +05:30
provide: {
glFeatures: { jiraConnectOauth: true },
subscriptionsPath: mockSubscriptionsPath,
},
});
2022-10-11 01:57:18 +05:30
findSignInPage().vm.$emit('sign-in-oauth');
await nextTick();
2022-07-16 23:28:13 +05:30
});
describe('when oauth button emits `sign-in-oauth` event', () => {
it('dispatches `fetchSubscriptions` action', () => {
expect(store.dispatch).toHaveBeenCalledWith('fetchSubscriptions', mockSubscriptionsPath);
});
});
});
2021-03-08 18:12:59 +05:30
});