Skip to content

Commit 0770421

Browse files
committed
add tests for null and undefined values
1 parent c8024b8 commit 0770421

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

test.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,34 @@ describe('arraySort', function() {
6060
]);
6161
});
6262

63+
it('should sort by a property with null values:', function() {
64+
var arr = [{key: null}, {key: 'z'}, {key: 'x'}];
65+
arraySort(arr, 'key').should.eql([
66+
{key: 'x'},
67+
{key: 'z'},
68+
{key: null}
69+
]);
70+
});
71+
72+
it('should sort by a property with undefined values:', function() {
73+
var arr = [{}, {key: 'z'}, {key: 'x'}];
74+
arraySort(arr, 'key').should.eql([
75+
{key: 'x'},
76+
{key: 'z'},
77+
{}
78+
]);
79+
});
80+
81+
it('should sort by a property with null and undefined values:', function() {
82+
var arr = [{key: null}, {key: 'z'}, {}, {key: 'x'}];
83+
arraySort(arr, 'key').should.eql([
84+
{key: 'x'},
85+
{key: 'z'},
86+
{key: null},
87+
{}
88+
]);
89+
});
90+
6391
it('should sort by a nested property:', function() {
6492
var res = arraySort(posts, 'locals.date');
6593
res.should.eql([
@@ -116,6 +144,84 @@ describe('arraySort', function() {
116144
]);
117145
});
118146

147+
it('should sort by multiple properties with null values:', function() {
148+
var posts = [
149+
{ foo: 'bbb', locals: { date: '2013-05-06' } },
150+
{ foo: 'aaa', locals: { date: '2012-01-02' } },
151+
{ foo: null, locals: { date: '2015-04-12' } },
152+
{ foo: 'ccc', locals: { date: '2014-01-02' } },
153+
{ foo: null, locals: { date: '2015-01-02' } },
154+
{ foo: 'ddd', locals: { date: '2014-01-09' } },
155+
{ foo: 'bbb', locals: { date: null } },
156+
{ foo: 'aaa', locals: { date: '2014-02-02' } },
157+
];
158+
159+
var actual = arraySort(posts, ['foo', 'locals.date']);
160+
161+
actual.should.eql([
162+
{ foo: 'aaa', locals: { date: '2012-01-02' } },
163+
{ foo: 'aaa', locals: { date: '2014-02-02' } },
164+
{ foo: 'bbb', locals: { date: '2013-05-06' } },
165+
{ foo: 'bbb', locals: { date: null } },
166+
{ foo: 'ccc', locals: { date: '2014-01-02' } },
167+
{ foo: 'ddd', locals: { date: '2014-01-09' } },
168+
{ foo: null, locals: { date: '2015-01-02' } },
169+
{ foo: null, locals: { date: '2015-04-12' } }
170+
]);
171+
});
172+
173+
it('should sort by multiple properties with undefined values:', function() {
174+
var posts = [
175+
{ foo: 'bbb', locals: { date: '2013-05-06' } },
176+
{ foo: 'aaa', locals: { date: '2012-01-02' } },
177+
{ locals: { date: '2015-04-12' } },
178+
{ foo: 'ccc', locals: { date: '2014-01-02' } },
179+
{ locals: { date: '2015-01-02' } },
180+
{ foo: 'ddd', locals: { date: '2014-01-09' } },
181+
{ foo: 'bbb', locals: {} },
182+
{ foo: 'aaa', locals: { date: '2014-02-02' } },
183+
];
184+
185+
var actual = arraySort(posts, ['foo', 'locals.date']);
186+
187+
actual.should.eql([
188+
{ foo: 'aaa', locals: { date: '2012-01-02' } },
189+
{ foo: 'aaa', locals: { date: '2014-02-02' } },
190+
{ foo: 'bbb', locals: { date: '2013-05-06' } },
191+
{ foo: 'bbb', locals: {} },
192+
{ foo: 'ccc', locals: { date: '2014-01-02' } },
193+
{ foo: 'ddd', locals: { date: '2014-01-09' } },
194+
{ locals: { date: '2015-01-02' } },
195+
{ locals: { date: '2015-04-12' } }
196+
]);
197+
});
198+
199+
it('should sort by multiple properties with null and undefined values:', function() {
200+
var posts = [
201+
{ foo: 'bbb', locals: { date: '2013-05-06' } },
202+
{ foo: 'aaa', locals: { date: null } },
203+
{ locals: { date: '2015-04-12' } },
204+
{ foo: 'ccc', locals: { date: '2014-01-02' } },
205+
{ locals: { date: '2015-01-02' } },
206+
{ foo: 'ddd', locals: { date: '2014-01-09' } },
207+
{ foo: null, locals: {} },
208+
{ foo: 'aaa', locals: { date: '2014-02-02' } },
209+
];
210+
211+
var actual = arraySort(posts, ['foo', 'locals.date']);
212+
213+
actual.should.eql([
214+
{ foo: 'aaa', locals: { date: '2014-02-02' } },
215+
{ foo: 'aaa', locals: { date: null } },
216+
{ foo: 'bbb', locals: { date: '2013-05-06' } },
217+
{ foo: 'ccc', locals: { date: '2014-01-02' } },
218+
{ foo: 'ddd', locals: { date: '2014-01-09' } },
219+
{ foo: null, locals: {} },
220+
{ locals: { date: '2015-01-02' } },
221+
{ locals: { date: '2015-04-12' } }
222+
]);
223+
});
224+
119225
it('should sort with a function:', function() {
120226
var arr = [{key: 'y'}, {key: 'z'}, {key: 'x'}];
121227

0 commit comments

Comments
 (0)