Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
tests(core): simplifying tests
Browse files Browse the repository at this point in the history
Using json-mock-extended to make the tests easier to write.
  • Loading branch information
markmcdowell committed Apr 21, 2020
1 parent 3b0cdd9 commit 32df39c
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 69 deletions.
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"helmet": "^3.22.0",
"ix": "^2.5.5",
"jest": "^25.4.0",
"jest-mock-extended": "^1.0.8",
"js-yaml": "^3.13.1",
"lerna": "^3.20.2",
"npm-run-all": "^4.1.5",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { mock } from "jest-mock-extended";

import { DirectoryConfigurationLoader } from "../../../src/configuration/loaders/directoryConfigurationLoader";
import { IConfigurationLoader } from "../../../src/configuration/loaders/iConfigurationLoader";

// tslint:disable:variable-name
describe("canLoad", () => {

describe("can load from", () => {

test("directory", () => {

const fileLoader = new (jest.fn<IConfigurationLoader<{}>, string[]>())();
const fileLoader = mock<IConfigurationLoader<{}>>();

const loader = new DirectoryConfigurationLoader(fileLoader);

Expand All @@ -21,7 +22,7 @@ describe("canLoad", () => {

test("http", () => {

const fileLoader = new (jest.fn<IConfigurationLoader<{}>, string[]>())();
const fileLoader = mock<IConfigurationLoader<{}>>();

const loader = new DirectoryConfigurationLoader(fileLoader);

Expand All @@ -31,7 +32,7 @@ describe("canLoad", () => {

test("https", () => {

const fileLoader = new (jest.fn<IConfigurationLoader<{}>, string[]>())();
const fileLoader = mock<IConfigurationLoader<{}>>();

const loader = new DirectoryConfigurationLoader(fileLoader);

Expand All @@ -41,7 +42,7 @@ describe("canLoad", () => {

test("an extension", () => {

const fileLoader = new (jest.fn<IConfigurationLoader<{}>, string[]>())();
const fileLoader = mock<IConfigurationLoader<{}>>();

const loader = new DirectoryConfigurationLoader(fileLoader);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { mock } from "jest-mock-extended";

import { LocalFileConfigurationLoader } from "../../../src/configuration/loaders/localFileConfigurationLoader";
import { IConfigurationParser } from "../../../src/configuration/parsers/iConfigurationParser";

// tslint:disable:variable-name
describe("canLoad", () => {

describe("can load from", () => {

test("extension", () => {

const parser = new (jest.fn<IConfigurationParser<{}>, string[]>())();
const parser = mock<IConfigurationParser<{}>>();

const loader = new LocalFileConfigurationLoader(parser, "yaml");

Expand All @@ -18,7 +19,7 @@ describe("canLoad", () => {

test("a local file", () => {

const parser = new (jest.fn<IConfigurationParser<{}>, string[]>())();
const parser = mock<IConfigurationParser<{}>>();

const loader = new LocalFileConfigurationLoader(parser, "yaml");

Expand All @@ -28,7 +29,7 @@ describe("canLoad", () => {

test("a network file", () => {

const parser = new (jest.fn<IConfigurationParser<{}>, string[]>())();
const parser = mock<IConfigurationParser<{}>>();

const loader = new LocalFileConfigurationLoader(parser, "yaml");

Expand All @@ -41,7 +42,7 @@ describe("canLoad", () => {

test("http", () => {

const parser = new (jest.fn<IConfigurationParser<{}>, string[]>())();
const parser = mock<IConfigurationParser<{}>>();

const loader = new LocalFileConfigurationLoader(parser, "json");

Expand All @@ -51,7 +52,7 @@ describe("canLoad", () => {

test("https", () => {

const parser = new (jest.fn<IConfigurationParser<{}>, string[]>())();
const parser = mock<IConfigurationParser<{}>>();

const loader = new LocalFileConfigurationLoader(parser, "yaml");

Expand All @@ -61,7 +62,7 @@ describe("canLoad", () => {

test("an unknown extension", () => {

const parser = new (jest.fn<IConfigurationParser<{}>, string[]>())();
const parser = mock<IConfigurationParser<{}>>();

const loader = new LocalFileConfigurationLoader(parser, "yaml");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { mock } from "jest-mock-extended";

import { RemoteFileConfigurationLoader } from "../../../src/configuration/loaders/remoteFileConfigurationLoader";
import { IConfigurationParser } from "../../../src/configuration/parsers/iConfigurationParser";

// tslint:disable:variable-name
describe("canLoad", () => {

describe("can load from", () => {

test("http", () => {

const parser = new (jest.fn<IConfigurationParser<{}>, string[]>())();
const parser = mock<IConfigurationParser<{}>>();

const loader = new RemoteFileConfigurationLoader(parser, "yaml");

Expand All @@ -18,7 +19,7 @@ describe("canLoad", () => {

test("https", () => {

const parser = new (jest.fn<IConfigurationParser<{}>, string[]>())();
const parser = mock<IConfigurationParser<{}>>();

const loader = new RemoteFileConfigurationLoader(parser, "yaml");

Expand All @@ -31,7 +32,7 @@ describe("canLoad", () => {

test("a local file", () => {

const parser = new (jest.fn<IConfigurationParser<{}>, string[]>())();
const parser = mock<IConfigurationParser<{}>>();

const loader = new RemoteFileConfigurationLoader(parser, "yaml");

Expand All @@ -40,7 +41,7 @@ describe("canLoad", () => {
});
test("a network file", () => {

const parser = new (jest.fn<IConfigurationParser<{}>, string[]>())();
const parser = mock<IConfigurationParser<{}>>();

const loader = new RemoteFileConfigurationLoader(parser, "yaml");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { mock } from "jest-mock-extended";

import { CompositeConfigurationWriter } from "../../../src/configuration/writers/compositeConfigurationWriter";
import { IConfigurationWriter } from "../../../src/configuration/writers/iConfigurationWriter";

describe("canWrite", () => {

describe("can write", () => {

test("when 1 writer can", () => {
const canWrite = jest.fn(() => ({
canWrite: jest.fn(() => true),
write: jest.fn(),
}));

const jsonWriter = new canWrite();
const jsonWriter = mock<IConfigurationWriter<any>>();
jsonWriter.canWrite.mockReturnValue(true);

const writer = new CompositeConfigurationWriter(jsonWriter);

Expand All @@ -19,19 +18,11 @@ describe("canWrite", () => {
});

test("when at least 1 writer can", () => {
const cantWrite = jest.fn(() => ({
canWrite: jest.fn(() => false),
write: jest.fn(),
}));

const yamlWriter = new cantWrite();
const yamlWriter = mock<IConfigurationWriter<any>>();
yamlWriter.canWrite.mockReturnValue(false);

const canWrite = jest.fn(() => ({
canWrite: jest.fn(() => true),
write: jest.fn(),
}));

const jsonWriter = new canWrite();
const jsonWriter = mock<IConfigurationWriter<any>>();
jsonWriter.canWrite.mockReturnValue(true);

const writer = new CompositeConfigurationWriter(yamlWriter, jsonWriter);

Expand All @@ -43,19 +34,11 @@ describe("canWrite", () => {
describe("can't write", () => {

test("when all writers can't", () => {
const cantWrite = jest.fn(() => ({
canWrite: jest.fn(() => false),
write: jest.fn(),
}));

const yamlWriter = new cantWrite();

const canWrite = jest.fn(() => ({
canWrite: jest.fn(() => false),
write: jest.fn(),
}));
const yamlWriter = mock<IConfigurationWriter<any>>();
yamlWriter.canWrite.mockReturnValue(false);

const jsonWriter = new canWrite();
const jsonWriter = mock<IConfigurationWriter<any>>();
jsonWriter.canWrite.mockReturnValue(false);

const writer = new CompositeConfigurationWriter(yamlWriter, jsonWriter);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { mock } from "jest-mock-extended";

import { IConfigurationParser } from "../../../src/configuration/parsers/iConfigurationParser";
import { LocalConfigurationWriter } from "../../../src/configuration/writers/localConfigurationWriter";

Expand All @@ -6,9 +8,7 @@ describe("canWrite", () => {
describe("can write to", () => {

test("specified extension", () => {
const mockParser = jest.fn<IConfigurationParser<{}>, string[]>();

const parser = new mockParser();
const parser = mock<IConfigurationParser<{}>>();

const writer = new LocalConfigurationWriter(parser, "yaml");

Expand All @@ -20,9 +20,7 @@ describe("canWrite", () => {
describe("can't write to", () => {

test("unspecified extension", () => {
const mockParser = jest.fn<IConfigurationParser<{}>, string[]>();

const parser = new mockParser();
const parser = mock<IConfigurationParser<{}>>();

const writer = new LocalConfigurationWriter(parser, "yaml");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { mock } from "jest-mock-extended";

import { ConfigurationKind } from "../../src/configuration/configurationKind";
import { IConfiguration } from "../../src/configuration/iConfiguration";
import { ServiceHost } from "../../src/configuration/serviceHost";
Expand All @@ -11,9 +13,9 @@ describe("canLaunch", () => {

test("when kind is application", () => {

const windowFactory = new (jest.fn<IWindowFactory, string[]>())();
const windowFactory = mock<IWindowFactory>();

const logger = new (jest.fn<ILogger, string[]>())();
const logger = mock<ILogger>();

const launcher = new ApplicationLauncherService(logger, windowFactory);

Expand All @@ -35,9 +37,9 @@ describe("canLaunch", () => {
describe("can't launch", () => {

test("when kind is service", () => {
const windowFactory = new (jest.fn<IWindowFactory, string[]>())();
const windowFactory = mock<IWindowFactory>();

const logger = new (jest.fn<ILogger, string[]>())();
const logger = mock<ILogger>();

const launcher = new ApplicationLauncherService(logger, windowFactory);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { mock } from "jest-mock-extended";

import { ConfigurationKind } from "../../src/configuration/configurationKind";
import { IConfiguration } from "../../src/configuration/iConfiguration";
import { ServiceHost } from "../../src/configuration/serviceHost";
Expand All @@ -7,13 +9,13 @@ import { IWindowFactory } from "../../src/windowing/iWindowFactory";

describe("canLaunch", () => {

describe("can launcher", () => {
describe("can launch", () => {

test("when kind is service and host is electron", () => {

const windowFactory = new (jest.fn<IWindowFactory, string[]>())();
const windowFactory = mock<IWindowFactory>();

const logger = new (jest.fn<ILogger, string[]>())();
const logger = mock<ILogger>();

const launcher = new ElectronServiceLauncherService(logger, windowFactory);

Expand All @@ -36,9 +38,9 @@ describe("canLaunch", () => {
describe("can't launch", () => {

test("when host is node", () => {
const windowFactory = new (jest.fn<IWindowFactory, string[]>())();
const windowFactory = mock<IWindowFactory>();

const logger = new (jest.fn<ILogger, string[]>())();
const logger = mock<ILogger>();

const launcher = new ElectronServiceLauncherService(logger, windowFactory);

Expand All @@ -58,9 +60,9 @@ describe("canLaunch", () => {
});

test("when kind is application", () => {
const windowFactory = new (jest.fn<IWindowFactory, string[]>())();
const windowFactory = mock<IWindowFactory>();

const logger = new (jest.fn<ILogger, string[]>())();
const logger = mock<ILogger>();

const launcher = new ElectronServiceLauncherService(logger, windowFactory);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { mock } from "jest-mock-extended";

import { ConfigurationKind } from "../../src/configuration/configurationKind";
import { IConfiguration } from "../../src/configuration/iConfiguration";
import { IRegistryService } from "../../src/registry/iRegistryService";
Expand Down Expand Up @@ -27,11 +29,8 @@ describe("getRegistry", () => {
},
};

const unsortedRegistry = new (jest.fn<IRegistryService, string[]>(() => ({
getRegistry: jest.fn(() => Promise.resolve<IConfiguration[]>([application, session])),
registerConfig: jest.fn(),
registerUrl: jest.fn(),
})))();
const unsortedRegistry = mock<IRegistryService>();
unsortedRegistry.getRegistry.mockReturnValue(Promise.resolve<IConfiguration[]>([application, session]));

const service = new PriorityConfigurationRegistryService(unsortedRegistry);

Expand Down

0 comments on commit 32df39c

Please sign in to comment.