2021-01-29 18:09:17 +00:00
|
|
|
import { shallowMount, createLocalVue } from '@vue/test-utils';
|
2021-02-14 18:09:20 +00:00
|
|
|
import Vuex from 'vuex';
|
2021-01-29 18:09:17 +00:00
|
|
|
import component from '~/packages/list/components/package_search.vue';
|
|
|
|
import PackageTypeToken from '~/packages/list/components/tokens/package_type_token.vue';
|
2021-04-14 15:09:04 +00:00
|
|
|
import { sortableFields } from '~/packages/list/utils';
|
2021-02-14 18:09:20 +00:00
|
|
|
import RegistrySearch from '~/vue_shared/components/registry/registry_search.vue';
|
2021-03-30 21:10:07 +00:00
|
|
|
import UrlSync from '~/vue_shared/components/url_sync.vue';
|
2021-01-29 18:09:17 +00:00
|
|
|
|
|
|
|
const localVue = createLocalVue();
|
|
|
|
localVue.use(Vuex);
|
|
|
|
|
|
|
|
describe('Package Search', () => {
|
|
|
|
let wrapper;
|
|
|
|
let store;
|
|
|
|
|
2021-03-30 21:10:07 +00:00
|
|
|
const findRegistrySearch = () => wrapper.findComponent(RegistrySearch);
|
|
|
|
const findUrlSync = () => wrapper.findComponent(UrlSync);
|
2021-01-29 18:09:17 +00:00
|
|
|
|
|
|
|
const createStore = (isGroupPage) => {
|
|
|
|
const state = {
|
|
|
|
config: {
|
|
|
|
isGroupPage,
|
|
|
|
},
|
|
|
|
sorting: {
|
|
|
|
orderBy: 'version',
|
|
|
|
sort: 'desc',
|
|
|
|
},
|
|
|
|
filter: [],
|
|
|
|
};
|
|
|
|
store = new Vuex.Store({
|
|
|
|
state,
|
|
|
|
});
|
|
|
|
store.dispatch = jest.fn();
|
|
|
|
};
|
|
|
|
|
|
|
|
const mountComponent = (isGroupPage = false) => {
|
|
|
|
createStore(isGroupPage);
|
|
|
|
|
|
|
|
wrapper = shallowMount(component, {
|
|
|
|
localVue,
|
|
|
|
store,
|
2021-03-30 21:10:07 +00:00
|
|
|
stubs: {
|
|
|
|
UrlSync,
|
|
|
|
},
|
2021-01-29 18:09:17 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
wrapper = null;
|
|
|
|
});
|
|
|
|
|
2021-02-08 12:09:20 +00:00
|
|
|
it('has a registry search component', () => {
|
|
|
|
mountComponent();
|
|
|
|
|
|
|
|
expect(findRegistrySearch().exists()).toBe(true);
|
|
|
|
expect(findRegistrySearch().props()).toMatchObject({
|
|
|
|
filter: store.state.filter,
|
|
|
|
sorting: store.state.sorting,
|
|
|
|
tokens: expect.arrayContaining([
|
|
|
|
expect.objectContaining({ token: PackageTypeToken, type: 'type', icon: 'package' }),
|
|
|
|
]),
|
2021-04-14 15:09:04 +00:00
|
|
|
sortableFields: sortableFields(),
|
2021-01-29 18:09:17 +00:00
|
|
|
});
|
2021-02-08 12:09:20 +00:00
|
|
|
});
|
2021-01-29 18:09:17 +00:00
|
|
|
|
2021-02-08 12:09:20 +00:00
|
|
|
it.each`
|
|
|
|
isGroupPage | page
|
|
|
|
${false} | ${'project'}
|
|
|
|
${true} | ${'group'}
|
|
|
|
`('in a $page page binds the right props', ({ isGroupPage }) => {
|
|
|
|
mountComponent(isGroupPage);
|
|
|
|
|
|
|
|
expect(findRegistrySearch().props()).toMatchObject({
|
|
|
|
filter: store.state.filter,
|
|
|
|
sorting: store.state.sorting,
|
|
|
|
tokens: expect.arrayContaining([
|
|
|
|
expect.objectContaining({ token: PackageTypeToken, type: 'type', icon: 'package' }),
|
|
|
|
]),
|
2021-04-14 15:09:04 +00:00
|
|
|
sortableFields: sortableFields(isGroupPage),
|
2021-01-29 18:09:17 +00:00
|
|
|
});
|
2021-02-08 12:09:20 +00:00
|
|
|
});
|
2021-01-29 18:09:17 +00:00
|
|
|
|
2021-02-08 12:09:20 +00:00
|
|
|
it('on sorting:changed emits update event and calls vuex setSorting', () => {
|
|
|
|
const payload = { sort: 'foo' };
|
2021-01-29 18:09:17 +00:00
|
|
|
|
2021-02-08 12:09:20 +00:00
|
|
|
mountComponent();
|
2021-01-29 18:09:17 +00:00
|
|
|
|
2021-02-08 12:09:20 +00:00
|
|
|
findRegistrySearch().vm.$emit('sorting:changed', payload);
|
2021-01-29 18:09:17 +00:00
|
|
|
|
2021-02-08 12:09:20 +00:00
|
|
|
expect(store.dispatch).toHaveBeenCalledWith('setSorting', payload);
|
|
|
|
expect(wrapper.emitted('update')).toEqual([[]]);
|
|
|
|
});
|
2021-01-29 18:09:17 +00:00
|
|
|
|
2021-02-08 12:09:20 +00:00
|
|
|
it('on filter:changed calls vuex setFilter', () => {
|
|
|
|
const payload = ['foo'];
|
2021-01-29 18:09:17 +00:00
|
|
|
|
2021-02-08 12:09:20 +00:00
|
|
|
mountComponent();
|
2021-01-29 18:09:17 +00:00
|
|
|
|
2021-02-08 12:09:20 +00:00
|
|
|
findRegistrySearch().vm.$emit('filter:changed', payload);
|
2021-01-29 18:09:17 +00:00
|
|
|
|
2021-02-08 12:09:20 +00:00
|
|
|
expect(store.dispatch).toHaveBeenCalledWith('setFilter', payload);
|
2021-01-29 18:09:17 +00:00
|
|
|
});
|
|
|
|
|
2021-02-08 12:09:20 +00:00
|
|
|
it('on filter:submit emits update event', () => {
|
|
|
|
mountComponent();
|
2021-01-29 18:09:17 +00:00
|
|
|
|
2021-02-08 12:09:20 +00:00
|
|
|
findRegistrySearch().vm.$emit('filter:submit');
|
2021-01-29 18:09:17 +00:00
|
|
|
|
2021-02-08 12:09:20 +00:00
|
|
|
expect(wrapper.emitted('update')).toEqual([[]]);
|
2021-01-29 18:09:17 +00:00
|
|
|
});
|
2021-03-30 21:10:07 +00:00
|
|
|
|
|
|
|
it('has a UrlSync component', () => {
|
|
|
|
mountComponent();
|
|
|
|
|
|
|
|
expect(findUrlSync().exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('on query:changed calls updateQuery from UrlSync', () => {
|
|
|
|
jest.spyOn(UrlSync.methods, 'updateQuery').mockImplementation(() => {});
|
|
|
|
|
|
|
|
mountComponent();
|
|
|
|
|
|
|
|
findRegistrySearch().vm.$emit('query:changed');
|
|
|
|
|
|
|
|
expect(UrlSync.methods.updateQuery).toHaveBeenCalled();
|
|
|
|
});
|
2021-01-29 18:09:17 +00:00
|
|
|
});
|