|
1 | 1 | /* @globals set, generator, init */
|
2 | 2 | import "jest";
|
3 | 3 |
|
4 |
| -import { RequestInitGenerator } from "../RequestInitGenerator"; |
| 4 | +import { RequestInitGenerator, RequestInitGeneratorOpts } from "../RequestInitGenerator"; |
5 | 5 |
|
6 |
| -describe("RequestInitGenerator", () => { |
7 |
| - set("opts", () => undefined); |
8 |
| - set("generator", () => new RequestInitGenerator(opts)); |
9 |
| - subject(() => generator); |
10 |
| - set("method", () => undefined); |
11 |
| - set("accept", () => undefined); |
| 6 | +const getGenerator = (opts?: Partial<RequestInitGeneratorOpts>): RequestInitGenerator => |
| 7 | + new RequestInitGenerator(opts as RequestInitGeneratorOpts); |
12 | 8 |
|
| 9 | +describe("RequestInitGenerator", () => { |
13 | 10 | describe("#constructor", () => {
|
14 |
| - set("opts", () => ({ csrfFieldName: "custom-element ", mode: "no-cors" })); |
15 |
| - |
16 |
| - its("mode", () => isExpected.toEqual("no-cors")); |
| 11 | + it("sets the mode", () => { |
| 12 | + const subject = getGenerator({ csrfFieldName: "custom-element ", mode: "no-cors" }); |
| 13 | + expect(subject).toHaveProperty("mode", "no-cors"); |
| 14 | + }); |
17 | 15 | });
|
18 | 16 |
|
19 | 17 | describe("#authenticityHeader", () => {
|
20 |
| - subject(() => generator.authenticityHeader()); |
| 18 | + it("has the correct X-Requested-With header", () => { |
| 19 | + expect(getGenerator()).toHaveProperty("xRequestedWith", "XMLHttpRequest"); |
| 20 | + }); |
21 | 21 |
|
22 |
| - its("X-Requested-With", () => isExpected.toEqual("XMLHttpRequest")); |
23 |
| - its("X-CSRF-Token", () => isExpected.toEqual("")); |
| 22 | + it("has no X-CSRF-Token header", () => { |
| 23 | + expect(getGenerator()).not.toHaveProperty("X-CSRF-Token"); |
| 24 | + }); |
24 | 25 | });
|
25 | 26 |
|
26 | 27 | describe("#generate", () => {
|
27 |
| - subject(() => generator.generate(method, accept)); |
28 |
| - |
29 |
| - its("credentials", () => isExpected.toEqual("include")); |
30 |
| - its("method", () => isExpected.toEqual("GET")); |
31 |
| - its("mode", () => isExpected.toEqual("same-origin")); |
32 |
| - |
33 |
| - its("headers.Accept", () => isExpected.toEqual("text/turtle")); |
34 |
| - its("headers.X-Requested-With", () => isExpected.toEqual("XMLHttpRequest")); |
| 28 | + describe("with empty parameters", () => { |
| 29 | + const subject = getGenerator().generate(undefined, undefined); |
| 30 | + |
| 31 | + it("sets the credentials option", () => expect(subject).toHaveProperty("credentials", "include")); |
| 32 | + it("sets the method option", () => expect(subject).toHaveProperty("method", "GET")); |
| 33 | + it("sets the mode option", () => expect(subject).toHaveProperty("mode", "same-origin")); |
| 34 | + |
| 35 | + const headers = subject.headers; |
| 36 | + it("sets the Accept header", () => expect(headers).toHaveProperty("Accept", "text/turtle")); |
| 37 | + it("sets the X-Requested-With header", () => { |
| 38 | + expect(headers).toHaveProperty("X-Requested-With", "XMLHttpRequest"); |
| 39 | + }); |
| 40 | + }); |
35 | 41 |
|
36 | 42 | describe("with arguments", () => {
|
37 |
| - set("method", () => "POST"); |
38 |
| - set("accept", () => "application/n-quads"); |
| 43 | + const subject = getGenerator().generate("POST", "application/n-quads"); |
| 44 | + |
| 45 | + it("sets the method option", () => expect(subject).toHaveProperty("method", "POST")); |
39 | 46 |
|
40 |
| - its("method", () => isExpected.toEqual("POST")); |
41 |
| - its("headers.Accept", () => isExpected.toEqual("application/n-quads")); |
| 47 | + const headers = subject.headers; |
| 48 | + it("sets the Accept header", () => expect(headers).toHaveProperty("Accept", "application/n-quads")); |
42 | 49 | });
|
43 | 50 | });
|
44 | 51 | });
|
0 commit comments