-
Notifications
You must be signed in to change notification settings - Fork 28
/
empty-test.js
122 lines (116 loc) · 3.15 KB
/
empty-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
var test = require('tape');
var firebase = require('firebase');
var path = 'firebasePaginator/empty';
var firebaseConfig = require('./env.json').firebaseConfig;
firebase.initializeApp(firebaseConfig);
var FirebasePaginator = require('./firebase-paginator');
var ref = firebase.database().ref(path);
var populateCollection = function () {
return new Promise(function (resolve, reject) {
var promises = [];
var i = 3;
while (i--) {
promises.push(ref.push(3 - i));
}
Promise.all(promises).then(resolve, reject);
});
};
var testPage = function (paginator, length, start, end, testName) {
return new Promise(function (resolve, reject) {
test(testName || `should return records ${start} to ${end}`, function (t) {
paginator.once('value', function (snap) {
var collection = this.collection;
var keys = Object.keys(collection);
var i = keys.length;
t.equal(i, length);
t.equal(collection[keys[0]], start);
t.equal(collection[keys[i - 1]], end);
t.end();
resolve(paginator);
});
});
});
};
ref.remove()
.then(function () {
var paginator = new FirebasePaginator(ref);
return paginator;
})
// .then(function (paginator) {
// return new Promise(function (resolve, reject) {
// test(`should return empty collection`, function (t) {
// paginator.once('value', function (snap) {
// t.equal(snap.numChildren(), 0);
// t.end();
// resolve(paginator);
// });
// });
// });
// })
.then(function (paginator) {
return populateCollection()
.then(function () {
return paginator;
});
})
.then(function (paginator) {
return new Promise(function (resolve, reject) {
test(`should return three results`, function (t) {
paginator.once('value', function (snap) {
t.equal(snap.numChildren(), 3);
t.end();
resolve(paginator);
});
});
});
})
.then(function () {
var paginator = new FirebasePaginator(ref, {
pageSize: 5,
finite: true,
auth: firebaseConfig.secret
});
return paginator;
})
.then(function (paginator) {
return ref.remove()
.then(function () {
return paginator;
});
})
.then(function (paginator) {
return new Promise(function (resolve, reject) {
test(`should return empty collection`, function (t) {
paginator.once('value', function (snap) {
t.equal(snap.numChildren(), 0);
t.end();
resolve(paginator);
});
});
});
})
.then(function () {
return populateCollection()
.then(function () {
return paginator;
});
})
.then(function (paginator) {
return new Promise(function (resolve, reject) {
test(`should return three results`, function (t) {
paginator.once('value', function (snap) {
t.equal(snap.numChildren(), 3);
t.end();
resolve(paginator);
});
});
});
})
.then(function () {
console.log('complete');
process.exit();
})
.catch(function (err) {
console.log('error', err);
process.exit();
});