Skip to content

Instantly share code, notes, and snippets.

@voodooGQ
Forked from davidwhitney/LICENSE.txt
Created October 4, 2019 14:29
Show Gist options
  • Save voodooGQ/2cfc5a564c1f12103692a074a7006360 to your computer and use it in GitHub Desktop.
Save voodooGQ/2cfc5a564c1f12103692a074a7006360 to your computer and use it in GitHub Desktop.
A Tiny Typescript mocking... um... library? class? That doesn't annoy me, and doesn't cause Typescript linters to get confused or fill your code up with :anys. The license is MIT.
Copyright (c) 2019 David Whitney
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
/* tslint:disable:max-classes-per-file */
class InvocationStats {
public key: string;
public invocationCount: number;
constructor(key: string) { this.key = key; }
public wasCalled = (): boolean => this.invocationCount > 0;
}
class MockBehaviours {
public static returnNull = (): null => null;
public static ignoreCall = (): void => null;
public static return = <TReturnType>(value: TReturnType): TReturnType => value;
public static throw = (exception: Error = new Error("Error!")): void => { throw exception; };
}
class Mock<T> {
public untypedObject: any = {};
private readonly invocationStats: any = {};
private readonly invocationQueues: any = {};
public object = (): T => (this.untypedObject as T);
public function = (key: string): InvocationStats => this.invocationStats[key];
public when(key: string, operation?: CallableFunction) {
operation = operation == null ? MockBehaviours.ignoreCall : operation;
this.invocationQueues[key] = this.invocationQueues.hasOwnProperty(key) ? this.invocationQueues[key] : [];
this.invocationQueues[key].push(operation);
this.untypedObject[key] = () => this.returnFakeFor(key);
}
private returnFakeFor(key: string) {
this.invocationStats[key] = this.invocationStats[key] ? this.invocationStats[key] : new InvocationStats(key);
this.invocationStats[key].invocationCount++;
return this.invocationQueues[key].length === 1
? this.invocationQueues[key][0]
: this.invocationQueues[key].shift();
}
}
export { Mock, MockBehaviours as then };
import { BrowserWindow } from "electron";
import * as express from "express";
import "jest";
import * as request from "supertest";
import {Mock, then} from "./mock";
import DeviceApi from "./server";
describe("The embedded webserver", () => {
let mockBrowser: Mock<BrowserWindow>;
let expressInstance: any;
let api: DeviceApi;
let sut: any;
beforeEach(() => {
mockBrowser = new Mock<BrowserWindow>();
expressInstance = express();
api = new DeviceApi();
api.configureRoutes(expressInstance, mockBrowser.object());
sut = request(expressInstance);
});
it("Can run a test", async () => {
mockBrowser.when("reload", then.ignoreCall);
const resp = await sut.get("/test");
expect(resp.body.updated).toBe(true);
expect(mockBrowser.function("reload").wasCalled());
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment