Commit 5b3d25fa authored by Kushal Pandya's avatar Kushal Pandya

Merge branch 'cngo-add-base-token-loading-test' into 'master'

Add loading spec for BaseToken

See merge request gitlab-org/gitlab!78896
parents 7a4ac73d 8dc93211
import { GlFilteredSearchToken } from '@gitlab/ui'; import { GlFilteredSearchToken, GlLoadingIcon } from '@gitlab/ui';
import { mount } from '@vue/test-utils'; import { mount } from '@vue/test-utils';
import { import {
mockRegularLabel, mockRegularLabel,
...@@ -61,13 +61,10 @@ const mockProps = { ...@@ -61,13 +61,10 @@ const mockProps = {
getActiveTokenValue: (labels, data) => labels.find((label) => label.title === data), getActiveTokenValue: (labels, data) => labels.find((label) => label.title === data),
}; };
function createComponent({ function createComponent({ props = {}, stubs = defaultStubs, slots = defaultSlots } = {}) {
props = { ...mockProps },
stubs = defaultStubs,
slots = defaultSlots,
} = {}) {
return mount(BaseToken, { return mount(BaseToken, {
propsData: { propsData: {
...mockProps,
...props, ...props,
}, },
provide: { provide: {
...@@ -83,15 +80,7 @@ function createComponent({ ...@@ -83,15 +80,7 @@ function createComponent({
describe('BaseToken', () => { describe('BaseToken', () => {
let wrapper; let wrapper;
beforeEach(() => { const findGlFilteredSearchToken = () => wrapper.findComponent(GlFilteredSearchToken);
wrapper = createComponent({
props: {
...mockProps,
value: { data: `"${mockRegularLabel.title}"` },
suggestions: mockLabels,
},
});
});
afterEach(() => { afterEach(() => {
wrapper.destroy(); wrapper.destroy();
...@@ -99,21 +88,25 @@ describe('BaseToken', () => { ...@@ -99,21 +88,25 @@ describe('BaseToken', () => {
describe('data', () => { describe('data', () => {
it('calls `getRecentlyUsedSuggestions` to populate `recentSuggestions` when `recentSuggestionsStorageKey` is defined', () => { it('calls `getRecentlyUsedSuggestions` to populate `recentSuggestions` when `recentSuggestionsStorageKey` is defined', () => {
wrapper = createComponent();
expect(getRecentlyUsedSuggestions).toHaveBeenCalledWith(mockStorageKey); expect(getRecentlyUsedSuggestions).toHaveBeenCalledWith(mockStorageKey);
}); });
}); });
describe('computed', () => { describe('computed', () => {
describe('activeTokenValue', () => { describe('activeTokenValue', () => {
it('calls `getActiveTokenValue` when it is provided', async () => { it('calls `getActiveTokenValue` when it is provided', () => {
const mockGetActiveTokenValue = jest.fn(); const mockGetActiveTokenValue = jest.fn();
wrapper.setProps({ wrapper = createComponent({
props: {
value: { data: `"${mockRegularLabel.title}"` },
suggestions: mockLabels,
getActiveTokenValue: mockGetActiveTokenValue, getActiveTokenValue: mockGetActiveTokenValue,
},
}); });
await wrapper.vm.$nextTick();
expect(mockGetActiveTokenValue).toHaveBeenCalledTimes(1); expect(mockGetActiveTokenValue).toHaveBeenCalledTimes(1);
expect(mockGetActiveTokenValue).toHaveBeenCalledWith( expect(mockGetActiveTokenValue).toHaveBeenCalledWith(
mockLabels, mockLabels,
...@@ -125,33 +118,19 @@ describe('BaseToken', () => { ...@@ -125,33 +118,19 @@ describe('BaseToken', () => {
describe('watch', () => { describe('watch', () => {
describe('active', () => { describe('active', () => {
let wrapperWithTokenActive;
beforeEach(() => { beforeEach(() => {
wrapperWithTokenActive = createComponent({ wrapper = createComponent({
props: { props: {
...mockProps,
value: { data: `"${mockRegularLabel.title}"` }, value: { data: `"${mockRegularLabel.title}"` },
active: true, active: true,
}, },
}); });
}); });
afterEach(() => {
wrapperWithTokenActive.destroy();
});
it('emits `fetch-suggestions` event on the component when value of this prop is changed to false and `suggestions` array is empty', async () => { it('emits `fetch-suggestions` event on the component when value of this prop is changed to false and `suggestions` array is empty', async () => {
wrapperWithTokenActive.setProps({ await wrapper.setProps({ active: false });
active: false,
});
await wrapperWithTokenActive.vm.$nextTick(); expect(wrapper.emitted('fetch-suggestions')).toEqual([[`"${mockRegularLabel.title}"`]]);
expect(wrapperWithTokenActive.emitted('fetch-suggestions')).toBeTruthy();
expect(wrapperWithTokenActive.emitted('fetch-suggestions')).toEqual([
[`"${mockRegularLabel.title}"`],
]);
}); });
}); });
}); });
...@@ -161,17 +140,15 @@ describe('BaseToken', () => { ...@@ -161,17 +140,15 @@ describe('BaseToken', () => {
const mockTokenValue = mockLabels[0]; const mockTokenValue = mockLabels[0];
it('calls `setTokenValueToRecentlyUsed` when `recentSuggestionsStorageKey` is defined', () => { it('calls `setTokenValueToRecentlyUsed` when `recentSuggestionsStorageKey` is defined', () => {
wrapper = createComponent({ props: { suggestions: mockLabels } });
wrapper.vm.handleTokenValueSelected(mockTokenValue.title); wrapper.vm.handleTokenValueSelected(mockTokenValue.title);
expect(setTokenValueToRecentlyUsed).toHaveBeenCalledWith(mockStorageKey, mockTokenValue); expect(setTokenValueToRecentlyUsed).toHaveBeenCalledWith(mockStorageKey, mockTokenValue);
}); });
it('does not add token from preloadedSuggestions', async () => { it('does not add token from preloadedSuggestions', () => {
wrapper.setProps({ wrapper = createComponent({ props: { preloadedSuggestions: [mockTokenValue] } });
preloadedSuggestions: [mockTokenValue],
});
await wrapper.vm.$nextTick();
wrapper.vm.handleTokenValueSelected(mockTokenValue.title); wrapper.vm.handleTokenValueSelected(mockTokenValue.title);
...@@ -182,58 +159,60 @@ describe('BaseToken', () => { ...@@ -182,58 +159,60 @@ describe('BaseToken', () => {
describe('template', () => { describe('template', () => {
it('renders gl-filtered-search-token component', () => { it('renders gl-filtered-search-token component', () => {
const wrapperWithNoStubs = createComponent({ wrapper = createComponent({ stubs: {} });
stubs: {},
});
const glFilteredSearchToken = wrapperWithNoStubs.find(GlFilteredSearchToken);
expect(glFilteredSearchToken.exists()).toBe(true);
expect(glFilteredSearchToken.props('config')).toEqual(mockProps.config);
wrapperWithNoStubs.destroy(); expect(findGlFilteredSearchToken().props('config')).toEqual(mockProps.config);
}); });
it('renders `view-token` slot when present', () => { it('renders `view-token` slot when present', () => {
wrapper = createComponent();
expect(wrapper.find('.js-view-token').exists()).toBe(true); expect(wrapper.find('.js-view-token').exists()).toBe(true);
}); });
it('renders `view` slot when present', () => { it('renders `view` slot when present', () => {
wrapper = createComponent();
expect(wrapper.find('.js-view').exists()).toBe(true); expect(wrapper.find('.js-view').exists()).toBe(true);
}); });
describe('events', () => { it('renders loading spinner when loading', () => {
let wrapperWithNoStubs; wrapper = createComponent({
props: {
active: true,
config: mockLabelToken,
suggestionsLoading: true,
},
stubs: { Portal: true },
});
afterEach(() => { expect(wrapper.findComponent(GlLoadingIcon).exists()).toBe(true);
wrapperWithNoStubs.destroy();
}); });
describe('events', () => {
describe('when activeToken has been selected', () => { describe('when activeToken has been selected', () => {
beforeEach(() => { beforeEach(() => {
wrapperWithNoStubs = createComponent({ wrapper = createComponent({
props: { props: { getActiveTokenValue: () => ({ title: '' }) },
...mockProps,
getActiveTokenValue: () => ({ title: '' }),
suggestionsLoading: true,
},
stubs: { Portal: true }, stubs: { Portal: true },
}); });
}); });
it('does not emit `fetch-suggestions` event on component after a delay when component emits `input` event', async () => { it('does not emit `fetch-suggestions` event on component after a delay when component emits `input` event', async () => {
jest.useFakeTimers(); jest.useFakeTimers();
wrapperWithNoStubs.find(GlFilteredSearchToken).vm.$emit('input', { data: 'foo' }); findGlFilteredSearchToken().vm.$emit('input', { data: 'foo' });
await wrapperWithNoStubs.vm.$nextTick(); await wrapper.vm.$nextTick();
jest.runAllTimers(); jest.runAllTimers();
expect(wrapperWithNoStubs.emitted('fetch-suggestions')).toEqual([['']]); expect(wrapper.emitted('fetch-suggestions')).toEqual([['']]);
}); });
}); });
describe('when activeToken has not been selected', () => { describe('when activeToken has not been selected', () => {
beforeEach(() => { beforeEach(() => {
wrapperWithNoStubs = createComponent({ wrapper = createComponent({
stubs: { Portal: true }, stubs: { Portal: true },
}); });
}); });
...@@ -241,38 +220,27 @@ describe('BaseToken', () => { ...@@ -241,38 +220,27 @@ describe('BaseToken', () => {
it('emits `fetch-suggestions` event on component after a delay when component emits `input` event', async () => { it('emits `fetch-suggestions` event on component after a delay when component emits `input` event', async () => {
jest.useFakeTimers(); jest.useFakeTimers();
wrapperWithNoStubs.find(GlFilteredSearchToken).vm.$emit('input', { data: 'foo' }); findGlFilteredSearchToken().vm.$emit('input', { data: 'foo' });
await wrapperWithNoStubs.vm.$nextTick(); await wrapper.vm.$nextTick();
jest.runAllTimers(); jest.runAllTimers();
expect(wrapperWithNoStubs.emitted('fetch-suggestions')).toBeTruthy(); expect(wrapper.emitted('fetch-suggestions')[2]).toEqual(['foo']);
expect(wrapperWithNoStubs.emitted('fetch-suggestions')[2]).toEqual(['foo']);
}); });
describe('when search is started with a quote', () => { describe('when search is started with a quote', () => {
it('emits `fetch-suggestions` with filtered value', async () => { it('emits `fetch-suggestions` with filtered value', () => {
jest.useFakeTimers(); findGlFilteredSearchToken().vm.$emit('input', { data: '"foo' });
wrapperWithNoStubs.find(GlFilteredSearchToken).vm.$emit('input', { data: '"foo' }); expect(wrapper.emitted('fetch-suggestions')[2]).toEqual(['foo']);
await wrapperWithNoStubs.vm.$nextTick();
jest.runAllTimers();
expect(wrapperWithNoStubs.emitted('fetch-suggestions')[2]).toEqual(['foo']);
}); });
}); });
describe('when search starts and ends with a quote', () => { describe('when search starts and ends with a quote', () => {
it('emits `fetch-suggestions` with filtered value', async () => { it('emits `fetch-suggestions` with filtered value', () => {
jest.useFakeTimers(); findGlFilteredSearchToken().vm.$emit('input', { data: '"foo"' });
wrapperWithNoStubs.find(GlFilteredSearchToken).vm.$emit('input', { data: '"foo"' });
await wrapperWithNoStubs.vm.$nextTick();
jest.runAllTimers();
expect(wrapperWithNoStubs.emitted('fetch-suggestions')[2]).toEqual(['foo']); expect(wrapper.emitted('fetch-suggestions')[2]).toEqual(['foo']);
}); });
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment