forked from deltaepsilon/firebase-paginator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase-paginator.spec.js
165 lines (141 loc) · 4.26 KB
/
firebase-paginator.spec.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const admin = require('firebase-admin');
const firebaseConfig = require('./env.json').firebaseConfig;
const secret = firebaseConfig.secret;
admin.initializeApp({
databaseURL: firebaseConfig.databaseURL,
credential: admin.credential.cert(firebaseConfig.serviceAccount)
});
const ref = admin.database().ref('firebasePaginator');
const collectionRef = ref.child('collection');
const smallCollectionRef = ref.child('small-collection');
const emptyCollectionRef = ref.child('empty-collection');
const FirebasePaginator = require('./firebase-paginator');
describe('Firebase Paginator', () => {
beforeAll(done => {
ref.remove().then(done);
});
beforeAll(done => {
populateCollection(100, collectionRef).then(done);
});
beforeAll(done => {
populateCollection(3, smallCollectionRef).then(done);
});
function populateCollection(count, ref) {
return new Promise(function(resolve, reject) {
var promises = [];
var i = count;
while (i--) {
promises.push(ref.push(count - i));
}
Promise.all(promises).then(resolve, reject);
});
}
describe('Initial Load', () => {
it('collection', done => {
collectionRef.once('value').then(snap => {
expect(snap.numChildren()).toEqual(100);
done();
});
});
it('small-collection', done => {
smallCollectionRef.once('value').then(snap => {
expect(snap.numChildren()).toEqual(3);
done();
});
});
it('empty-collection', done => {
emptyCollectionRef.once('value').then(snap => {
expect(snap.numChildren()).toEqual(0);
done();
});
});
});
let paginator;
function testPage(length, start, end, testName, precursorName) {
it(testName || `should return records ${start} to ${end}`, done => {
Promise.resolve()
.then(() => {
if (precursorName) {
return paginator[precursorName]();
} else {
return paginator.once('value');
}
})
.then(snap => {
var collection = paginator.collection || {};
var keys = Object.keys(collection);
var i = keys.length;
expect(i).toEqual(length);
if (length) {
expect(collection[keys[0]]).toEqual(start);
expect(collection[keys[i - 1]]).toEqual(end);
}
done();
});
});
}
describe('Finite Pagination', () => {
describe('empty-collection', () => {
beforeEach(() => {
paginator = new FirebasePaginator(emptyCollectionRef, {
finite: true,
auth: secret
});
});
testPage(0, undefined, undefined);
});
describe('small-collection', () => {
describe('pageSize: 10', () => {
beforeEach(() => {
paginator = new FirebasePaginator(smallCollectionRef, {
finite: true,
auth: secret,
pageSize: 10
});
});
testPage(3, 1, 3);
});
describe('pageSize: 3', () => {
beforeEach(() => {
paginator = new FirebasePaginator(smallCollectionRef, {
finite: true,
auth: secret,
pageSize: 3
});
});
testPage(3, 1, 3);
});
});
describe('collection', () => {
describe('pageSize: 10', () => {
beforeAll(() => {
paginator = new FirebasePaginator(collectionRef, {
finite: true,
auth: secret,
pageSize: 10
});
});
testPage(10, 91, 100);
for (let i = 90; i > 0; i -= 10) {
testPage(10, i-9, i, false, 'previous');
}
testPage(10, 1, 10, 'should fail to back paginate', 'previous');
});
describe('pageSize: 3', () => {
beforeAll(() => {
paginator = new FirebasePaginator(collectionRef, {
finite: true,
auth: secret,
pageSize: 3
});
});
testPage(3, 98, 100, false);
testPage(3, 95, 97, false, 'previous');
testPage(3, 92, 94, false, 'previous');
testPage(3, 95, 97, false, 'next');
testPage(3, 98, 100, false, 'next');
testPage(3, 98, 100, 'should fail to forward paginate and stick 98 to 100', 'next');
});
});
});
});