debian-mirror-gitlab/spec/frontend/ide/components/error_message_spec.js

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

120 lines
3 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import { GlLoadingIcon } from '@gitlab/ui';
2022-04-04 11:22:00 +05:30
import { mount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
2019-12-21 20:55:43 +05:30
import Vuex from 'vuex';
import ErrorMessage from '~/ide/components/error_message.vue';
2022-04-04 11:22:00 +05:30
Vue.use(Vuex);
2019-12-21 20:55:43 +05:30
describe('IDE error message component', () => {
let wrapper;
const setErrorMessageMock = jest.fn();
2021-03-08 18:12:59 +05:30
const createComponent = (messageProps) => {
2019-12-21 20:55:43 +05:30
const fakeStore = new Vuex.Store({
actions: { setErrorMessage: setErrorMessageMock },
});
2020-03-13 15:44:24 +05:30
wrapper = mount(ErrorMessage, {
2019-12-21 20:55:43 +05:30
propsData: {
message: {
text: 'some text',
actionText: 'test action',
actionPayload: 'testActionPayload',
...messageProps,
},
},
store: fakeStore,
});
};
beforeEach(() => {
setErrorMessageMock.mockReset();
});
afterEach(() => {
wrapper.destroy();
wrapper = null;
});
2020-03-13 15:44:24 +05:30
const findDismissButton = () => wrapper.find('button[aria-label=Dismiss]');
const findActionButton = () => wrapper.find('button.gl-alert-action');
2019-12-21 20:55:43 +05:30
it('renders error message', () => {
const text = 'error message';
createComponent({ text });
expect(wrapper.text()).toContain(text);
});
2020-03-13 15:44:24 +05:30
it('clears error message on dismiss click', () => {
2019-12-21 20:55:43 +05:30
createComponent();
2020-03-13 15:44:24 +05:30
findDismissButton().trigger('click');
2019-12-21 20:55:43 +05:30
2020-11-24 15:15:51 +05:30
expect(setErrorMessageMock).toHaveBeenCalledWith(expect.any(Object), null);
2019-12-21 20:55:43 +05:30
});
describe('with action', () => {
let actionMock;
const message = {
actionText: 'test action',
actionPayload: 'testActionPayload',
};
beforeEach(() => {
actionMock = jest.fn().mockResolvedValue();
createComponent({
...message,
action: actionMock,
});
});
it('renders action button', () => {
2020-03-13 15:44:24 +05:30
const button = findActionButton();
2019-12-21 20:55:43 +05:30
expect(button.exists()).toBe(true);
expect(button.text()).toContain(message.actionText);
});
2020-03-13 15:44:24 +05:30
it('does not show dismiss button', () => {
expect(findDismissButton().exists()).toBe(false);
2019-12-21 20:55:43 +05:30
});
it('dispatches action', () => {
2020-03-13 15:44:24 +05:30
findActionButton().trigger('click');
2019-12-21 20:55:43 +05:30
expect(actionMock).toHaveBeenCalledWith(message.actionPayload);
});
2022-04-04 11:22:00 +05:30
it('does not dispatch action when already loading', async () => {
2020-03-13 15:44:24 +05:30
findActionButton().trigger('click');
2019-12-21 20:55:43 +05:30
actionMock.mockReset();
2022-04-04 11:22:00 +05:30
findActionButton().trigger('click');
await nextTick();
expect(actionMock).not.toHaveBeenCalled();
2019-12-21 20:55:43 +05:30
});
2022-04-04 11:22:00 +05:30
it('shows loading icon when loading', async () => {
2019-12-21 20:55:43 +05:30
let resolveAction;
actionMock.mockImplementation(
() =>
2021-03-08 18:12:59 +05:30
new Promise((resolve) => {
2019-12-21 20:55:43 +05:30
resolveAction = resolve;
}),
);
2020-03-13 15:44:24 +05:30
findActionButton().trigger('click');
2019-12-21 20:55:43 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
expect(wrapper.find(GlLoadingIcon).isVisible()).toBe(true);
resolveAction();
2019-12-21 20:55:43 +05:30
});
2022-04-04 11:22:00 +05:30
it('hides loading icon when operation finishes', async () => {
2020-03-13 15:44:24 +05:30
findActionButton().trigger('click');
2022-04-04 11:22:00 +05:30
await actionMock();
await nextTick();
expect(wrapper.find(GlLoadingIcon).isVisible()).toBe(false);
2019-12-21 20:55:43 +05:30
});
});
});