Skip to content

Commit

Permalink
Initial typescript declaration files
Browse files Browse the repository at this point in the history
  • Loading branch information
berniegp committed Jul 4, 2019
1 parent b438a23 commit 10b6887
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 83 deletions.
121 changes: 121 additions & 0 deletions types/MockXhr.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
export class MockXhr {
constructor();

/**
* Set the request method and url.
* https://xhr.spec.whatwg.org/#the-open()-method
*
* @param method request HTTP method (GET, POST, etc.)
* @param url request url
* @param async async request flag (only true is supported)
*/
open(method: string, url: string, async?: boolean): void;

/**
* Add a request header value.
* https://xhr.spec.whatwg.org/#the-setrequestheader()-method
*
* @param name header name
* @param value header value
*/
setRequestHeader(name: string, value: string): void;

/**
* Initiate the request.
* https://xhr.spec.whatwg.org/#the-send()-method
*
* @param body request body
*/
send(body?: any): void;

/**
* Abort the request.
* https://xhr.spec.whatwg.org/#the-abort()-method
*/
abort(): void;

/**
* Get a response header value.
* https://xhr.spec.whatwg.org/#dom-xmlhttprequest-getresponseheader
*
* @param name header name
* @returns header value
*/
getResponseHeader(name: string): string | null;

/**
* Get all response headers as a string.
* https://xhr.spec.whatwg.org/#dom-xmlhttprequest-getallresponseheaders
*
* @returns concatenated headers
*/
getAllResponseHeaders(): string;

/**
* https://xhr.spec.whatwg.org/#dom-xmlhttprequest-overridemimetype
*
* @param mime MIME type
*/
overrideMimeType(mime: string): void;

/**
* Fire a request upload progress event.
*
* @param transmitted bytes transmitted
*/
uploadProgress(transmitted: number): void;

/**
* Complete response method. Sets the response headers and body. Will set the
* state to DONE.
*
* @param status response http status (default 200)
* @param headers name-value headers (optional)
* @param body response body (default null)
* @param statusText response http status text (optional)
*/
respond(status?: number, headers?: object, body?: any, statusText?: string): void;

/**
* Set only the response headers. Will change the state to HEADERS_RECEIVED.
*
* @param status response http status (default 200)
* @param headers name-value headers (optional)
* @param statusText response http status text (optional)
*/
setResponseHeaders(status?: number, headers?: object, statusText?: string): void;

/**
* Fire a response progress event. Will set the state to LOADING.
*
* @param transmitted transmitted bytes
* @param length total bytes
*/
downloadProgress(transmitted: number, length: number): void;

/**
* Set the response body. Will set the state to DONE.
*
* @param body response body (default null)
*/
setResponseBody(body?: any): void;

/**
* Simulate a network error. Will set the state to DONE.
*/
setNetworkError(): void;

/**
* Simulate a request timeout. Will set the state to DONE.
*/
setRequestTimeout(): void;

// Global flag to enable the effects of the timeout attribute
static timeoutEnabled: boolean;

static DONE: number;
static HEADERS_RECEIVED: number;
static LOADING: number;
static OPENED: number;
static UNSENT: number;
}
114 changes: 114 additions & 0 deletions types/MockXhrServer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
export class MockXhrServer {
/**
* Constructor
*
* @param xhrMock XMLHttpRequest mock class
* @param routes routes
*/
constructor(xhrMock: MockXhr, routes?: object);

/**
* Install the server's XMLHttpRequest mock in the context. Revert with remove().
*
* @param context context object (e.g. global, window)
* @returns this
*/
install(context: object): MockXhrServer;

/**
* Remove the server as the global XMLHttpRequest mock. Reverts the actions of install(global).
*/
remove(): void;

/**
* Disable the effects of the timeout attribute on the XMLHttpRequest mock used by the server.
*/
disableTimeout(): void;

/**
* Enable the effects of the timeout attribute on the XMLHttpRequest mock used by the server.
*/
enableTimeout(): void;

/**
* Add a GET request handler.
*
* @param matcher url matcher
* @param handler request handler
* @returns this
*/
get(
matcher: string | RegExp | Function,
handler: object | Function | object[] | Function[],
): MockXhrServer;

/**
* Add a POST request handler.
*
* @param matcher url matcher
* @param handler request handler
* @returns this
*/
post(
matcher: string | RegExp | Function,
handler: object | Function | object[] | Function[],
): MockXhrServer;

/**
* Add a PUT request handler.
*
* @param matcher url matcher
* @param handler request handler
* @returns this
*/
put(
matcher: string | RegExp | Function,
handler: object | Function | object[] | Function[],
): MockXhrServer;

/**
* Add a DELETE request handler.
*
* @param matcher url matcher
* @param handler request handler
* @returns this
*/
delete(
matcher: string | RegExp | Function,
handler: object | Function | object[] | Function[],
): MockXhrServer;

/**
* Add a request handler.
*
* @param method HTTP method
* @param matcher url matcher
* @param handler request handler
* @returns this
*/
addHandler(
method: string,
matcher: string | RegExp | Function,
handler: object | Function | object[] | Function[],
): MockXhrServer;

/**
* Set the default request handler for requests that don't match any route.
*
* @param {object|Function|object[]|Function[]} handler request handler
* @returns this
*/
setDefaultHandler(handler: object | Function | object[] | Function[]): MockXhrServer;

/**
* Return 404 responses for requests that don't match any route.
*
* @returns this
*/
setDefault404(): MockXhrServer;

/**
* @returns list of requests received by the server. Entries: { method, url }
*/
getRequestLog(): object[];
}
100 changes: 18 additions & 82 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,18 @@
/** Declaration file generated by dts-gen */

export class MockXhr {
constructor(...args: any[]);

abort(...args: any[]): void;

downloadProgress(...args: any[]): void;

getAllResponseHeaders(...args: any[]): void;

getResponseHeader(...args: any[]): void;

open(...args: any[]): void;

overrideMimeType(...args: any[]): void;

respond(...args: any[]): void;

send(...args: any[]): void;

setNetworkError(...args: any[]): void;

setRequestHeader(...args: any[]): void;

setRequestTimeout(...args: any[]): void;

setResponseBody(...args: any[]): void;

setResponseHeaders(...args: any[]): void;

uploadProgress(...args: any[]): void;

static DONE: number;

static HEADERS_RECEIVED: number;

static LOADING: number;

static OPENED: number;

static UNSENT: number;

static events: string[];

static timeoutEnabled: boolean;

}

export class MockXhrServer {
constructor(...args: any[]);

addHandler(...args: any[]): void;

delete(...args: any[]): void;

disableTimeout(...args: any[]): void;

enableTimeout(...args: any[]): void;

get(...args: any[]): void;

getRequestLog(...args: any[]): void;

install(...args: any[]): void;

post(...args: any[]): void;

put(...args: any[]): void;

remove(...args: any[]): void;

setDefault404(...args: any[]): void;

setDefaultHandler(...args: any[]): void;

}

export function newMockXhr(): any;

export function newServer(routes: any): any;

import './MockXhr';
import './MockXhrServer';

/**
* Create a new "local" MockXhr subclass. This makes it easier to have self-contained unit tests
* since "global" hooks can be registered directly on the subclass. These hooks don't need to then
* be removed after tests because they are local to the new subclass.
*
* @returns new MockXhr subclass
*/
export function newMockXhr(): MockXhr;

/**
* Create a new mock server using MockXhr.
*
* @returns new mock server
*/
export function newServer(routes: any): MockXhrServer;
2 changes: 1 addition & 1 deletion types/test/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
// TODO
import * MockXMLHttpRequest from 'mock-xmlhttprequest';

0 comments on commit 10b6887

Please sign in to comment.