-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.test.js
142 lines (136 loc) · 5.18 KB
/
sort.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import Sort from './sort.js';
describe('#constructor', () => {
it('sets a property name if given', () => {
expect(new Sort('test').property).toBe('test');
});
it('sets a property name and sort order if given', () => {
expect(new Sort('test', Sort.DIR.DESC).property).toBe('test');
expect(new Sort('test', Sort.DIR.DESC).dir).toBe(Sort.DIR.DESC);
});
it('sets the default sort direction to ascending.', () => {
expect(new Sort().dir).toBe(Sort.DIR.ASC);
});
});
describe('#toString', () => {
it('returns a property and direction when set.', () => {
expect(new Sort('test', Sort.DIR.ASC).toString()).toBe('{test} ASC');
});
it('returns a property when set but direction is missing.', () => {
let s = new Sort('test');
s.dir = null;
expect(s.toString()).toBe('{test}');
});
it('returns a blank string when the property is missing.', () => {
expect(new Sort('', Sort.DIR.ASC).toString()).toBe('');
expect(new Sort(null, Sort.DIR.ASC).toString()).toBe('');
expect(new Sort(undefined, Sort.DIR.DESC).toString()).toBe('');
});
});
describe('.asc', () => {
it('returns a new Sort instance sorted in ASC direction.', () => {
expect(Sort.asc('abc').property).toBe('abc');
expect(Sort.asc('abc').dir).toBe(Sort.DIR.ASC);
});
});
describe('.desc', () => {
it('returns a new Sort instance sorted in DESC direction.', () => {
expect(Sort.desc('abc').property).toBe('abc');
expect(Sort.desc('abc').dir).toBe(Sort.DIR.DESC);
});
});
describe('.parse', () => {
it('returns a new Sort instance created from just a property name.', () => {
let tries = [
{ input: '{test}', expected: { property: 'test', dir: Sort.DIR.ASC } },
{ input: '{test} desc', expected: { property: 'test', dir: Sort.DIR.DESC } },
{ input: '{test} DESC', expected: { property: 'test', dir: Sort.DIR.DESC } },
{ input: '{test} asc', expected: { property: 'test', dir: Sort.DIR.ASC } },
{ input: '{test} ASC', expected: { property: 'test', dir: Sort.DIR.ASC } }
];
for (let t of tries) {
let s = Sort.parse(t.input);
expect(s).toBeInstanceOf(Sort);
expect(s.property).toBe(t.expected.property);
expect(s.dir).toBe(t.expected.dir);
}
});
it('returns an array of sorts if the string is comma-seperated', () => {
let tries = [
{
input: '{test}, {moose}, {rest}',
expected: [
{ property: 'test', dir: Sort.DIR.ASC },
{ property: 'moose', dir: Sort.DIR.ASC },
{ property: 'rest', dir: Sort.DIR.ASC }
]
},
{
input: '{test} desc, {moose} asc, {rest} desc',
expected: [
{ property: 'test', dir: Sort.DIR.DESC },
{ property: 'moose', dir: Sort.DIR.ASC },
{ property: 'rest', dir: Sort.DIR.DESC }
]
},
{
input: '{test} desc, {moose}',
expected: [
{ property: 'test', dir: Sort.DIR.DESC },
{ property: 'moose', dir: Sort.DIR.ASC }
]
}
];
for (let t of tries) {
let s = Sort.parse(t.input);
expect(s).toBeInstanceOf(Array);
expect(s).toEqual(t.expected);
expect(s.length).toBeGreaterThan(1);
}
});
it('Returns null if unparsable.', () => {
let tries = [null, undefined, '', 0, false];
for (let t of tries) {
let s = Sort.parse(t);
expect(s).toBeNull();
}
});
});
describe('._tokenize', () => {
it('retrieves properties, order, and separator tokens.', () => {
let tests = [
{
input: '{Bob}',
expected: [
{ type: 'property', value: 'Bob' }
]
},
{
input: '{\\{Bobby Tables\\}}',
expected: [
{ type: 'property', value: '{Bobby Tables}' }
]
},
{
input: '{Bob}, {Susan} desc, {Micael} asc',
expected: [
{ type: 'property', value: 'Bob' },
{ type: 'separator' },
{ type: 'property', value: 'Susan' },
{ type: 'order', value: Sort.DIR.DESC },
{ type: 'separator' },
{ type: 'property', value: 'Micael' },
{ type: 'order', value: Sort.DIR.ASC },
]
},
];
for (let t of tests) {
let s = Sort._tokenize(t.input);
expect(s).toBeInstanceOf(Array);
expect(s).toEqual(t.expected);
}
});
it('throws SyntaxError on invalid or unescaped character in the string.', () => {
expect(() => Sort._tokenize('{Marshmellow} mellon')).toThrow(SyntaxError);
expect(() => Sort._tokenize('{Marshmellow} ||')).toThrow(SyntaxError);
});
});