Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
feat: #12 add eslint for unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pamapa committed Aug 17, 2021
1 parent 40f047a commit 9e01e6f
Show file tree
Hide file tree
Showing 29 changed files with 687 additions and 669 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json"
"project": [
"./tsconfig.json",
"./test/unit/tsconfig.json"
]
},
"plugins": [
"@typescript-eslint",
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
"start": "tsdx watch",
"build": "tsdx build",
"test": "tsdx test",
"lint": "eslint ./src/**/*.ts",
"lint:fix": "eslint ./src/**/*.ts --fix",
"lint": "eslint ./src/**/*.ts ./test/unit/**/*.ts",
"sample": "concurrently --kill-others \"npm run parcel\" \"npm run server\"",
"parcel": "parcel serve ./samples/Parcel/src/index.html --no-cache",
"server": "node ./samples/Parcel/server.js"
Expand Down
2 changes: 1 addition & 1 deletion src/SigninRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SigninState } from "./SigninState";

export class SigninRequest {
public readonly url: string;
public readonly state: any;
public readonly state: SigninState;

constructor({
// mandatory
Expand Down
2 changes: 1 addition & 1 deletion src/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Log, random } from "./utils";
import { StateStore } from "./StateStore";

export class State {
public readonly id: any;
public readonly id: string;
public readonly data: any;
public readonly created: number;
public readonly request_type: any;
Expand Down
13 changes: 7 additions & 6 deletions test/unit/AccessTokenEvents.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Timer } from '../../src/utils';
import { AccessTokenEvents } from '../../src/AccessTokenEvents';
import { User } from '../../src/User';
import { Timer } from "../../src/utils";
import { AccessTokenEvents } from "../../src/AccessTokenEvents";
import { User } from "../../src/User";

describe("AccessTokenEvents", () => {

Expand All @@ -22,8 +22,7 @@ describe("AccessTokenEvents", () => {
describe("constructor", () => {

it("should use default expiringNotificationTime", () => {
// @ts-ignore
expect(subject._accessTokenExpiringNotificationTime).toEqual(60);
expect(subject["_accessTokenExpiringNotificationTime"]).toEqual(60);
});

});
Expand Down Expand Up @@ -125,7 +124,7 @@ class StubTimer extends Timer {
duration: any;

constructor(name: string) {
super(name)
super(name);
this.cancelWasCalled = false;
}

Expand All @@ -137,6 +136,8 @@ class StubTimer extends Timer {
this.cancelWasCalled = true;
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
addHandler() {}
// eslint-disable-next-line @typescript-eslint/no-empty-function
removeHandler() {}
}
18 changes: 9 additions & 9 deletions test/unit/ErrorResponse.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { ErrorResponse } from '../../src/ErrorResponse';
import { ErrorResponse } from "../../src/ErrorResponse";

describe("ErrorResponse", () => {

Expand All @@ -22,31 +22,31 @@ describe("ErrorResponse", () => {

it("should read error", () => {
// act
let subject = new ErrorResponse({error:"foo"});
const subject = new ErrorResponse({error:"foo"});

// assert
expect(subject.error).toEqual("foo");
});

it("should read error_description", () => {
// act
let subject = new ErrorResponse({error:"error", error_description:"foo"});
const subject = new ErrorResponse({error:"error", error_description:"foo"});

// assert
expect(subject.error_description).toEqual("foo");
});

it("should read error_uri", () => {
// act
let subject = new ErrorResponse({error:"error", error_uri:"foo"});
const subject = new ErrorResponse({error:"error", error_uri:"foo"});

// assert
expect(subject.error_uri).toEqual("foo");
});

it("should read state", () => {
// act
let subject = new ErrorResponse({error:"error", state:"foo"});
const subject = new ErrorResponse({error:"error", state:"foo"});

// assert
expect(subject.state).toEqual("foo");
Expand All @@ -57,15 +57,15 @@ describe("ErrorResponse", () => {
describe("message", () => {
it("should be description if set", () => {
// act
let subject = new ErrorResponse({error:"error", error_description:"foo"});
const subject = new ErrorResponse({error:"error", error_description:"foo"});

// assert
expect(subject.message).toEqual("foo");
});

it("should be error if description not set", () => {
// act
let subject = new ErrorResponse({error:"error"});
const subject = new ErrorResponse({error:"error"});

// assert
expect(subject.message).toEqual("error");
Expand All @@ -75,7 +75,7 @@ describe("ErrorResponse", () => {
describe("name", () => {
it("should be class name", () => {
// act
let subject = new ErrorResponse({error:"error"});
const subject = new ErrorResponse({error:"error"});

// assert
expect(subject.name).toEqual("ErrorResponse");
Expand All @@ -85,7 +85,7 @@ describe("ErrorResponse", () => {
describe("stack", () => {
it("should be set", () => {
// act
let subject = new ErrorResponse({error:"error"});
const subject = new ErrorResponse({error:"error"});

// assert
expect(subject.stack).not.toBeNull();
Expand Down
45 changes: 24 additions & 21 deletions test/unit/JsonService.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Log } from '../../src/utils';
import { JsonService } from '../../src/JsonService';
import { Log } from "../../src/utils";
import { JsonService } from "../../src/JsonService";

describe("JsonService", () => {
let subject: JsonService;
let fetchMock: jest.Mock<any, any>
let fetchMock: jest.Mock<any, any>;

beforeEach(() =>{
Log.logger = console;
Expand All @@ -22,44 +22,47 @@ describe("JsonService", () => {

it("should return a promise", async () => {
// act
let p = subject.getJson("http://test");
const p = subject.getJson("http://test");

// assert
expect(p).toBeInstanceOf(Promise);
// eslint-disable-next-line no-empty
try { await p; } catch {}
});

it("should make GET request to url", async () => {
// act
let p = subject.getJson("http://test");
const p = subject.getJson("http://test");

// assert
expect(fetchMock).toBeCalledWith('http://test', {
expect(fetchMock).toBeCalledWith("http://test", {
headers: {},
method: 'GET'
method: "GET"
});
// eslint-disable-next-line no-empty
try { await p; } catch {}
});

it("should set token as authorization header", async () => {
// act
let p = subject.getJson("http://test", "token");
const p = subject.getJson("http://test", "token");

// assert
expect(fetchMock).toBeCalledWith('http://test', {
headers: { Authorization: 'Bearer token' },
method: 'GET'
expect(fetchMock).toBeCalledWith("http://test", {
headers: { Authorization: "Bearer token" },
method: "GET"
});
// eslint-disable-next-line no-empty
try { await p; } catch {}
});

it("should fulfill promise when http response is 200", async () => {
// arrange
const json = { foo: 1, bar: 'test' };
const json = { foo: 1, bar: "test" };
fetchMock.mockResolvedValue({
status: 200,
headers: new Headers({
'Content-Type': 'application/json'
"Content-Type": "application/json"
}),
json: () => Promise.resolve(json)
});
Expand All @@ -84,8 +87,8 @@ describe("JsonService", () => {
fail("should not come here");
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error.message).toContain('500');
expect(error.message).toContain('server error');
expect(error.message).toContain("500");
expect(error.message).toContain("server error");
}
});

Expand All @@ -105,11 +108,11 @@ describe("JsonService", () => {

it("should reject promise when http response content type is not json", async () => {
// arrange
const json = { foo: 1, bar: 'test' };
const json = { foo: 1, bar: "test" };
fetchMock.mockResolvedValue({
status: 200,
headers: new Headers({
'Content-Type': 'text/html'
"Content-Type": "text/html"
}),
json: () => Promise.resolve(json)
});
Expand All @@ -126,18 +129,18 @@ describe("JsonService", () => {

it("should accept custom content type in response", async () => {
// arrange
subject = new JsonService(['foo/bar']);
const json = { foo: 1, bar: 'test' };
subject = new JsonService(["foo/bar"]);
const json = { foo: 1, bar: "test" };
fetchMock.mockResolvedValue({
status: 200,
headers: new Headers({
'Content-Type': 'foo/bar'
"Content-Type": "foo/bar"
}),
json: () => Promise.resolve(json)
});

// act
let result = await subject.getJson("http://test");
const result = await subject.getJson("http://test");

// assert
expect(result).toEqual(json);
Expand Down
Loading

0 comments on commit 9e01e6f

Please sign in to comment.