-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathqueryable.ts
119 lines (85 loc) · 3.22 KB
/
queryable.ts
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
import { expect } from "chai";
import {
Queryable,
BearerToken,
InjectHeaders,
DefaultParse,
ResolveOnData,
RejectOnError,
} from "@pnp/queryable";
import { pnpTest } from "../pnp-test.js";
describe("Queryable", function () {
it("Lifecycle works as expected", pnpTest("721e2be1-6dd2-478e-9090-64575d36859d", async function () {
const tracker = [];
const q = new Queryable("https://bing.com");
q.using(ResolveOnData(), RejectOnError());
q.on.init(() => {
tracker.push(1);
});
q.on.pre(async (url, init, result) => {
tracker.push(2);
return [url, init, result];
});
q.on.auth(async (url, init) => {
tracker.push(3);
return [url, init];
});
q.on.send(async () => {
tracker.push(4);
return new Response({} as BodyInit, {});
});
q.on.parse(async (url, response, result) => {
tracker.push(5);
return [url, response, result];
});
q.on.post(async (url, result) => {
tracker.push(6);
return [url, result];
});
q.on.dispose(() => {
tracker.push(7);
});
await q();
expect(tracker.length).to.eq(7);
expect(tracker[0]).to.eq(1);
expect(tracker[1]).to.eq(2);
expect(tracker[2]).to.eq(3);
expect(tracker[3]).to.eq(4);
expect(tracker[4]).to.eq(5);
expect(tracker[5]).to.eq(6);
expect(tracker[6]).to.eq(7);
}));
it("Observer inhertance", pnpTest("2ed992fb-4830-4e99-9989-3b6738acb666", function () {
const q = new Queryable("https://bing.com");
q.using(
BearerToken("token"),
DefaultParse(),
InjectHeaders({
"X-name": "value",
}));
const q2 = new Queryable(q);
// directly inherited with no changes should equal parent
expect((<any>q2).observers).to.be.equal((<any>q).observers);
q.on.post(async (url, result) => [url, result]);
// addition to parent should appear to child as they share a ref
expect((<any>q2).observers).to.be.equal((<any>q).observers);
q2.on.post(async (url, result) => [url, result]);
// ref to parent is broken due to edit of child
expect((<any>q2).observers).to.not.be.equal((<any>q).observers);
}));
it("Url manipulation", pnpTest("3f3439e5-d980-4f2e-b403-82efec217727", function () {
const q = new Queryable("https://bing.com");
const q2 = new Queryable(q);
expect(q.toUrl()).to.be.eq(q2.toUrl());
const q3 = new Queryable(q, "path1");
const q4 = new Queryable(q3, "path2");
expect(q3.toUrl()).to.be.eq("https://bing.com/path1");
expect(q4.toUrl()).to.be.eq("https://bing.com/path1/path2");
q4.concat("(path3)");
expect(q4.toUrl()).to.be.eq("https://bing.com/path1/path2(path3)");
q.query.set("key", "value");
expect(q.toRequestUrl()).to.be.eq("https://bing.com?key=value");
q.query.set("key2", "value2?");
expect(q.toRequestUrl()).to.be.eq("https://bing.com?key=value&key2=value2%3F");
}));
});