2023-03-04 22:38:38 +05:30
|
|
|
import { mount, createWrapper } from '@vue/test-utils';
|
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import { getByText as getByTextHelper } from '@testing-library/dom';
|
2023-05-27 22:25:52 +05:30
|
|
|
import { GlDisclosureDropdownItem, GlToggle } from '@gitlab/ui';
|
2023-03-04 22:38:38 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2023-04-23 21:23:45 +05:30
|
|
|
import { HTTP_STATUS_INTERNAL_SERVER_ERROR, HTTP_STATUS_OK } from '~/lib/utils/http_status';
|
2023-03-04 22:38:38 +05:30
|
|
|
import { useMockLocationHelper } from 'helpers/mock_window_location_helper';
|
|
|
|
import NewNavToggle from '~/nav/components/new_nav_toggle.vue';
|
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2023-05-27 22:25:52 +05:30
|
|
|
import { createAlert } from '~/alert';
|
2023-03-04 22:38:38 +05:30
|
|
|
import { s__ } from '~/locale';
|
2023-06-20 00:43:36 +05:30
|
|
|
import { mockTracking } from 'helpers/tracking_helper';
|
2023-03-04 22:38:38 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
jest.mock('~/alert');
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
const TEST_ENDPONT = 'https://example.com/toggle';
|
|
|
|
|
|
|
|
describe('NewNavToggle', () => {
|
2023-03-17 16:20:25 +05:30
|
|
|
useMockLocationHelper();
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
let wrapper;
|
2023-06-20 00:43:36 +05:30
|
|
|
let trackingSpy;
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
const findToggle = () => wrapper.findComponent(GlToggle);
|
2023-05-27 22:25:52 +05:30
|
|
|
const findDisclosureItem = () => wrapper.findComponent(GlDisclosureDropdownItem);
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
const createComponent = (propsData = { enabled: false }) => {
|
|
|
|
wrapper = mount(NewNavToggle, {
|
|
|
|
propsData: {
|
|
|
|
endpoint: TEST_ENDPONT,
|
|
|
|
...propsData,
|
|
|
|
},
|
|
|
|
});
|
2023-06-20 00:43:36 +05:30
|
|
|
|
|
|
|
trackingSpy = mockTracking(undefined, wrapper.element, jest.spyOn);
|
2023-03-04 22:38:38 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
const getByText = (text, options) =>
|
|
|
|
createWrapper(getByTextHelper(wrapper.element, text, options));
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
describe('When rendered in scope of the new navigation', () => {
|
|
|
|
it('renders the disclosure item', () => {
|
|
|
|
createComponent({ newNavigation: true, enabled: true });
|
|
|
|
expect(findDisclosureItem().exists()).toBe(true);
|
|
|
|
});
|
2023-03-04 22:38:38 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
describe('when user preference is enabled', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
createComponent({ newNavigation: true, enabled: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders the toggle as enabled', () => {
|
|
|
|
expect(findToggle().props('value')).toBe(true);
|
|
|
|
});
|
2023-03-04 22:38:38 +05:30
|
|
|
});
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
describe('when user preference is disabled', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
createComponent({ enabled: false });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders the toggle as disabled', () => {
|
|
|
|
expect(findToggle().props('value')).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe.each`
|
2023-06-20 00:43:36 +05:30
|
|
|
desc | actFn | toggleValue | trackingLabel | trackingProperty
|
|
|
|
${'when toggle button is clicked'} | ${() => findToggle().trigger('click')} | ${false} | ${'enable_new_nav_beta'} | ${'navigation_top'}
|
|
|
|
${'when menu item text is clicked'} | ${() => getByText('New navigation').trigger('click')} | ${false} | ${'enable_new_nav_beta'} | ${'navigation_top'}
|
|
|
|
${'when toggle button is clicked'} | ${() => findToggle().trigger('click')} | ${true} | ${'disable_new_nav_beta'} | ${'nav_user_menu'}
|
|
|
|
${'when menu item text is clicked'} | ${() => getByText('New navigation').trigger('click')} | ${true} | ${'disable_new_nav_beta'} | ${'nav_user_menu'}
|
|
|
|
`('$desc', ({ actFn, toggleValue, trackingLabel, trackingProperty }) => {
|
2023-05-27 22:25:52 +05:30
|
|
|
let mock;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
2023-06-20 00:43:36 +05:30
|
|
|
createComponent({ enabled: toggleValue });
|
2023-05-27 22:25:52 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('reloads the page on success', async () => {
|
|
|
|
mock.onPut(TEST_ENDPONT).reply(HTTP_STATUS_OK);
|
|
|
|
|
|
|
|
actFn();
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(window.location.reload).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows an alert on error', async () => {
|
|
|
|
mock.onPut(TEST_ENDPONT).reply(HTTP_STATUS_INTERNAL_SERVER_ERROR);
|
|
|
|
|
|
|
|
actFn();
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(createAlert).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining({
|
|
|
|
message: s__(
|
|
|
|
'NorthstarNavigation|Could not update the new navigation preference. Please try again later.',
|
|
|
|
),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
expect(window.location.reload).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('changes the toggle', async () => {
|
|
|
|
await actFn();
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
expect(findToggle().props('value')).toBe(!toggleValue);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('tracks the Snowplow event', async () => {
|
|
|
|
mock.onPut(TEST_ENDPONT).reply(HTTP_STATUS_OK);
|
|
|
|
await actFn();
|
|
|
|
await waitForPromises();
|
|
|
|
expect(trackingSpy).toHaveBeenCalledWith(undefined, 'click_toggle', {
|
|
|
|
label: trackingLabel,
|
|
|
|
property: trackingProperty,
|
|
|
|
});
|
2023-05-27 22:25:52 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
});
|
2023-03-04 22:38:38 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
describe('When rendered in scope of the current navigation', () => {
|
|
|
|
it('renders its title', () => {
|
|
|
|
createComponent();
|
|
|
|
expect(getByText('Navigation redesign').exists()).toBe(true);
|
2023-03-04 22:38:38 +05:30
|
|
|
});
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
describe('when user preference is enabled', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
createComponent({ enabled: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders the toggle as enabled', () => {
|
|
|
|
expect(findToggle().props('value')).toBe(true);
|
|
|
|
});
|
2023-03-04 22:38:38 +05:30
|
|
|
});
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
describe('when user preference is disabled', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
createComponent({ enabled: false });
|
|
|
|
});
|
2023-03-04 22:38:38 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
it('renders the toggle as disabled', () => {
|
|
|
|
expect(findToggle().props('value')).toBe(false);
|
|
|
|
});
|
2023-03-04 22:38:38 +05:30
|
|
|
});
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
describe.each`
|
2023-06-20 00:43:36 +05:30
|
|
|
desc | actFn | toggleValue | trackingLabel | trackingProperty
|
|
|
|
${'when toggle button is clicked'} | ${() => findToggle().trigger('click')} | ${false} | ${'enable_new_nav_beta'} | ${'navigation_top'}
|
|
|
|
${'when menu item text is clicked'} | ${() => getByText('New navigation').trigger('click')} | ${false} | ${'enable_new_nav_beta'} | ${'navigation_top'}
|
|
|
|
${'when toggle button is clicked'} | ${() => findToggle().trigger('click')} | ${true} | ${'disable_new_nav_beta'} | ${'nav_user_menu'}
|
|
|
|
${'when menu item text is clicked'} | ${() => getByText('New navigation').trigger('click')} | ${true} | ${'disable_new_nav_beta'} | ${'nav_user_menu'}
|
|
|
|
`('$desc', ({ actFn, toggleValue, trackingLabel, trackingProperty }) => {
|
2023-05-27 22:25:52 +05:30
|
|
|
let mock;
|
2023-03-17 16:20:25 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
2023-06-20 00:43:36 +05:30
|
|
|
createComponent({ enabled: toggleValue });
|
2023-05-27 22:25:52 +05:30
|
|
|
});
|
2023-03-04 22:38:38 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
it('reloads the page on success', async () => {
|
|
|
|
mock.onPut(TEST_ENDPONT).reply(HTTP_STATUS_OK);
|
2023-03-04 22:38:38 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
actFn();
|
|
|
|
await waitForPromises();
|
2023-03-17 16:20:25 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
expect(window.location.reload).toHaveBeenCalled();
|
|
|
|
});
|
2023-03-04 22:38:38 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
it('shows an alert on error', async () => {
|
|
|
|
mock.onPut(TEST_ENDPONT).reply(HTTP_STATUS_INTERNAL_SERVER_ERROR);
|
2023-03-04 22:38:38 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
actFn();
|
|
|
|
await waitForPromises();
|
2023-03-17 16:20:25 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
expect(createAlert).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining({
|
|
|
|
message: s__(
|
|
|
|
'NorthstarNavigation|Could not update the new navigation preference. Please try again later.',
|
|
|
|
),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
expect(window.location.reload).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('changes the toggle', async () => {
|
|
|
|
await actFn();
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
expect(findToggle().props('value')).toBe(!toggleValue);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('tracks the Snowplow event', async () => {
|
|
|
|
mock.onPut(TEST_ENDPONT).reply(HTTP_STATUS_OK);
|
|
|
|
await actFn();
|
|
|
|
await waitForPromises();
|
|
|
|
expect(trackingSpy).toHaveBeenCalledWith(undefined, 'click_toggle', {
|
|
|
|
label: trackingLabel,
|
|
|
|
property: trackingProperty,
|
|
|
|
});
|
2023-05-27 22:25:52 +05:30
|
|
|
});
|
2023-03-17 16:20:25 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
});
|
2023-03-04 22:38:38 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|