From 9f478ae4cc1ec875a3fadca87882fd25babb0540 Mon Sep 17 00:00:00 2001 From: jessezhang91 Date: Fri, 27 Oct 2017 23:40:56 -0400 Subject: [PATCH] [mock-knex] add types for mock-knex --- types/mock-knex/index.d.ts | 79 ++++++++++++++++++++++++++++++ types/mock-knex/mock-knex-tests.ts | 29 +++++++++++ types/mock-knex/tsconfig.json | 23 +++++++++ types/mock-knex/tslint.json | 1 + 4 files changed, 132 insertions(+) create mode 100644 types/mock-knex/index.d.ts create mode 100644 types/mock-knex/mock-knex-tests.ts create mode 100644 types/mock-knex/tsconfig.json create mode 100644 types/mock-knex/tslint.json diff --git a/types/mock-knex/index.d.ts b/types/mock-knex/index.d.ts new file mode 100644 index 0000000000..5633000ab9 --- /dev/null +++ b/types/mock-knex/index.d.ts @@ -0,0 +1,79 @@ +// Type definitions for mock-knex 0.3 +// Project: https://github.com/colonyamerican/mock-knex +// Definitions by: Jesse Zhang +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +/// + +import * as Knex from "knex"; +import { EventEmitter } from "events"; + +/** + * Attaches mocked client to knex instance + * + * @param knex initialized knex client + */ +export function mock(knex: Knex): void; + +/** + * Detaches mocked client from knex instance + * + * @param knex initialized knex client + */ +export function unmock(knex: Knex): void; + +/** + * Returns query Tracker instance + */ +export function getTracker(): Tracker; + +/** + * The tracker enables you to catch and respond to queries that occur during testing, see Test for more examples. + */ +export interface Tracker extends EventEmitter { + /** + * Enables query tracking mock on mocked knex client + */ + install(): void; + + /** + * Disables query tracking mock on mocked knex client. Also resets 'step' counter. + */ + uninstall(): void; + + /** + * Add event listener for 'query' event. It gets esecuted for each query that should end up in database. + * Instead of this callback gets executed and its up to you to assert queries and mock database responses. + * + * @param callback A function that gets executed on 'query' event. + */ + on(event: 'query', callback: (query: QueryDetails, step: number) => void): this; +} + +/** + * The object containing query details that is being sent to knex database dialect on query execution. + * Object properties signature matches with knex toSQL() output with additional method returns(values). + */ +export interface QueryDetails extends Knex.Sql { + /** + * Function that needs to be called to mock database query result for knex. + * + * @param error The Error, string or instance of Error, which represents why the result was rejected + */ + reject(error: Error | string): void; + + /** + * Function that needs to be called to mock database query result for knex. + * + * @param values An array of mock data to be returned by database. For Bookshelf this is mostly array of objects. Knex could return any type of data. + */ + response(values: any[], options?: QueryDetailsResponseOption): void; +} + +export interface QueryDetailsResponseOption { + /** + * Is this a stream response, defaults to false + */ + stream: boolean; +} diff --git a/types/mock-knex/mock-knex-tests.ts b/types/mock-knex/mock-knex-tests.ts new file mode 100644 index 0000000000..ec4ef0024e --- /dev/null +++ b/types/mock-knex/mock-knex-tests.ts @@ -0,0 +1,29 @@ +import * as knex from "knex"; +import * as mockDb from "mock-knex"; + +const db = knex({ + client: 'sqlite' +}); + +mockDb.mock(db); + +const tracker = mockDb.getTracker(); +tracker.install(); +tracker.on('query', (query, step) => { + if (query.method === "first" || step === 1) { + query.response([{ + a: 1 + }, { + a: 2 + }, { + a: 3 + }], { + stream: false + }); + } else { + query.reject(new Error("bad query")); + } +}); +tracker.uninstall(); + +mockDb.unmock(db); diff --git a/types/mock-knex/tsconfig.json b/types/mock-knex/tsconfig.json new file mode 100644 index 0000000000..427ad9f843 --- /dev/null +++ b/types/mock-knex/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "mock-knex-tests.ts" + ] +} diff --git a/types/mock-knex/tslint.json b/types/mock-knex/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/mock-knex/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }