Skip to content

Commit 5a6825c

Browse files
author
Fletcher91
committed
[FIX] Improve test suite
1 parent 3ff8819 commit 5a6825c

10 files changed

+106
-142
lines changed

__mocks__/solid-auth-client.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* The `rdflib` fetcher module doesn't use spec compliant implementations provided by the
3+
* ecosystem.. so we need to do some overrides again..
4+
* @module SolidAuthClient
5+
*/
6+
7+
module.exports = undefined;

jest-plugins.js

-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
11
// Write jest like rspec tests
22
// https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugins
33
global.fetch = require('jest-fetch-mock');
4-
5-
require('jest-plugins')([
6-
'jest-plugin-action',
7-
'jest-plugin-context',
8-
'jest-plugin-its',
9-
'jest-plugin-set',
10-
]);

package.json

+9-7
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,29 @@
4343
"http-status-codes": ">= 1.x",
4444
"jest": "^23.6.0",
4545
"jest-fetch-mock": "^1.6.6",
46-
"jest-plugins": "^2.9.0",
47-
"jest-plugins-recommended": "^2.9.0",
4846
"rdflib": ">= 0.17.x",
4947
"rollup": "^0.66.2",
5048
"rollup-plugin-commonjs": "^9.1.8",
5149
"rollup-plugin-node-resolve": "^3.4.0",
5250
"rollup-plugin-sourcemaps": "^0.4.2",
5351
"rollup-plugin-typescript2": "^0.17.0",
54-
"ts-jest": "=23.1.4",
52+
"ts-jest": "^23.10.2",
5553
"tslint": "^5.11.0",
5654
"typedoc": "^0.12.0",
5755
"typescript": "^3.0.3"
5856
},
5957
"jest": {
6058
"automock": false,
59+
"coveragePathIgnorePatterns": [
60+
"/node_modules/",
61+
"src/utilities/DisjointSet.ts"
62+
],
6163
"coverageThreshold": {
6264
"global": {
63-
"branches": 64,
64-
"functions": 76,
65-
"lines": 76,
66-
"statements": 75
65+
"branches": 65,
66+
"functions": 77,
67+
"lines": 79,
68+
"statements": 79
6769
}
6870
},
6971
"setupTestFrameworkScriptFile": "./jest-plugins",

rollup.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default {
1111
"ml-disjoint-set",
1212
"node-fetch",
1313
"rdflib",
14+
"solid-auth-client",
1415
],
1516
input: "src/link-lib.ts",
1617
output: [

src/__tests__/LinkedRenderStore.spec.ts

-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ describe("LinkedRenderStore", () => {
200200
store.store.addStatements(actionStatements);
201201

202202
it("sends the described request", async () => {
203-
fetch.mockResponseOnce("", {});
204203
const sub = jest.fn();
205204
store.lrs.subscribe({ callback: sub, onlySubjects: false });
206205

src/processor/RequestInitGenerator.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
export interface RequestInitGeneratorOpts {
2+
credentials: string;
3+
csrfFieldName: string;
4+
mode: string;
5+
xRequestedWith: string;
6+
}
7+
18
export class RequestInitGenerator {
29
public readonly credentials: string;
310
public readonly csrfFieldName: string;
411
public readonly mode: string;
512
public readonly xRequestedWith: string;
613

7-
constructor(opts = {
14+
constructor(opts: RequestInitGeneratorOpts = {
815
credentials: "include",
916
csrfFieldName: "csrf-token",
1017
mode: "same-origin",

src/processor/__tests__/ProcessorError.spec.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@ import "jest";
33

44
import { ProcessorError } from "../ProcessorError";
55

6-
describe("ProcessorError", () => {
7-
set("msg", () => undefined);
8-
set("response", () => undefined);
9-
subject(() => new ProcessorError(msg, response));
6+
const getMessage = (msg: string, response?: Response): ProcessorError => new ProcessorError(msg, response);
107

11-
its("message", () => isExpected.toEqual(""));
8+
const r = new Response();
129

10+
describe("ProcessorError", () => {
1311
describe("with message", () => {
14-
set("msg", () => "info");
15-
16-
its("message", () => isExpected.toEqual("info"));
12+
it("has a message", () => {
13+
expect(getMessage("info", undefined)).toHaveProperty("message", "info");
14+
expect(getMessage("info", undefined)).not.toHaveProperty("response", r);
15+
});
1716
});
1817

1918
describe("with response", () => {
20-
set("response", () => "info");
21-
22-
its("response", () => isExpected.toEqual("info"));
19+
it("has a message", () => {
20+
expect(getMessage("info", r)).toHaveProperty("response", r);
21+
});
2322
});
2423
});
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,51 @@
11
/* @globals set, generator, init */
22
import "jest";
33

4-
import { RequestInitGenerator } from "../RequestInitGenerator";
4+
import { RequestInitGenerator, RequestInitGeneratorOpts } from "../RequestInitGenerator";
55

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);
128

9+
describe("RequestInitGenerator", () => {
1310
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+
});
1715
});
1816

1917
describe("#authenticityHeader", () => {
20-
subject(() => generator.authenticityHeader());
18+
it("has the correct X-Requested-With header", () => {
19+
expect(getGenerator()).toHaveProperty("xRequestedWith", "XMLHttpRequest");
20+
});
2121

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+
});
2425
});
2526

2627
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+
});
3541

3642
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"));
3946

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"));
4249
});
4350
});
4451
});

src/utilities/__tests__/responses.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ describe("responses", () => {
9292
"Content-Type": "text/turtle",
9393
},
9494
});
95+
// @ts-ignore
9596
response.headers = {
9697
get: (header: string): string | undefined => {
9798
if (header === "Content-Type") {

0 commit comments

Comments
 (0)