From 6800cd1a0aa752cf6f2e05cb7d5317fd5d3856c1 Mon Sep 17 00:00:00 2001 From: Joscha Feth Date: Thu, 24 Aug 2017 17:54:13 +1000 Subject: [PATCH 1/3] add: xhr-mock --- types/xhr-mock/index.d.ts | 57 ++++++++++++++++++++++++++++++++ types/xhr-mock/tsconfig.json | 23 +++++++++++++ types/xhr-mock/tslint.json | 1 + types/xhr-mock/xhr-mock-tests.ts | 25 ++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 types/xhr-mock/index.d.ts create mode 100644 types/xhr-mock/tsconfig.json create mode 100644 types/xhr-mock/tslint.json create mode 100644 types/xhr-mock/xhr-mock-tests.ts diff --git a/types/xhr-mock/index.d.ts b/types/xhr-mock/index.d.ts new file mode 100644 index 0000000000..eacec1c5fc --- /dev/null +++ b/types/xhr-mock/index.d.ts @@ -0,0 +1,57 @@ +// Type definitions for xhr-mock 1.9 +// Project: https://github.com/jameslnewell/xhr-mock#readme +// Definitions by: Joscha Feth +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare namespace mock { + interface Headers { + [k: string]: string; + } + + interface MockResponse { + status(code: number): this; + status(): number; + statusText(statusText: string): this; + statusText(): string; + header(name: string, value: string): this; + header(name: string): string | null; + headers(obj: Headers): this; + headers(): Headers; + body(body: string): this; + body(): string; + timeout(timeout: boolean | number): this; + timeout(): boolean | number; + } + + interface MockRequest { + method(): string; + url(): string; + query(): string; + header(name: string, value: string): this; + header(name: string): string | null; + headers(obj: Headers): this; + headers(): Headers; + body(body: string): this; + body(): string; + progress(loaded: number, total: number, lengthComputable?: boolean): void; + } + + type MockFunction = (req: MockRequest, res: MockResponse) => MockResponse | null; + + interface XhrMock { + XMLHttpRequest: XMLHttpRequest; + setup(): this; + teardown(): this; + reset(): this; + mock(method: string, url: string, fn: mock.MockFunction): this; + get(url: string, fn: mock.MockFunction): this; + post(url: string, fn: mock.MockFunction): this; + put(url: string, fn: mock.MockFunction): this; + patch(url: string, fn: mock.MockFunction): this; + delete(url: string, fn: mock.MockFunction): this; + } +} + +declare var mock: mock.XhrMock; +export = mock; +export as namespace mock; diff --git a/types/xhr-mock/tsconfig.json b/types/xhr-mock/tsconfig.json new file mode 100644 index 0000000000..6a5c03268f --- /dev/null +++ b/types/xhr-mock/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "xhr-mock-tests.ts" + ] +} diff --git a/types/xhr-mock/tslint.json b/types/xhr-mock/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/xhr-mock/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/xhr-mock/xhr-mock-tests.ts b/types/xhr-mock/xhr-mock-tests.ts new file mode 100644 index 0000000000..fd26b3a43f --- /dev/null +++ b/types/xhr-mock/xhr-mock-tests.ts @@ -0,0 +1,25 @@ +// replace the real XHR object with the mock XHR object +mock.setup(); + +// create a mock response for all POST requests with the URL http://localhost/api/user +mock.post('http://localhost/api/user', (req: mock.MockRequest, res: mock.MockResponse) => { + // return null; //simulate an error + // return res.timeout(true); //simulate a timeout + + return res + .status(201) + .header('Content-Type', 'application/json') + .body(JSON.stringify({data: { + first_name: 'John', last_name: 'Smith' + }})); +}); + +// create an instance of the (mock) XHR object and use as per normal +const xhr = new XMLHttpRequest(); + +xhr.onreadystatechange = () => { + if (xhr.readyState === 4) { + // when you're finished put the real XHR object back + mock.teardown(); + } +}; From c88276a0d4b106d3ec2f6cff8f9112c41f5ac77c Mon Sep 17 00:00:00 2001 From: Joscha Feth Date: Thu, 31 Aug 2017 09:07:35 +1000 Subject: [PATCH 2/3] remove global --- types/xhr-mock/index.d.ts | 1 - types/xhr-mock/xhr-mock-tests.ts | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/types/xhr-mock/index.d.ts b/types/xhr-mock/index.d.ts index eacec1c5fc..2f85e44c8d 100644 --- a/types/xhr-mock/index.d.ts +++ b/types/xhr-mock/index.d.ts @@ -54,4 +54,3 @@ declare namespace mock { declare var mock: mock.XhrMock; export = mock; -export as namespace mock; diff --git a/types/xhr-mock/xhr-mock-tests.ts b/types/xhr-mock/xhr-mock-tests.ts index fd26b3a43f..37fc651d64 100644 --- a/types/xhr-mock/xhr-mock-tests.ts +++ b/types/xhr-mock/xhr-mock-tests.ts @@ -1,3 +1,5 @@ +import mock = require('xhr-mock'); + // replace the real XHR object with the mock XHR object mock.setup(); From 2091f460a58119538bf48e3d43685c4f5e87c8c5 Mon Sep 17 00:00:00 2001 From: Joscha Feth Date: Tue, 5 Sep 2017 12:36:18 +1000 Subject: [PATCH 3/3] remove qualifiers --- types/xhr-mock/index.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/types/xhr-mock/index.d.ts b/types/xhr-mock/index.d.ts index 2f85e44c8d..2c50c5d854 100644 --- a/types/xhr-mock/index.d.ts +++ b/types/xhr-mock/index.d.ts @@ -43,12 +43,12 @@ declare namespace mock { setup(): this; teardown(): this; reset(): this; - mock(method: string, url: string, fn: mock.MockFunction): this; - get(url: string, fn: mock.MockFunction): this; - post(url: string, fn: mock.MockFunction): this; - put(url: string, fn: mock.MockFunction): this; - patch(url: string, fn: mock.MockFunction): this; - delete(url: string, fn: mock.MockFunction): this; + mock(method: string, url: string, fn: MockFunction): this; + get(url: string, fn: MockFunction): this; + post(url: string, fn: MockFunction): this; + put(url: string, fn: MockFunction): this; + patch(url: string, fn: MockFunction): this; + delete(url: string, fn: MockFunction): this; } }