debian-mirror-gitlab/spec/frontend/__helpers__/vue_test_utils_helper_spec.js

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

359 lines
11 KiB
JavaScript
Raw Normal View History

2021-04-29 21:17:54 +05:30
import * as testingLibrary from '@testing-library/dom';
import * as vtu from '@vue/test-utils';
import {
shallowMount,
Wrapper as VTUWrapper,
WrapperArray as VTUWrapperArray,
2021-09-04 01:27:46 +05:30
ErrorWrapper as VTUErrorWrapper,
2021-04-29 21:17:54 +05:30
} from '@vue/test-utils';
2022-08-27 11:52:29 +05:30
import Vue from 'vue';
2021-04-29 21:17:54 +05:30
import {
extendedWrapper,
shallowMountExtended,
mountExtended,
shallowWrapperContainsSlotText,
} from './vue_test_utils_helper';
jest.mock('@testing-library/dom', () => ({
__esModule: true,
...jest.requireActual('@testing-library/dom'),
}));
2019-03-02 22:35:43 +05:30
describe('Vue test utils helpers', () => {
2021-04-29 21:17:54 +05:30
afterAll(() => {
jest.unmock('@testing-library/dom');
});
2019-03-02 22:35:43 +05:30
describe('shallowWrapperContainsSlotText', () => {
const mockText = 'text';
const mockSlot = `<div>${mockText}</div>`;
let mockComponent;
beforeEach(() => {
mockComponent = shallowMount(
{
render(h) {
h(`<div>mockedComponent</div>`);
},
},
{
slots: {
default: mockText,
namedSlot: mockSlot,
},
},
);
});
it('finds text within shallowWrapper default slot', () => {
expect(shallowWrapperContainsSlotText(mockComponent, 'default', mockText)).toBe(true);
});
it('finds text within shallowWrapper named slot', () => {
expect(shallowWrapperContainsSlotText(mockComponent, 'namedSlot', mockText)).toBe(true);
});
it('returns false when text is not present', () => {
const searchText = 'absent';
expect(shallowWrapperContainsSlotText(mockComponent, 'default', searchText)).toBe(false);
expect(shallowWrapperContainsSlotText(mockComponent, 'namedSlot', searchText)).toBe(false);
});
it('searches with case-sensitivity', () => {
const searchText = mockText.toUpperCase();
expect(shallowWrapperContainsSlotText(mockComponent, 'default', searchText)).toBe(false);
expect(shallowWrapperContainsSlotText(mockComponent, 'namedSlot', searchText)).toBe(false);
});
});
2021-02-22 17:27:13 +05:30
describe('extendedWrapper', () => {
describe('when an invalid wrapper is provided', () => {
beforeEach(() => {
// eslint-disable-next-line no-console
console.warn = jest.fn();
});
it.each`
wrapper
${{}}
${[]}
${null}
${undefined}
${1}
${''}
`('should warn with an error when the wrapper is $wrapper', ({ wrapper }) => {
extendedWrapper(wrapper);
/* eslint-disable no-console */
expect(console.warn).toHaveBeenCalled();
expect(console.warn).toHaveBeenCalledWith(
'[vue-test-utils-helper]: you are trying to extend an object that is not a VueWrapper.',
);
/* eslint-enable no-console */
});
});
describe('findByTestId', () => {
const testId = 'a-component';
let mockComponent;
beforeEach(() => {
mockComponent = extendedWrapper(
shallowMount({
template: `<div data-testid="${testId}"></div>`,
}),
);
});
2021-04-29 21:17:54 +05:30
it('should find the element by test id', () => {
2021-02-22 17:27:13 +05:30
expect(mockComponent.findByTestId(testId).exists()).toBe(true);
});
});
2021-04-17 20:07:23 +05:30
describe('findAllByTestId', () => {
const testId = 'a-component';
let mockComponent;
beforeEach(() => {
mockComponent = extendedWrapper(
shallowMount({
template: `<div><div data-testid="${testId}"></div><div data-testid="${testId}"></div></div>`,
}),
);
});
it('should find all components by test id', () => {
expect(mockComponent.findAllByTestId(testId)).toHaveLength(2);
});
});
2021-04-29 21:17:54 +05:30
describe.each`
findMethod | expectedQuery
${'findByRole'} | ${'queryAllByRole'}
${'findByLabelText'} | ${'queryAllByLabelText'}
${'findByPlaceholderText'} | ${'queryAllByPlaceholderText'}
${'findByText'} | ${'queryAllByText'}
${'findByDisplayValue'} | ${'queryAllByDisplayValue'}
${'findByAltText'} | ${'queryAllByAltText'}
`('$findMethod', ({ findMethod, expectedQuery }) => {
const text = 'foo bar';
const options = { selector: 'div' };
const mockDiv = document.createElement('div');
2022-11-25 23:54:43 +05:30
let mockVm;
2021-04-29 21:17:54 +05:30
let wrapper;
beforeEach(() => {
2022-08-27 11:52:29 +05:30
jest.spyOn(vtu, 'createWrapper');
2022-11-25 23:54:43 +05:30
mockVm = new Vue({ render: (h) => h('div') }).$mount();
2022-08-27 11:52:29 +05:30
2021-04-29 21:17:54 +05:30
wrapper = extendedWrapper(
shallowMount({
template: `<div>foo bar</div>`,
}),
);
});
it(`calls Testing Library \`${expectedQuery}\` function with correct parameters`, () => {
jest.spyOn(testingLibrary, expectedQuery).mockImplementation(() => [mockDiv]);
wrapper[findMethod](text, options);
expect(testingLibrary[expectedQuery]).toHaveBeenLastCalledWith(
wrapper.element,
text,
options,
);
});
describe('when element is found', () => {
beforeEach(() => {
jest.spyOn(testingLibrary, expectedQuery).mockImplementation(() => [mockDiv]);
});
it('returns a VTU wrapper', () => {
const result = wrapper[findMethod](text, options);
expect(vtu.createWrapper).toHaveBeenCalledWith(mockDiv, wrapper.options);
expect(result).toBeInstanceOf(VTUWrapper);
2022-08-27 11:52:29 +05:30
expect(result.vm).toBeUndefined();
2021-04-29 21:17:54 +05:30
});
});
2022-08-27 11:52:29 +05:30
describe('when a Vue instance element is found', () => {
beforeEach(() => {
jest.spyOn(testingLibrary, expectedQuery).mockImplementation(() => [mockVm.$el]);
});
it('returns a VTU wrapper', () => {
const result = wrapper[findMethod](text, options);
expect(vtu.createWrapper).toHaveBeenCalledWith(mockVm, wrapper.options);
expect(result).toBeInstanceOf(VTUWrapper);
expect(result.vm).toBeInstanceOf(Vue);
});
});
2021-04-29 21:17:54 +05:30
describe('when multiple elements are found', () => {
beforeEach(() => {
const mockSpan = document.createElement('span');
jest.spyOn(testingLibrary, expectedQuery).mockImplementation(() => [mockDiv, mockSpan]);
});
it('returns the first element as a VTU wrapper', () => {
const result = wrapper[findMethod](text, options);
expect(vtu.createWrapper).toHaveBeenCalledWith(mockDiv, wrapper.options);
expect(result).toBeInstanceOf(VTUWrapper);
2022-08-27 11:52:29 +05:30
expect(result.vm).toBeUndefined();
});
});
describe('when multiple Vue instances are found', () => {
beforeEach(() => {
const mockVm2 = new Vue({ render: (h) => h('span') }).$mount();
jest
.spyOn(testingLibrary, expectedQuery)
.mockImplementation(() => [mockVm.$el, mockVm2.$el]);
});
it('returns the first element as a VTU wrapper', () => {
const result = wrapper[findMethod](text, options);
expect(vtu.createWrapper).toHaveBeenCalledWith(mockVm, wrapper.options);
expect(result).toBeInstanceOf(VTUWrapper);
expect(result.vm).toBeInstanceOf(Vue);
2021-04-29 21:17:54 +05:30
});
});
describe('when element is not found', () => {
beforeEach(() => {
jest.spyOn(testingLibrary, expectedQuery).mockImplementation(() => []);
});
it('returns a VTU error wrapper', () => {
2021-09-04 01:27:46 +05:30
expect(wrapper[findMethod](text, options)).toBeInstanceOf(VTUErrorWrapper);
2021-04-29 21:17:54 +05:30
});
});
});
describe.each`
findMethod | expectedQuery
${'findAllByRole'} | ${'queryAllByRole'}
${'findAllByLabelText'} | ${'queryAllByLabelText'}
${'findAllByPlaceholderText'} | ${'queryAllByPlaceholderText'}
${'findAllByText'} | ${'queryAllByText'}
${'findAllByDisplayValue'} | ${'queryAllByDisplayValue'}
${'findAllByAltText'} | ${'queryAllByAltText'}
`('$findMethod', ({ findMethod, expectedQuery }) => {
const text = 'foo bar';
2022-08-27 11:52:29 +05:30
const options = { selector: 'li' };
2021-04-29 21:17:54 +05:30
const mockElements = [
document.createElement('li'),
document.createElement('li'),
document.createElement('li'),
];
2022-08-27 11:52:29 +05:30
const mockVms = [
new Vue({ render: (h) => h('li') }).$mount(),
new Vue({ render: (h) => h('li') }).$mount(),
new Vue({ render: (h) => h('li') }).$mount(),
];
2021-04-29 21:17:54 +05:30
let wrapper;
beforeEach(() => {
wrapper = extendedWrapper(
shallowMount({
template: `
<ul>
<li>foo</li>
<li>bar</li>
<li>baz</li>
</ul>
`,
}),
);
});
it(`calls Testing Library \`${expectedQuery}\` function with correct parameters`, () => {
jest.spyOn(testingLibrary, expectedQuery).mockImplementation(() => mockElements);
wrapper[findMethod](text, options);
expect(testingLibrary[expectedQuery]).toHaveBeenLastCalledWith(
wrapper.element,
text,
options,
);
});
2022-08-27 11:52:29 +05:30
describe.each`
case | mockResult | isVueInstance
${'HTMLElements'} | ${mockElements} | ${false}
${'Vue instance elements'} | ${mockVms} | ${true}
`('when $case are found', ({ mockResult, isVueInstance }) => {
2021-04-29 21:17:54 +05:30
beforeEach(() => {
2022-08-27 11:52:29 +05:30
jest.spyOn(testingLibrary, expectedQuery).mockImplementation(() => mockResult);
2021-04-29 21:17:54 +05:30
});
it('returns a VTU wrapper array', () => {
const result = wrapper[findMethod](text, options);
expect(result).toBeInstanceOf(VTUWrapperArray);
expect(
result.wrappers.every(
(resultWrapper) =>
2022-08-27 11:52:29 +05:30
resultWrapper instanceof VTUWrapper &&
resultWrapper.vm instanceof Vue === isVueInstance &&
resultWrapper.options === wrapper.options,
2021-04-29 21:17:54 +05:30
),
).toBe(true);
expect(result.length).toBe(3);
});
});
describe('when elements are not found', () => {
beforeEach(() => {
jest.spyOn(testingLibrary, expectedQuery).mockImplementation(() => []);
});
it('returns an empty VTU wrapper array', () => {
const result = wrapper[findMethod](text, options);
expect(result).toBeInstanceOf(VTUWrapperArray);
expect(result.length).toBe(0);
});
});
});
});
describe.each`
mountExtendedFunction | expectedMountFunction
${shallowMountExtended} | ${'shallowMount'}
${mountExtended} | ${'mount'}
`('$mountExtendedFunction', ({ mountExtendedFunction, expectedMountFunction }) => {
const FakeComponent = jest.fn();
const options = {
propsData: {
foo: 'bar',
},
};
beforeEach(() => {
const mockWrapper = { find: jest.fn() };
jest.spyOn(vtu, expectedMountFunction).mockImplementation(() => mockWrapper);
});
it(`calls \`${expectedMountFunction}\` with passed arguments`, () => {
mountExtendedFunction(FakeComponent, options);
expect(vtu[expectedMountFunction]).toHaveBeenCalledWith(FakeComponent, options);
});
it('returns extended wrapper', () => {
const result = mountExtendedFunction(FakeComponent, options);
expect(result).toHaveProperty('find');
expect(result).toHaveProperty('findByTestId');
});
2021-02-22 17:27:13 +05:30
});
2019-03-02 22:35:43 +05:30
});