2021-04-29 21:17:54 +05:30
|
|
|
import { shallowMount } from '@vue/test-utils';
|
2023-04-23 21:23:45 +05:30
|
|
|
import { historyPushState, historyReplaceState } from '~/lib/utils/common_utils';
|
2022-11-25 23:54:43 +05:30
|
|
|
import { mergeUrlParams, setUrlParams } from '~/lib/utils/url_utility';
|
2023-04-23 21:23:45 +05:30
|
|
|
import UrlSyncComponent, {
|
|
|
|
URL_SET_PARAMS_STRATEGY,
|
|
|
|
HISTORY_REPLACE_UPDATE_METHOD,
|
|
|
|
} from '~/vue_shared/components/url_sync.vue';
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
jest.mock('~/lib/utils/url_utility', () => ({
|
2022-11-25 23:54:43 +05:30
|
|
|
mergeUrlParams: jest.fn((query, url) => `urlParams: ${JSON.stringify(query)} ${url}`),
|
|
|
|
setUrlParams: jest.fn((query, url) => `urlParams: ${JSON.stringify(query)} ${url}`),
|
2021-04-29 21:17:54 +05:30
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock('~/lib/utils/common_utils', () => ({
|
|
|
|
historyPushState: jest.fn(),
|
2023-04-23 21:23:45 +05:30
|
|
|
historyReplaceState: jest.fn(),
|
2021-04-29 21:17:54 +05:30
|
|
|
}));
|
|
|
|
|
|
|
|
describe('url sync component', () => {
|
|
|
|
let wrapper;
|
|
|
|
const mockQuery = { group_id: '5014437163714', project_ids: ['5014437608314'] };
|
|
|
|
|
|
|
|
const findButton = () => wrapper.find('button');
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
const createComponent = ({ props = {}, scopedSlots, slots } = {}) => {
|
2021-04-29 21:17:54 +05:30
|
|
|
wrapper = shallowMount(UrlSyncComponent, {
|
2023-04-23 21:23:45 +05:30
|
|
|
propsData: {
|
|
|
|
query: mockQuery,
|
|
|
|
...props,
|
|
|
|
},
|
2021-04-29 21:17:54 +05:30
|
|
|
scopedSlots,
|
|
|
|
slots,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
const expectUrlSyncWithMergeUrlParams = (
|
2022-11-25 23:54:43 +05:30
|
|
|
query,
|
|
|
|
times,
|
2023-04-23 21:23:45 +05:30
|
|
|
mergeUrlParamsReturnValue,
|
|
|
|
historyMethod = historyPushState,
|
2022-11-25 23:54:43 +05:30
|
|
|
) => {
|
2023-04-23 21:23:45 +05:30
|
|
|
expect(mergeUrlParams).toHaveBeenCalledTimes(times);
|
|
|
|
expect(mergeUrlParams).toHaveBeenCalledWith(query, window.location.href, {
|
|
|
|
spreadArrays: true,
|
|
|
|
});
|
2022-11-25 23:54:43 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
expect(historyMethod).toHaveBeenCalledTimes(times);
|
|
|
|
expect(historyMethod).toHaveBeenCalledWith(mergeUrlParamsReturnValue);
|
2022-11-25 23:54:43 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
const expectUrlSyncWithSetUrlParams = (query, times, setUrlParamsReturnValue) => {
|
2023-04-23 21:23:45 +05:30
|
|
|
expect(setUrlParams).toHaveBeenCalledTimes(times);
|
|
|
|
expect(setUrlParams).toHaveBeenCalledWith(query, window.location.href, true, true, true);
|
|
|
|
|
|
|
|
expect(historyPushState).toHaveBeenCalledTimes(times);
|
|
|
|
expect(historyPushState).toHaveBeenCalledWith(setUrlParamsReturnValue);
|
2021-04-29 21:17:54 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
describe('with query as a props', () => {
|
|
|
|
it('immediately syncs the query to the URL', () => {
|
|
|
|
createComponent();
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expectUrlSyncWithMergeUrlParams(mockQuery, 1, mergeUrlParams.mock.results[0].value);
|
2021-04-29 21:17:54 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
describe('when the query is modified', () => {
|
|
|
|
const newQuery = { foo: true };
|
|
|
|
|
|
|
|
it('updates the URL with the new query', async () => {
|
|
|
|
createComponent();
|
|
|
|
// using setProps to test the watcher
|
|
|
|
await wrapper.setProps({ query: newQuery });
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expectUrlSyncWithMergeUrlParams(mockQuery, 2, mergeUrlParams.mock.results[1].value);
|
2021-04-29 21:17:54 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
describe('with url-params-update-strategy equals to URL_SET_PARAMS_STRATEGY', () => {
|
|
|
|
it('uses setUrlParams to generate URL', () => {
|
|
|
|
createComponent({
|
2023-04-23 21:23:45 +05:30
|
|
|
props: {
|
|
|
|
urlParamsUpdateStrategy: URL_SET_PARAMS_STRATEGY,
|
|
|
|
},
|
2022-11-25 23:54:43 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
expectUrlSyncWithSetUrlParams(mockQuery, 1, setUrlParams.mock.results[0].value);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
describe('with history-update-method equals to HISTORY_REPLACE_UPDATE_METHOD', () => {
|
|
|
|
it('uses historyReplaceState to update the URL', () => {
|
|
|
|
createComponent({
|
|
|
|
props: {
|
|
|
|
historyUpdateMethod: HISTORY_REPLACE_UPDATE_METHOD,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expectUrlSyncWithMergeUrlParams(
|
|
|
|
mockQuery,
|
|
|
|
1,
|
|
|
|
mergeUrlParams.mock.results[0].value,
|
|
|
|
historyReplaceState,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
describe('with scoped slot', () => {
|
|
|
|
const scopedSlots = {
|
|
|
|
default: `
|
|
|
|
<button @click="props.updateQuery({bar: 'baz'})">Update Query </button>
|
|
|
|
`,
|
|
|
|
};
|
|
|
|
|
|
|
|
it('renders the scoped slot', () => {
|
2023-04-23 21:23:45 +05:30
|
|
|
createComponent({ props: { query: null }, scopedSlots });
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
expect(findButton().exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('syncs the url with the scoped slots function', () => {
|
2023-04-23 21:23:45 +05:30
|
|
|
createComponent({ props: { query: null }, scopedSlots });
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
findButton().trigger('click');
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expectUrlSyncWithMergeUrlParams({ bar: 'baz' }, 1, mergeUrlParams.mock.results[0].value);
|
2021-04-29 21:17:54 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with slot', () => {
|
|
|
|
const slots = {
|
|
|
|
default: '<button>Normal Slot</button>',
|
|
|
|
};
|
|
|
|
|
|
|
|
it('renders the default slot', () => {
|
2023-04-23 21:23:45 +05:30
|
|
|
createComponent({ props: { query: null }, slots });
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
expect(findButton().exists()).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|