Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dlohvinov committed Dec 15, 2023
2 parents 971cd81 + 6de7031 commit e15861f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
43 changes: 25 additions & 18 deletions src/mixins/dataFilterMixins/__tests__/sortFilterMixin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,46 @@ const headers = [{
field: 'online',
}];

const sortedHeaders = [{
value: 'queue',
show: true,
sort: 'asc',
field: 'queue',
}, {
value: 'agents',
show: true,
sort: null,
field: 'online',
}];

describe('Sort filter mixin', () => {
let wrapper;
const setHeaders = vi.fn();
const Component = {
render() {},
mixins: [sortFilterMixin],
data: () => ({ headers }),
methods: { setHeaders },
};

beforeEach(async () => {
await router.replace('/');
wrapper = shallowMount(Component, {
global: { plugins: [router] },
});
});


it('Correctly sets value from $route query', async () => {
await router.replace({ path: '/', query: { sort: '+queue' } });
const queue = wrapper.vm.headers.find((header) => header.value === 'queue');
expect(queue.sort).toEqual('asc');
});

it('After "sort" trigger, header column sort value changes properly', async () => {
let queue;
queue = wrapper.vm.headers.find((header) => header.value === 'queue');
await wrapper.vm.sort(queue);
queue = wrapper.vm.headers.find((header) => header.value === 'queue');
expect(queue.sort).toEqual('asc');
const wrapper = shallowMount(Component, {
global: { plugins: [router] },
});
expect(setHeaders).toHaveBeenCalledWith(sortedHeaders);
});

it('After "sort" trigger, url query changes properly', async () => {
await router.replace({ path: '/', query: { sort: '+queue' } });
it('After "sort" trigger, header column sort value changes properly', () => {
const wrapper = shallowMount(Component, {
global: { plugins: [router] },
});
const queue = wrapper.vm.headers.find((header) => header.value === 'queue');
await wrapper.vm.sort(queue);
expect(wrapper.vm.$route.query.sort).toEqual('-queue');
wrapper.vm.sort(queue);
expect(setHeaders).toHaveBeenCalledWith(sortedHeaders);
});
});
15 changes: 10 additions & 5 deletions src/mixins/dataFilterMixins/sortFilterMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ export default {
},

setValue({ column, order }) {
this.headers = this.headers.map((col) => ({
...col,
sort: col === column ? order : null,
}));
const headers = this.headers.map((col) => {
const sortFieldValue = col?.sort;
return {
...col,
sort: col === column ? order : sortFieldValue,
}
});
const value = encodeSortQuery({ column, order });
this.setHeaders(headers);
return this.setValueToQuery({
value,
filterQuery: this.filterQuery,
Expand All @@ -66,10 +70,11 @@ export default {

restoreValue(value) {
const sortedColumns = decodeSortQuery({ value });
this.headers = this.headers.map((header) => ({
const headers = this.headers.map((header) => ({
...header,
sort: sortedColumns[header.field] || null,
}));
this.setHeaders(headers);
},
},
};

0 comments on commit e15861f

Please sign in to comment.