-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.test.js
More file actions
61 lines (49 loc) · 1.89 KB
/
fetch.test.js
File metadata and controls
61 lines (49 loc) · 1.89 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
const sinon = require("sinon");
const expect = require("chai").expect;
const request = require("./request");
const FormData = require("form-data");
const {isNode, isBrowser} = require("browser-or-node");
const {fetch} = require("./fetch");
const fakersm = require("./fake").response;
const RSM_HOST = "http://localhost";
function compareStreams(form1, form2) {
let stream1, stream2;
if (isNode) {
stream1 = form1._streams.filter((s) => (typeof s) === "string")
.map((s) => s.replace(form1._boundary, ""));
stream2 = form2._streams.filter((s) => (typeof s) === "string")
.map((s) => s.replace(form2._boundary, ""));
} else if (isBrowser) {
stream1 = Array.from(form1.entries());
stream2 = Array.from(form2.entries());
} else {
throw new Error("Test only works on browser or node!")
}
expect(stream1).to.deep.equal(stream2);
return true
}
describe("RSM Fetch", () => {
let sandbox = sinon.createSandbox();
afterEach(() => sandbox.restore());
it("should send correct request", async () => {
const stub = sandbox.stub(request, "fetch")
.resolves({text: () => fakersm([])});
const formdata = new FormData();
formdata.append("Test", "Albert");
formdata.append("Data", "Einstein");
await fetch(RSM_HOST, "/target", {"Test": "Albert", "Data": "Einstein"});
expect(stub.calledOnceWithExactly(RSM_HOST + "/target",
sinon.match({
headers: sinon.match({"content-type": sinon.match("multipart/form-data")}),
body: sinon.match((data) => compareStreams(data, formdata)),
})
)).to.be.true;
});
it("should parse a DOM removing <br> tags", async () => {
const stub = sandbox.stub(request, "fetch")
.resolves({text: () => "<p id='main'>Hi, I'm<br>Tuna Man</p>"});
const res = await fetch(RSM_HOST, "/whatever", {});
expect(res.getElementById("main").textContent)
.to.equal("Hi, I'm\nTuna Man")
});
});