diff --git a/types/xhr-mock/index.d.ts b/types/xhr-mock/index.d.ts new file mode 100644 index 0000000000..2c50c5d854 --- /dev/null +++ b/types/xhr-mock/index.d.ts @@ -0,0 +1,56 @@ +// 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: 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; + } +} + +declare var mock: mock.XhrMock; +export = 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..37fc651d64 --- /dev/null +++ b/types/xhr-mock/xhr-mock-tests.ts @@ -0,0 +1,27 @@ +import mock = require('xhr-mock'); + +// 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(); + } +};