forked from microsoftgraph/msgraph-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageIterator.ts
More file actions
158 lines (139 loc) · 4.27 KB
/
Copy pathPageIterator.ts
File metadata and controls
158 lines (139 loc) · 4.27 KB
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
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
import { assert } from "chai";
import { Client } from "../../src/index";
import { PageIterator, PageIteratorCallback } from "../../src/tasks/PageIterator";
import { getClient } from "../test-helper";
const client: Client = getClient();
const value = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const getPageCollection = () => {
return {
value: [...value],
additionalContent: "additional content",
};
};
const getPageCollectionWithNext = () => {
return {
value: [...value],
"@odata.nextLink": "nextURL",
additionalContent: "additional content",
};
};
const getEmptyPageCollection = () => {
return {
value: [],
};
};
const getEmptyPageCollectionWithNext = () => {
return {
value: [],
"@odata.nextLink": "nextURL",
};
};
const truthyCallback: PageIteratorCallback = (data) => {
return true;
};
let halfWayCallbackCounter = 5;
const halfWayCallback: PageIteratorCallback = (data) => {
halfWayCallbackCounter--;
if (halfWayCallbackCounter === 0) {
return false;
} else {
return true;
}
};
describe("PageIterator.ts", () => {
describe("Constructor", () => {
it("Should create instance without nextLink", () => {
const pageIterator = new PageIterator(client, getPageCollection(), truthyCallback);
assert(pageIterator instanceof PageIterator);
});
it("Should create instance with nextLink", () => {
const pageIterator = new PageIterator(client, getPageCollectionWithNext(), truthyCallback);
assert(pageIterator instanceof PageIterator);
});
});
describe("iterate", () => {
it("Should iterate over a complete collection without nextLink", async () => {
const pageIterator = new PageIterator(client, getPageCollection(), truthyCallback);
try {
await pageIterator.iterate();
assert.isTrue(pageIterator.isComplete());
} catch (error) {
throw error;
}
});
it("Should not iterate over an empty collection", async () => {
const pageIterator = new PageIterator(client, getEmptyPageCollection(), truthyCallback);
halfWayCallbackCounter = 1;
try {
await pageIterator.iterate();
assert.equal(halfWayCallbackCounter, 1);
} catch (error) {
throw error;
}
});
it("Should break in the middle way", async () => {
const pageIterator = new PageIterator(client, getPageCollection(), halfWayCallback);
halfWayCallbackCounter = 5;
try {
await pageIterator.iterate();
assert.isFalse(pageIterator.isComplete());
} catch (error) {
throw error;
}
});
});
/* tslint:disable: no-string-literal */
describe("iterationHelper", () => {
it("Should return true for empty collection with next link", () => {
const pageIterator = new PageIterator(client, getEmptyPageCollectionWithNext(), truthyCallback);
try {
const advance = pageIterator["iterationHelper"]();
assert.isTrue(advance);
} catch (error) {
throw error;
}
});
});
describe("resume", () => {
it("Should start from the place where it left the iteration", async () => {
const pageIterator = new PageIterator(client, getPageCollection(), halfWayCallback);
halfWayCallbackCounter = 5;
try {
await pageIterator.iterate();
assert.isFalse(pageIterator.isComplete());
await pageIterator.resume();
assert.isTrue(pageIterator.isComplete());
} catch (error) {
throw error;
}
});
});
describe("isComplete", () => {
it("Should return false for incomplete iteration", async () => {
const pageIterator = new PageIterator(client, getPageCollection(), halfWayCallback);
halfWayCallbackCounter = 5;
try {
await pageIterator.iterate();
assert.isFalse(pageIterator.isComplete());
} catch (error) {
throw error;
}
});
it("Should return true for complete iteration", async () => {
const pageIterator = new PageIterator(client, getPageCollection(), truthyCallback);
try {
await pageIterator.iterate();
assert.isTrue(pageIterator.isComplete());
} catch (error) {
throw error;
}
});
});
/* tslint:enable: no-string-literal */
});