2021-01-29 00:20:46 +05:30
|
|
|
import { GlModal } from '@gitlab/ui';
|
2018-05-09 12:01:36 +05:30
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2023-05-27 22:25:52 +05:30
|
|
|
import Vue, { nextTick } from 'vue';
|
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2020-04-22 19:07:51 +05:30
|
|
|
import { TEST_HOST } from 'helpers/test_constants';
|
2023-05-27 22:25:52 +05:30
|
|
|
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
|
|
|
|
import { createAlert } from '~/alert';
|
2020-01-01 13:55:28 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2023-04-23 21:23:45 +05:30
|
|
|
import { HTTP_STATUS_BAD_REQUEST, HTTP_STATUS_OK } from '~/lib/utils/http_status';
|
2021-01-29 00:20:46 +05:30
|
|
|
import UpdateUsername from '~/profile/account/components/update_username.vue';
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
jest.mock('~/alert');
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
describe('UpdateUsername component', () => {
|
2020-04-22 19:07:51 +05:30
|
|
|
const rootUrl = TEST_HOST;
|
|
|
|
const actionUrl = `${TEST_HOST}/update/username`;
|
2021-01-29 00:20:46 +05:30
|
|
|
const defaultProps = {
|
|
|
|
actionUrl,
|
|
|
|
rootUrl,
|
|
|
|
initialUsername: 'hasnoname',
|
|
|
|
};
|
|
|
|
let wrapper;
|
2018-05-09 12:01:36 +05:30
|
|
|
let axiosMock;
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
const findNewUsernameInput = () => wrapper.findByTestId('new-username-input');
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
const createComponent = (props = {}) => {
|
2023-05-27 22:25:52 +05:30
|
|
|
wrapper = shallowMountExtended(UpdateUsername, {
|
2021-01-29 00:20:46 +05:30
|
|
|
propsData: {
|
|
|
|
...defaultProps,
|
|
|
|
...props,
|
|
|
|
},
|
|
|
|
stubs: {
|
|
|
|
GlModal,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
beforeEach(() => {
|
|
|
|
axiosMock = new MockAdapter(axios);
|
2021-01-29 00:20:46 +05:30
|
|
|
createComponent();
|
2018-05-09 12:01:36 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
axiosMock.restore();
|
2023-05-27 22:25:52 +05:30
|
|
|
Vue.config.errorHandler = null;
|
2018-05-09 12:01:36 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
const findElements = () => {
|
2022-10-11 01:57:18 +05:30
|
|
|
const modal = wrapper.findComponent(GlModal);
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
return {
|
2021-01-29 00:20:46 +05:30
|
|
|
modal,
|
|
|
|
input: wrapper.find(`#${wrapper.vm.$options.inputId}`),
|
|
|
|
openModalBtn: wrapper.find('[data-testid="username-change-confirmation-modal"]'),
|
|
|
|
modalBody: modal.find('.modal-body'),
|
|
|
|
modalHeader: modal.find('.modal-title'),
|
2022-07-23 23:45:48 +05:30
|
|
|
confirmModalBtn: wrapper.find('.btn-confirm'),
|
2018-05-09 12:01:36 +05:30
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
const clickModalWithErrorResponse = () => {
|
|
|
|
Vue.config.errorHandler = jest.fn(); // silence thrown error
|
|
|
|
const { modal } = findElements();
|
|
|
|
modal.vm.$emit('primary');
|
|
|
|
return waitForPromises();
|
|
|
|
};
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
it('has a disabled button if the username was not changed', async () => {
|
|
|
|
const { openModalBtn } = findElements();
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
await nextTick();
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
expect(openModalBtn.props('disabled')).toBe(true);
|
2018-05-09 12:01:36 +05:30
|
|
|
});
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
it('has an enabled button which if the username was changed', async () => {
|
2018-05-09 12:01:36 +05:30
|
|
|
const { input, openModalBtn } = findElements();
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
input.element.value = 'newUsername';
|
|
|
|
input.trigger('input');
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
await nextTick();
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
expect(openModalBtn.props('disabled')).toBe(false);
|
2018-05-09 12:01:36 +05:30
|
|
|
});
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
describe('changing username', () => {
|
|
|
|
const newUsername = 'new_username';
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
beforeEach(async () => {
|
|
|
|
createComponent();
|
2023-05-27 22:25:52 +05:30
|
|
|
await findNewUsernameInput().setValue(newUsername);
|
2021-01-29 00:20:46 +05:30
|
|
|
});
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
it('confirmation modal contains proper header and body', async () => {
|
|
|
|
const { modal } = findElements();
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
expect(modal.props('title')).toBe('Change username?');
|
2021-01-29 00:20:46 +05:30
|
|
|
expect(modal.text()).toContain(
|
|
|
|
`You are going to change the username ${defaultProps.initialUsername} to ${newUsername}`,
|
|
|
|
);
|
|
|
|
});
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
it('executes API call on confirmation button click', async () => {
|
2023-04-23 21:23:45 +05:30
|
|
|
axiosMock.onPut(actionUrl).replyOnce(() => [HTTP_STATUS_OK, { message: 'Username changed' }]);
|
2021-01-29 00:20:46 +05:30
|
|
|
jest.spyOn(axios, 'put');
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
const { modal } = findElements();
|
|
|
|
modal.vm.$emit('primary');
|
|
|
|
await waitForPromises();
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
expect(axios.put).toHaveBeenCalledWith(actionUrl, { user: { username: newUsername } });
|
|
|
|
});
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
it('sets the username after a successful update', async () => {
|
2023-05-27 22:25:52 +05:30
|
|
|
const { input, openModalBtn, modal } = findElements();
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
axiosMock.onPut(actionUrl).replyOnce(() => {
|
|
|
|
expect(input.attributes('disabled')).toBe('disabled');
|
2021-04-17 20:07:23 +05:30
|
|
|
expect(openModalBtn.props('disabled')).toBe(false);
|
|
|
|
expect(openModalBtn.props('loading')).toBe(true);
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
return [HTTP_STATUS_OK, { message: 'Username changed' }];
|
2021-01-29 00:20:46 +05:30
|
|
|
});
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
modal.vm.$emit('primary');
|
|
|
|
await waitForPromises();
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
expect(input.attributes('disabled')).toBe(undefined);
|
|
|
|
expect(openModalBtn.props('disabled')).toBe(true);
|
2021-04-17 20:07:23 +05:30
|
|
|
expect(openModalBtn.props('loading')).toBe(false);
|
2018-05-09 12:01:36 +05:30
|
|
|
});
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
it('does not set the username after a erroneous update', async () => {
|
|
|
|
const { input, openModalBtn } = findElements();
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
axiosMock.onPut(actionUrl).replyOnce(() => {
|
|
|
|
expect(input.attributes('disabled')).toBe('disabled');
|
2021-04-17 20:07:23 +05:30
|
|
|
expect(openModalBtn.props('disabled')).toBe(false);
|
|
|
|
expect(openModalBtn.props('loading')).toBe(true);
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
return [HTTP_STATUS_BAD_REQUEST, { message: 'Invalid username' }];
|
2021-01-29 00:20:46 +05:30
|
|
|
});
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
await clickModalWithErrorResponse();
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
expect(input.attributes('disabled')).toBe(undefined);
|
|
|
|
expect(openModalBtn.props('disabled')).toBe(false);
|
2021-04-17 20:07:23 +05:30
|
|
|
expect(openModalBtn.props('loading')).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows an error message if the error response has a `message` property', async () => {
|
|
|
|
axiosMock.onPut(actionUrl).replyOnce(() => {
|
2023-04-23 21:23:45 +05:30
|
|
|
return [HTTP_STATUS_BAD_REQUEST, { message: 'Invalid username' }];
|
2021-04-17 20:07:23 +05:30
|
|
|
});
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
await clickModalWithErrorResponse();
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(createAlert).toHaveBeenCalledWith({
|
2021-09-04 01:27:46 +05:30
|
|
|
message: 'Invalid username',
|
|
|
|
});
|
2021-04-17 20:07:23 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it("shows a fallback error message if the error response doesn't have a `message` property", async () => {
|
|
|
|
axiosMock.onPut(actionUrl).replyOnce(() => {
|
2023-04-23 21:23:45 +05:30
|
|
|
return [HTTP_STATUS_BAD_REQUEST];
|
2021-04-17 20:07:23 +05:30
|
|
|
});
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
await clickModalWithErrorResponse();
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(createAlert).toHaveBeenCalledWith({
|
2021-09-04 01:27:46 +05:30
|
|
|
message: 'An error occurred while updating your username, please try again.',
|
|
|
|
});
|
2018-05-09 12:01:36 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|