Skip to content

Commit

Permalink
Add tsd for type declaration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasz-zablocki authored and berniegp committed Aug 19, 2019
1 parent 10b6887 commit 672238d
Show file tree
Hide file tree
Showing 10 changed files with 2,239 additions and 96 deletions.
2,174 changes: 2,130 additions & 44 deletions package-lock.json

Large diffs are not rendered by default.

21 changes: 17 additions & 4 deletions package.json
Expand Up @@ -5,18 +5,20 @@
"main": "dist/mock-xmlhttprequest.common.js",
"module": "dist/mock-xmlhttprequest.esm.js",
"unpkg": "dist/mock-xmlhttprequest.js",
"typings": "types/index.d.ts",
"types": "types/index.d.ts",
"files": [
"dist/**/*",
"types/*.d.ts"
"types/index.d.ts",
"types/MockXhr.d.ts",
"types/MockXhrServer.d.ts"
],
"scripts": {
"dev:dist": "rollup -wm -c build/rollup.dev.config.js",
"build": "node build/build.js",
"lint": "eslint src test",
"test": "npm run test:unit && npm run test:types",
"test:unit": "mocha test --recursive --require esm",
"test:types": "tsc -p types/test",
"test:types": "tsd",
"posttest": "npm run lint",
"prepare": "npm run build",
"prepublishOnly": "npm run test"
Expand Down Expand Up @@ -50,10 +52,21 @@
"rollup": "^1.16.4",
"rollup-plugin-buble": "^0.19.8",
"rollup-plugin-replace": "^2.2.0",
"typescript": "^3.5.3",
"terser": "^4.0.2",
"typescript": "^3.4.5"
"tsd": "^0.7.4"
},
"engines": {
"node": ">=6.0.0"
},
"tsd": {
"directory": "types/test",
"compilerOptions": {
"module": "es2015",
"target": "es5",
"lib": [
"dom"
]
}
}
}
14 changes: 6 additions & 8 deletions types/MockXhr.d.ts
@@ -1,6 +1,4 @@
export class MockXhr {
constructor();

export default class MockXhr {
/**
* Set the request method and url.
* https://xhr.spec.whatwg.org/#the-open()-method
Expand Down Expand Up @@ -113,9 +111,9 @@ export class MockXhr {
// 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;
static UNSENT: 0
static OPENED: 1
static HEADERS_RECEIVED: 2
static LOADING: 3
static DONE: 4
}
73 changes: 54 additions & 19 deletions types/MockXhrServer.d.ts
@@ -1,3 +1,5 @@
import MockXhr from "./MockXhr"

export class MockXhrServer {
/**
* Constructor
Expand All @@ -13,7 +15,7 @@ export class MockXhrServer {
* @param context context object (e.g. global, window)
* @returns this
*/
install(context: object): MockXhrServer;
install(context: object): this;

/**
* Remove the server as the global XMLHttpRequest mock. Reverts the actions of install(global).
Expand All @@ -38,9 +40,9 @@ export class MockXhrServer {
* @returns this
*/
get(
matcher: string | RegExp | Function,
handler: object | Function | object[] | Function[],
): MockXhrServer;
matcher: MockXhrServer.UrlMatcher,
handler: MockXhrServer.Handler
): this;

/**
* Add a POST request handler.
Expand All @@ -50,9 +52,9 @@ export class MockXhrServer {
* @returns this
*/
post(
matcher: string | RegExp | Function,
handler: object | Function | object[] | Function[],
): MockXhrServer;
matcher: MockXhrServer.UrlMatcher,
handler: MockXhrServer.Handler
): this;

/**
* Add a PUT request handler.
Expand All @@ -62,9 +64,9 @@ export class MockXhrServer {
* @returns this
*/
put(
matcher: string | RegExp | Function,
handler: object | Function | object[] | Function[],
): MockXhrServer;
matcher: MockXhrServer.UrlMatcher,
handler: MockXhrServer.Handler
): this;

/**
* Add a DELETE request handler.
Expand All @@ -74,9 +76,9 @@ export class MockXhrServer {
* @returns this
*/
delete(
matcher: string | RegExp | Function,
handler: object | Function | object[] | Function[],
): MockXhrServer;
matcher: MockXhrServer.UrlMatcher,
handler: MockXhrServer.Handler
): this;

/**
* Add a request handler.
Expand All @@ -88,27 +90,60 @@ export class MockXhrServer {
*/
addHandler(
method: string,
matcher: string | RegExp | Function,
handler: object | Function | object[] | Function[],
): MockXhrServer;
matcher: MockXhrServer.UrlMatcher,
handler: MockXhrServer.Handler
): this;

/**
* 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;
setDefaultHandler(handler: MockXhrServer.Handler): this;

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

/**
* @returns list of requests received by the server. Entries: { method, url }
*/
getRequestLog(): object[];
getRequestLog(): MockXhrServer.RequestLog;
}

export namespace MockXhrServer {
type UrlMatcher =
((url: string) => boolean)
| string
| RegExp

interface MockResponseHandler {
status: number;
statusText: string;
headers: Record<string, string>;
body: string;
}

interface MockCallbackHandler {
(xhr: MockXhr): void
}

type Handler =
(Partial<MockResponseHandler> | MockCallbackHandler)
| (Partial<MockResponseHandler> | MockCallbackHandler)[]

interface RequestLogEntry {
method: string;
url: string;
headers: Record<string, string>;
body?: any
}

type RequestLog = ReadonlyArray<RequestLogEntry>
}

export default MockXhrServer
15 changes: 11 additions & 4 deletions types/index.d.ts
@@ -1,5 +1,5 @@
import './MockXhr';
import './MockXhrServer';
import MockXhr from "./MockXhr"
import MockXhrServer from "./MockXhrServer"

/**
* Create a new "local" MockXhr subclass. This makes it easier to have self-contained unit tests
Expand All @@ -8,11 +8,18 @@ import './MockXhrServer';
*
* @returns new MockXhr subclass
*/
export function newMockXhr(): MockXhr;
declare function newMockXhr(): MockXhr;

/**
* Create a new mock server using MockXhr.
*
* @returns new mock server
*/
export function newServer(routes: any): MockXhrServer;
declare function newServer(routes: any): MockXhrServer;

export {
MockXhr,
MockXhrServer,
newServer,
newMockXhr
}
9 changes: 9 additions & 0 deletions types/test/MockXhrServer.test-d.ts
@@ -0,0 +1,9 @@
import { expectError, expectType } from 'tsd'
import MockXhrServer from '../MockXhrServer'

expectType<MockXhrServer.UrlMatcher>('http://foo/bar.com')
expectType<MockXhrServer.UrlMatcher>(/http:/)
expectType<MockXhrServer.UrlMatcher>((url: string) => url === 'http://foo/bar.com')

expectError<MockXhrServer.UrlMatcher>((url: string) => url)
expectError<MockXhrServer.UrlMatcher>(true)
7 changes: 7 additions & 0 deletions types/test/XMLHttpRequest-compat.test-d.ts
@@ -0,0 +1,7 @@
import { expectError } from 'tsd'
import { newMockXhr } from '../'

const xhr = newMockXhr()

//can be used to validate structural compliance with XMLHttpRequest interface
expectError<XMLHttpRequest>(xhr) //not yet compatible
5 changes: 5 additions & 0 deletions types/test/index.test-d.ts
@@ -0,0 +1,5 @@
import { expectType } from 'tsd'
import { MockXhr, MockXhrServer, newMockXhr, newServer } from '../'

expectType<MockXhr>(newMockXhr())
expectType<MockXhrServer>(newServer(newMockXhr()))
1 change: 0 additions & 1 deletion types/test/index.ts

This file was deleted.

16 changes: 0 additions & 16 deletions types/test/tsconfig.json

This file was deleted.

0 comments on commit 672238d

Please sign in to comment.