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

128 lines
3.2 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import { GlLoadingIcon } from '@gitlab/ui';
2020-03-13 15:44:24 +05:30
import { mount, createLocalVue } from '@vue/test-utils';
2019-12-21 20:55:43 +05:30
import Vuex from 'vuex';
import ErrorMessage from '~/ide/components/error_message.vue';
const localVue = createLocalVue();
localVue.use(Vuex);
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,
localVue,
});
};
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);
});
it('does not dispatch action when already loading', () => {
2020-03-13 15:44:24 +05:30
findActionButton().trigger('click');
2019-12-21 20:55:43 +05:30
actionMock.mockReset();
2020-03-13 15:44:24 +05:30
return wrapper.vm.$nextTick(() => {
findActionButton().trigger('click');
return wrapper.vm.$nextTick().then(() => {
expect(actionMock).not.toHaveBeenCalled();
});
});
2019-12-21 20:55:43 +05:30
});
it('shows loading icon when loading', () => {
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
return wrapper.vm.$nextTick(() => {
expect(wrapper.find(GlLoadingIcon).isVisible()).toBe(true);
resolveAction();
});
});
it('hides loading icon when operation finishes', () => {
2020-03-13 15:44:24 +05:30
findActionButton().trigger('click');
2019-12-21 20:55:43 +05:30
return actionMock()
.then(() => wrapper.vm.$nextTick())
.then(() => {
expect(wrapper.find(GlLoadingIcon).isVisible()).toBe(false);
});
});
});
});