mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-06-28 22:30:01 +00:00
protractor-http-mock 0.10.0 (#40051)
* Allow registering mixed types of mocks. * Allow registering mixed types of plugins. * Update the configuration type. * Update request operation types. * Be more specific in plugins' match() signature.
This commit is contained in:
committed by
Nathan Shively-Sanders
parent
5b30992fd5
commit
756751f416
124
types/protractor-http-mock/index.d.ts
vendored
124
types/protractor-http-mock/index.d.ts
vendored
@@ -1,6 +1,7 @@
|
||||
// Type definitions for protractor-http-mock
|
||||
// Type definitions for protractor-http-mock 0.10
|
||||
// Project: https://github.com/atecarlos/protractor-http-mock
|
||||
// Definitions by: Crevil <https://github.com/Crevil>
|
||||
// Adam Kwiatek <https://github.com/akwiatek>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.1
|
||||
|
||||
@@ -11,38 +12,11 @@ declare namespace mock {
|
||||
/**
|
||||
* Instantiate mock module. This must be done before the browser connects.
|
||||
*
|
||||
* @param mocks An array of mock modules to load into the application.
|
||||
* @param plugins An array of Plugin objects.
|
||||
* @param mocks An array of either mock modules or module names relative to the rootDirectory configuration to load into the application.
|
||||
* @param plugins An array of either Plugin objects or NPM modules as strings.
|
||||
* @param skipDefaults Set true to skip loading of default mocks.
|
||||
*/
|
||||
<TResponse, TPayload>(mocks?: Array<requests.AllRequests<TResponse, TPayload>>, plugins?: Array<Plugin>, skipDefaults?: boolean): ProtractorHttpMock;
|
||||
|
||||
/**
|
||||
* Instantiate mock module. This must be done before the browser connects.
|
||||
*
|
||||
* @param mocks An array of mock modules to load into the application.
|
||||
* @param plugins An array of NPM modules as strings.
|
||||
* @param skipDefaults Set true to skip loading of default mocks.
|
||||
*/
|
||||
<TResponse, TPayload>(mocks?: Array<requests.AllRequests<TResponse, TPayload>>, plugins?: Array<string>, skipDefaults?: boolean): ProtractorHttpMock;
|
||||
|
||||
/**
|
||||
* Instantiate mock modules from files. This must be done before the browser connects.
|
||||
*
|
||||
* @param mocks An array of mock module names relative to the rootDirectory configuration.
|
||||
* @param plugins An array of Plugin objects.
|
||||
* @param skipDefaults Set true to skip loading of default mocks.
|
||||
*/
|
||||
(mocks: Array<string>, plugins?: Array<Plugin>, skipDefaults?: boolean): ProtractorHttpMock;
|
||||
|
||||
/**
|
||||
* Instantiate mock modules from files. This must be done before the browser connects.
|
||||
*
|
||||
* @param mocks An array of mock module names relative to the rootDirectory configuration.
|
||||
* @param plugins An array of NPM modules as strings.
|
||||
* @param skipDefaults Set true to skip loading of default mocks.
|
||||
*/
|
||||
(mocks: Array<string>, plugins?: Array<string>, skipDefaults?: boolean): ProtractorHttpMock;
|
||||
(mocks?: ReadonlyArray<requests.AllRequests | string>, plugins?: ReadonlyArray<Plugin1<any> | Plugin2<any, any> | string>, skipDefaults?: boolean): ProtractorHttpMock;
|
||||
|
||||
/**
|
||||
* Clean up.
|
||||
@@ -75,9 +49,31 @@ declare namespace mock {
|
||||
|
||||
/**
|
||||
* Path to protractor configuration file.
|
||||
* Default: protractor.conf
|
||||
* Default: protractor-conf.js
|
||||
*/
|
||||
protractorConfig?: string;
|
||||
|
||||
mocks?: {
|
||||
/**
|
||||
* Name of the folder where mocks will reside.
|
||||
* Default: 'mocks'
|
||||
*/
|
||||
dir?: string,
|
||||
|
||||
/**
|
||||
* Collection of default mocks to load for every test.
|
||||
* Default: []
|
||||
*/
|
||||
default?: ReadonlyArray<string>
|
||||
},
|
||||
|
||||
plugins?: {
|
||||
/**
|
||||
* Collection of default plugins to load for every test.
|
||||
* Default: []
|
||||
*/
|
||||
default?: ReadonlyArray<string>
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -87,7 +83,7 @@ declare namespace mock {
|
||||
*
|
||||
* @param mocks An array of mock modules to load into the application.
|
||||
*/
|
||||
add<T1, T2>(mocks: Array<requests.AllRequests<T1, T2>>): webdriver.promise.Promise<boolean>;
|
||||
add(mocks: ReadonlyArray<requests.AllRequests>): webdriver.promise.Promise<boolean>;
|
||||
|
||||
/**
|
||||
* Remove mocks during test execution.
|
||||
@@ -96,7 +92,7 @@ declare namespace mock {
|
||||
*
|
||||
* @param mocks An array of mock modules to remove from the application.
|
||||
*/
|
||||
remove<T1, T2>(mocks: Array<requests.AllRequests<T1, T2>>): webdriver.promise.Promise<boolean>;
|
||||
remove(mocks: ReadonlyArray<requests.AllRequests>): webdriver.promise.Promise<boolean>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,9 +104,9 @@ declare namespace mock {
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugin for custom matching logic.
|
||||
* Plugin for custom matching logic with 1 generic type.
|
||||
*/
|
||||
interface Plugin {
|
||||
interface Plugin1<T1> {
|
||||
/**
|
||||
* Match function.
|
||||
* Return a truthy value to indicate successfull match.
|
||||
@@ -118,13 +114,19 @@ declare namespace mock {
|
||||
* @param mockRequest The mock to compare request with.
|
||||
* @param requestConfig The request object to compare mock with.
|
||||
*/
|
||||
match<T1, T2>(mockRequest: requests.AllRequests<T1, T2>, requestConfig: requests.AllRequests<T1, T2>): boolean;
|
||||
match<O extends requests.Get<T1>>(mockRequest: O, requestConfig: O): boolean;
|
||||
match<O extends requests.Post<T1>>(mockRequest: O, requestConfig: O): boolean;
|
||||
match<O extends requests.Head<T1>>(mockRequest: O, requestConfig: O): boolean;
|
||||
match<O extends requests.Delete<T1>>(mockRequest: O, requestConfig: O): boolean;
|
||||
match<O extends requests.Put<T1>>(mockRequest: O, requestConfig: O): boolean;
|
||||
match<O extends requests.Patch<T1>>(mockRequest: O, requestConfig: O): boolean;
|
||||
match<O extends requests.Jsonp<T1>>(mockRequest: O, requestConfig: O): boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugin for custom matching logic.
|
||||
* Plugin for custom matching logic with 2 generic types.
|
||||
*/
|
||||
interface Plugin {
|
||||
interface Plugin2<T1, T2> {
|
||||
/**
|
||||
* Match function.
|
||||
* Return a truthy value to indicate successfull match.
|
||||
@@ -132,7 +134,7 @@ declare namespace mock {
|
||||
* @param mockRequest The mock to compare request with.
|
||||
* @param requestConfig The request object to compare mock with.
|
||||
*/
|
||||
match<T1, T2>(mockRequest: requests.AllRequests<T1, T2>, requestConfig: requests.AllRequests<T1, T2>): boolean;
|
||||
match<O extends requests.PostData<T1, T2>>(mockRequest: O, requestConfig: O): boolean;
|
||||
}
|
||||
|
||||
namespace requests {
|
||||
@@ -144,31 +146,30 @@ declare namespace mock {
|
||||
/**
|
||||
* All available request types.
|
||||
*/
|
||||
type AllRequests<T1, T2> = Get<T1> |
|
||||
PostData<T1, T2> |
|
||||
Post<T1> |
|
||||
Head<T1> |
|
||||
Delete<T1> |
|
||||
Put<T1> |
|
||||
Patch<T1> |
|
||||
Jsonp<T1>;
|
||||
type AllRequests = Get<any> |
|
||||
PostData<any, any> |
|
||||
Post<any> |
|
||||
Head<any> |
|
||||
Delete<any> |
|
||||
Put<any> |
|
||||
Patch<any> |
|
||||
Jsonp<any>;
|
||||
|
||||
/**
|
||||
* GET request mock.
|
||||
*/
|
||||
interface Get<TResponse> {
|
||||
request: {
|
||||
method: Method;
|
||||
method: "GET";
|
||||
path: string;
|
||||
regex?: boolean;
|
||||
params?: Object;
|
||||
queryString?: Object;
|
||||
headers?: Object;
|
||||
interceptedRequest?: boolean;
|
||||
interceptedAnonymousRequest?: boolean;
|
||||
};
|
||||
response: {
|
||||
status?: number;
|
||||
delay?: number;
|
||||
data: TResponse;
|
||||
};
|
||||
}
|
||||
@@ -178,13 +179,14 @@ declare namespace mock {
|
||||
*/
|
||||
interface PostData<TResponse, TPayload> {
|
||||
request: {
|
||||
method: Method;
|
||||
method: "POST";
|
||||
path: string;
|
||||
regex?: boolean;
|
||||
data: TPayload;
|
||||
};
|
||||
response: {
|
||||
status?: number;
|
||||
delay?: number;
|
||||
data: TResponse;
|
||||
};
|
||||
}
|
||||
@@ -194,12 +196,13 @@ declare namespace mock {
|
||||
*/
|
||||
interface Post<TResponse> {
|
||||
request: {
|
||||
method: Method;
|
||||
method: "POST";
|
||||
path: string;
|
||||
regex?: boolean;
|
||||
};
|
||||
response: {
|
||||
status?: number;
|
||||
delay?: number;
|
||||
data: TResponse;
|
||||
};
|
||||
}
|
||||
@@ -209,12 +212,13 @@ declare namespace mock {
|
||||
*/
|
||||
interface Head<TResponse> {
|
||||
request: {
|
||||
method: Method;
|
||||
method: "HEAD";
|
||||
path: string;
|
||||
regex?: boolean;
|
||||
};
|
||||
response: {
|
||||
status?: number;
|
||||
delay?: number;
|
||||
data: TResponse;
|
||||
};
|
||||
}
|
||||
@@ -224,12 +228,13 @@ declare namespace mock {
|
||||
*/
|
||||
interface Delete<TResponse> {
|
||||
request: {
|
||||
method: Method;
|
||||
method: "DELETE";
|
||||
path: string;
|
||||
regex?: boolean;
|
||||
};
|
||||
response: {
|
||||
status?: number;
|
||||
delay?: number;
|
||||
data: TResponse;
|
||||
};
|
||||
}
|
||||
@@ -239,12 +244,13 @@ declare namespace mock {
|
||||
*/
|
||||
interface Put<TResponse> {
|
||||
request: {
|
||||
method: Method;
|
||||
method: "PUT";
|
||||
path: string;
|
||||
regex?: boolean;
|
||||
};
|
||||
response: {
|
||||
status?: number;
|
||||
delay?: number;
|
||||
data: TResponse;
|
||||
};
|
||||
}
|
||||
@@ -254,12 +260,13 @@ declare namespace mock {
|
||||
*/
|
||||
interface Patch<TResponse> {
|
||||
request: {
|
||||
method: Method;
|
||||
method: "PATCH";
|
||||
path: string;
|
||||
regex?: boolean;
|
||||
};
|
||||
response: {
|
||||
status?: number;
|
||||
delay?: number;
|
||||
data: TResponse;
|
||||
};
|
||||
}
|
||||
@@ -269,12 +276,13 @@ declare namespace mock {
|
||||
*/
|
||||
interface Jsonp<TResponse> {
|
||||
request: {
|
||||
method: Method;
|
||||
method: "JSONP";
|
||||
path: string;
|
||||
regex?: boolean;
|
||||
};
|
||||
response: {
|
||||
status?: number;
|
||||
delay?: number;
|
||||
data: TResponse;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,12 +3,19 @@ import mock = require("protractor-http-mock");
|
||||
function TestConfig() {
|
||||
mock.config = {
|
||||
rootDirectory: "root",
|
||||
protractorConfig: "protractor.conf.js"
|
||||
protractorConfig: "protractor.conf.js",
|
||||
mocks: {
|
||||
dir: "mocks",
|
||||
default: ["default-mock"]
|
||||
},
|
||||
plugins: {
|
||||
default: ["default-plugin"]
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function TestCtorOverloads() {
|
||||
let del: mock.requests.Delete<number> = {
|
||||
let delNumber: mock.requests.Delete<number> = {
|
||||
request: {
|
||||
path: 'path',
|
||||
method: 'DELETE'
|
||||
@@ -18,38 +25,142 @@ function TestCtorOverloads() {
|
||||
data: 1
|
||||
}
|
||||
};
|
||||
let put: mock.requests.Put<number> = {
|
||||
let putString: mock.requests.Put<string> = {
|
||||
request: {
|
||||
path: 'path',
|
||||
method: 'PUT'
|
||||
},
|
||||
response: {
|
||||
status: 400,
|
||||
data: 1
|
||||
data: 'data to put'
|
||||
}
|
||||
};
|
||||
let plugin: mock.Plugin = {
|
||||
match: (mockRequest: mock.requests.Delete<number>, requestConfig: mock.requests.AllRequests<number, number>) => {
|
||||
if (requestConfig.request.method && mockRequest.request.method) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
let pluginNumber: mock.Plugin1<number> = {
|
||||
match: (mockRequest: mock.requests.Delete<number>, requestConfig: mock.requests.Delete<number>) => true
|
||||
};
|
||||
let pluginBoolean: mock.Plugin1<boolean> = {
|
||||
match: (mockRequest: mock.requests.Post<boolean>, requestConfig: mock.requests.Post<boolean>) => true
|
||||
};
|
||||
|
||||
let noParam: mock.ProtractorHttpMock = mock();
|
||||
let emptyArray: mock.ProtractorHttpMock = mock([]);
|
||||
let mockFiles: mock.ProtractorHttpMock = mock(['mock1', 'mock2']);
|
||||
let mocks: mock.ProtractorHttpMock = mock([del, put]);
|
||||
let mocks: mock.ProtractorHttpMock = mock([delNumber, putString]);
|
||||
|
||||
let mockFilesNpmPlugins: mock.ProtractorHttpMock = mock(['mock1'], ['plugin']);
|
||||
let mocksWithNpmPlugins: mock.ProtractorHttpMock = mock([del, put], ['plugin']);
|
||||
let mocksWithNpmPlugins: mock.ProtractorHttpMock = mock([delNumber, putString], ['plugin']);
|
||||
|
||||
let pluginMocks: mock.ProtractorHttpMock = mock([del, put], [plugin]);
|
||||
let pluginMocks: mock.ProtractorHttpMock = mock([delNumber, putString], [pluginNumber, pluginBoolean]);
|
||||
|
||||
let mockFilesNpmPluginsSkipDefaults: mock.ProtractorHttpMock = mock(['mock1'], ['plugin'], true);
|
||||
let skipDefaults: mock.ProtractorHttpMock = mock([del, put], [plugin], true);
|
||||
let skipDefaults: mock.ProtractorHttpMock = mock([delNumber, putString], [pluginNumber, pluginBoolean], true);
|
||||
}
|
||||
|
||||
function TestCtorMockOverloads() {
|
||||
let get: mock.requests.Get<number> = {
|
||||
request: {
|
||||
path: 'path',
|
||||
method: 'GET'
|
||||
},
|
||||
response: {
|
||||
data: 1
|
||||
}
|
||||
};
|
||||
let postData: mock.requests.PostData<number, string> = {
|
||||
request: {
|
||||
path: 'path',
|
||||
method: 'POST',
|
||||
data: 'data'
|
||||
},
|
||||
response: {
|
||||
data: 1
|
||||
}
|
||||
};
|
||||
let post: mock.requests.Post<number> = {
|
||||
request: {
|
||||
path: 'path',
|
||||
method: 'POST'
|
||||
},
|
||||
response: {
|
||||
data: 1
|
||||
}
|
||||
};
|
||||
let head: mock.requests.Head<number> = {
|
||||
request: {
|
||||
path: 'path',
|
||||
method: 'HEAD'
|
||||
},
|
||||
response: {
|
||||
data: 1
|
||||
}
|
||||
};
|
||||
let del: mock.requests.Delete<number> = {
|
||||
request: {
|
||||
path: 'path',
|
||||
method: 'DELETE'
|
||||
},
|
||||
response: {
|
||||
data: 1
|
||||
}
|
||||
};
|
||||
let put: mock.requests.Put<number> = {
|
||||
request: {
|
||||
path: 'path',
|
||||
method: 'PUT'
|
||||
},
|
||||
response: {
|
||||
data: 1
|
||||
}
|
||||
};
|
||||
let patch: mock.requests.Patch<number> = {
|
||||
request: {
|
||||
path: 'path',
|
||||
method: 'PATCH'
|
||||
},
|
||||
response: {
|
||||
data: 1
|
||||
}
|
||||
};
|
||||
let jsonp: mock.requests.Jsonp<number> = {
|
||||
request: {
|
||||
path: 'path',
|
||||
method: 'JSONP'
|
||||
},
|
||||
response: {
|
||||
data: 1
|
||||
}
|
||||
};
|
||||
|
||||
mock(['file', get, postData, post, head, del, put, patch, jsonp]);
|
||||
}
|
||||
|
||||
function TestCtorPluginOverloads() {
|
||||
let get: mock.Plugin1<number> = {
|
||||
match: (mockRequest: mock.requests.Get<number>, requestConfig: mock.requests.Get<number>) => true
|
||||
};
|
||||
let postData: mock.Plugin2<number, string> = {
|
||||
match: (mockRequest: mock.requests.PostData<number, string>, requestConfig: mock.requests.PostData<number, string>) => true
|
||||
};
|
||||
let post: mock.Plugin1<number> = {
|
||||
match: (mockRequest: mock.requests.Post<number>, requestConfig: mock.requests.Post<number>) => true
|
||||
};
|
||||
let head: mock.Plugin1<number> = {
|
||||
match: (mockRequest: mock.requests.Head<number>, requestConfig: mock.requests.Head<number>) => true
|
||||
};
|
||||
let del: mock.Plugin1<number> = {
|
||||
match: (mockRequest: mock.requests.Delete<number>, requestConfig: mock.requests.Delete<number>) => true
|
||||
};
|
||||
let put: mock.Plugin1<number> = {
|
||||
match: (mockRequest: mock.requests.Put<number>, requestConfig: mock.requests.Put<number>) => true
|
||||
};
|
||||
let patch: mock.Plugin1<number> = {
|
||||
match: (mockRequest: mock.requests.Patch<number>, requestConfig: mock.requests.Patch<number>) => true
|
||||
};
|
||||
let jsonp: mock.Plugin1<number> = {
|
||||
match: (mockRequest: mock.requests.Jsonp<number>, requestConfig: mock.requests.Jsonp<number>) => true
|
||||
};
|
||||
|
||||
mock([], ['npm', get, postData, post, head, del, put, patch, jsonp]);
|
||||
}
|
||||
|
||||
function TestTeardown() {
|
||||
@@ -102,12 +213,10 @@ function TestGetRequestDefinitions() {
|
||||
let getMinium: mock.requests.Get<number> = {
|
||||
request: {
|
||||
path: 'path',
|
||||
method: 'GET',
|
||||
regex: true
|
||||
method: 'GET'
|
||||
},
|
||||
response: {
|
||||
data: 1,
|
||||
status: 500
|
||||
data: 1
|
||||
}
|
||||
};
|
||||
|
||||
@@ -170,6 +279,19 @@ function TestGetRequestDefinitions() {
|
||||
status: 500
|
||||
}
|
||||
};
|
||||
|
||||
let getDelay: mock.requests.Get<number> = {
|
||||
request: {
|
||||
path: 'path',
|
||||
method: 'GET',
|
||||
regex: true
|
||||
},
|
||||
response: {
|
||||
data: 1,
|
||||
delay: 20,
|
||||
status: 500
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function TestPostRequestDefinitions() {
|
||||
@@ -181,6 +303,7 @@ function TestPostRequestDefinitions() {
|
||||
},
|
||||
response: {
|
||||
data: 1,
|
||||
delay: 20,
|
||||
status: 500
|
||||
}
|
||||
};
|
||||
@@ -194,6 +317,7 @@ function TestPostRequestDefinitions() {
|
||||
},
|
||||
response: {
|
||||
data: 1,
|
||||
delay: 20,
|
||||
status: 500
|
||||
}
|
||||
};
|
||||
@@ -208,6 +332,7 @@ function TestHeadRequestDefinitions() {
|
||||
},
|
||||
response: {
|
||||
status: 500,
|
||||
delay: 20,
|
||||
data: 1
|
||||
}
|
||||
};
|
||||
@@ -222,6 +347,7 @@ function TestDeleteRequestDefinitions() {
|
||||
},
|
||||
response: {
|
||||
status: 500,
|
||||
delay: 20,
|
||||
data: 1
|
||||
}
|
||||
};
|
||||
@@ -236,6 +362,7 @@ function TestPutRequestDefinitions() {
|
||||
},
|
||||
response: {
|
||||
status: 500,
|
||||
delay: 20,
|
||||
data: 1
|
||||
}
|
||||
};
|
||||
@@ -250,6 +377,7 @@ function TestPatchRequestDefinitions() {
|
||||
},
|
||||
response: {
|
||||
status: 500,
|
||||
delay: 20,
|
||||
data: 1
|
||||
}
|
||||
};
|
||||
@@ -264,6 +392,7 @@ function TestJsonpRequestDefinitions() {
|
||||
},
|
||||
response: {
|
||||
status: 500,
|
||||
delay: 20,
|
||||
data: 1
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user