Skip to content

Commit d913e15

Browse files
SortBy component introduced
1 parent 7404d9d commit d913e15

File tree

3 files changed

+210
-0
lines changed

3 files changed

+210
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
define([
6+
'uiElement'
7+
], function (Element) {
8+
'use strict';
9+
10+
return Element.extend({
11+
defaults: {
12+
template: 'ui/grid/sortBy',
13+
options: [],
14+
applied: {},
15+
sorting: 'desc',
16+
columnsProvider: 'ns = ${ $.ns }, componentType = columns',
17+
selectedOption: '',
18+
isVisible: true,
19+
listens: {
20+
'selectedOption': 'applyChanges'
21+
},
22+
statefull: {
23+
selectedOption: true,
24+
applied: true
25+
},
26+
exports: {
27+
applied: '${ $.provider }:params.sorting'
28+
},
29+
imports: {
30+
preparedOptions: '${ $.columnsProvider }:elems'
31+
},
32+
modules: {
33+
columns: '${ $.columnsProvider }'
34+
}
35+
},
36+
37+
/**
38+
* @inheritdoc
39+
*/
40+
initObservable: function () {
41+
return this._super()
42+
.observe([
43+
'applied',
44+
'selectedOption',
45+
'isVisible'
46+
]);
47+
},
48+
49+
/**
50+
* Prepared sort order options
51+
*/
52+
preparedOptions: function (columns) {
53+
if (columns && columns.length > 0) {
54+
columns.map(function (column) {
55+
if (column.sortable === true) {
56+
this.options.push({
57+
value: column.index,
58+
label: column.label
59+
});
60+
this.isVisible(true);
61+
} else {
62+
this.isVisible(false);
63+
}
64+
}.bind(this));
65+
}
66+
},
67+
68+
/**
69+
* Apply changes
70+
*/
71+
applyChanges: function () {
72+
this.applied({
73+
field: this.selectedOption(),
74+
direction: this.sorting
75+
});
76+
}
77+
});
78+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!--
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<div if="isVisible" class="masonry-sorting">
8+
<b><!-- ko i18n: 'Sort by' --><!-- /ko -->:</b>
9+
<select class="admin__control-select" data-bind="
10+
options: options,
11+
optionsValue: 'value',
12+
optionsText: 'label',
13+
value: selectedOption
14+
"></select>
15+
</div>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
define([
2+
'Magento_Ui/js/grid/sortBy'
3+
], function (sortBy) {
4+
'use strict';
5+
describe('Magento_Ui/js/grid/sortBy', function () {
6+
7+
var sortByObj;
8+
9+
beforeEach(function () {
10+
sortByObj = new sortBy({
11+
options: []
12+
});
13+
});
14+
15+
describe('"initObservable" method', function () {
16+
it('Check for defined ', function () {
17+
expect(sortByObj.hasOwnProperty('initObservable')).toBeDefined();
18+
});
19+
it('Check method type', function () {
20+
var type = typeof sortByObj.initObservable;
21+
22+
expect(type).toEqual('function');
23+
});
24+
it('Check returned value if method called without arguments', function () {
25+
expect(sortByObj.initObservable()).toBeDefined();
26+
});
27+
it('Check returned value type if method called without arguments', function () {
28+
var type = typeof sortByObj.initObservable();
29+
expect(type).toEqual('object');
30+
});
31+
});
32+
33+
describe('"preparedOptions" method', function () {
34+
it('Check for defined ', function () {
35+
expect(sortByObj.hasOwnProperty('preparedOptions')).toBeDefined();
36+
});
37+
it('Check method type', function () {
38+
var type = typeof sortByObj.preparedOptions;
39+
expect(type).toEqual('function');
40+
});
41+
42+
it('Check "options" array is empty if sortable is set false', function () {
43+
var columns = [{
44+
sortable: false,
45+
label: 'magento',
46+
index: 'test'
47+
}],
48+
expectedValue = [];
49+
sortByObj.preparedOptions(columns);
50+
expect(sortByObj.options).toEqual(expectedValue);
51+
});
52+
53+
it('Check "options" array is set the correct value', function () {
54+
var columns = [{
55+
sortable: true,
56+
label: 'magento',
57+
index: 'test'
58+
}],
59+
expectedValue = [{
60+
value: 'test',
61+
label: 'magento'
62+
}];
63+
sortByObj.preparedOptions(columns);
64+
expect(sortByObj.options).toEqual(expectedValue);
65+
});
66+
67+
it('Check "isVisible" set true if column is sortable', function () {
68+
var columns = [{
69+
sortable: true,
70+
label: 'magento',
71+
index: 'test'
72+
}];
73+
spyOn(sortByObj, "isVisible").and.callFake(function () {
74+
return true;
75+
});
76+
sortByObj.preparedOptions(columns);
77+
expect(sortByObj.isVisible).toHaveBeenCalled();
78+
expect(sortByObj.isVisible()).toBeTruthy();
79+
});
80+
81+
it('Check "isVisible" set true if column is sortable', function () {
82+
var columns = [{
83+
sortable: true,
84+
label: 'magento',
85+
index: 'test'
86+
}];
87+
spyOn(sortByObj, "isVisible").and.callFake(function () {
88+
return false;
89+
});
90+
sortByObj.preparedOptions(columns);
91+
expect(sortByObj.isVisible).toHaveBeenCalled();
92+
expect(sortByObj.isVisible()).toBeFalsy();
93+
});
94+
});
95+
describe('"applyChanges" method', function () {
96+
it('Check for defined ', function () {
97+
expect(sortByObj.hasOwnProperty('applyChanges')).toBeDefined();
98+
});
99+
it('Check method type', function () {
100+
var type = typeof sortByObj.applyChanges;
101+
expect(type).toEqual('function');
102+
});
103+
104+
it('Check "selectedOption" method has been called', function () {
105+
spyOn(sortByObj, 'selectedOption');
106+
sortByObj.applyChanges();
107+
expect(sortByObj.selectedOption).toHaveBeenCalled();
108+
});
109+
110+
it('Check "applied" method has been called', function () {
111+
spyOn(sortByObj, 'applied');
112+
sortByObj.applyChanges();
113+
expect(sortByObj.applied).toHaveBeenCalled();
114+
});
115+
});
116+
});
117+
});

0 commit comments

Comments
 (0)