From 756751f4168a22ffd6623ea7f52cfbfb0307de6f Mon Sep 17 00:00:00 2001 From: Adam Kwiatek Date: Thu, 7 Nov 2019 18:28:10 +0100 Subject: [PATCH] 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. --- types/protractor-http-mock/index.d.ts | 124 +++++++------ .../protractor-http-mock-tests.ts | 169 +++++++++++++++--- 2 files changed, 215 insertions(+), 78 deletions(-) diff --git a/types/protractor-http-mock/index.d.ts b/types/protractor-http-mock/index.d.ts index 7dfdd9057e..745e038825 100644 --- a/types/protractor-http-mock/index.d.ts +++ b/types/protractor-http-mock/index.d.ts @@ -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 +// Adam Kwiatek // 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. */ - (mocks?: Array>, plugins?: Array, 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. - */ - (mocks?: Array>, plugins?: Array, 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, plugins?: Array, 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, plugins?: Array, skipDefaults?: boolean): ProtractorHttpMock; + (mocks?: ReadonlyArray, plugins?: ReadonlyArray | Plugin2 | 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 + }, + + plugins?: { + /** + * Collection of default plugins to load for every test. + * Default: [] + */ + default?: ReadonlyArray + } }; /** @@ -87,7 +83,7 @@ declare namespace mock { * * @param mocks An array of mock modules to load into the application. */ - add(mocks: Array>): webdriver.promise.Promise; + add(mocks: ReadonlyArray): webdriver.promise.Promise; /** * 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(mocks: Array>): webdriver.promise.Promise; + remove(mocks: ReadonlyArray): webdriver.promise.Promise; } /** @@ -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 { /** * 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(mockRequest: requests.AllRequests, requestConfig: requests.AllRequests): boolean; + match>(mockRequest: O, requestConfig: O): boolean; + match>(mockRequest: O, requestConfig: O): boolean; + match>(mockRequest: O, requestConfig: O): boolean; + match>(mockRequest: O, requestConfig: O): boolean; + match>(mockRequest: O, requestConfig: O): boolean; + match>(mockRequest: O, requestConfig: O): boolean; + match>(mockRequest: O, requestConfig: O): boolean; } /** - * Plugin for custom matching logic. + * Plugin for custom matching logic with 2 generic types. */ - interface Plugin { + interface Plugin2 { /** * 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(mockRequest: requests.AllRequests, requestConfig: requests.AllRequests): boolean; + match>(mockRequest: O, requestConfig: O): boolean; } namespace requests { @@ -144,31 +146,30 @@ declare namespace mock { /** * All available request types. */ - type AllRequests = Get | - PostData | - Post | - Head | - Delete | - Put | - Patch | - Jsonp; + type AllRequests = Get | + PostData | + Post | + Head | + Delete | + Put | + Patch | + Jsonp; /** * GET request mock. */ interface Get { 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 { 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 { 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 { 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 { 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 { 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 { 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 { request: { - method: Method; + method: "JSONP"; path: string; regex?: boolean; }; response: { status?: number; + delay?: number; data: TResponse; }; } diff --git a/types/protractor-http-mock/protractor-http-mock-tests.ts b/types/protractor-http-mock/protractor-http-mock-tests.ts index 08b97fd09f..3b7fd83782 100644 --- a/types/protractor-http-mock/protractor-http-mock-tests.ts +++ b/types/protractor-http-mock/protractor-http-mock-tests.ts @@ -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 = { + let delNumber: mock.requests.Delete = { request: { path: 'path', method: 'DELETE' @@ -18,38 +25,142 @@ function TestCtorOverloads() { data: 1 } }; - let put: mock.requests.Put = { + let putString: mock.requests.Put = { request: { path: 'path', method: 'PUT' }, response: { status: 400, - data: 1 + data: 'data to put' } }; - let plugin: mock.Plugin = { - match: (mockRequest: mock.requests.Delete, requestConfig: mock.requests.AllRequests) => { - if (requestConfig.request.method && mockRequest.request.method) { - return true; - } - - return false; - } + let pluginNumber: mock.Plugin1 = { + match: (mockRequest: mock.requests.Delete, requestConfig: mock.requests.Delete) => true + }; + let pluginBoolean: mock.Plugin1 = { + match: (mockRequest: mock.requests.Post, requestConfig: mock.requests.Post) => 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 = { + request: { + path: 'path', + method: 'GET' + }, + response: { + data: 1 + } + }; + let postData: mock.requests.PostData = { + request: { + path: 'path', + method: 'POST', + data: 'data' + }, + response: { + data: 1 + } + }; + let post: mock.requests.Post = { + request: { + path: 'path', + method: 'POST' + }, + response: { + data: 1 + } + }; + let head: mock.requests.Head = { + request: { + path: 'path', + method: 'HEAD' + }, + response: { + data: 1 + } + }; + let del: mock.requests.Delete = { + request: { + path: 'path', + method: 'DELETE' + }, + response: { + data: 1 + } + }; + let put: mock.requests.Put = { + request: { + path: 'path', + method: 'PUT' + }, + response: { + data: 1 + } + }; + let patch: mock.requests.Patch = { + request: { + path: 'path', + method: 'PATCH' + }, + response: { + data: 1 + } + }; + let jsonp: mock.requests.Jsonp = { + request: { + path: 'path', + method: 'JSONP' + }, + response: { + data: 1 + } + }; + + mock(['file', get, postData, post, head, del, put, patch, jsonp]); +} + +function TestCtorPluginOverloads() { + let get: mock.Plugin1 = { + match: (mockRequest: mock.requests.Get, requestConfig: mock.requests.Get) => true + }; + let postData: mock.Plugin2 = { + match: (mockRequest: mock.requests.PostData, requestConfig: mock.requests.PostData) => true + }; + let post: mock.Plugin1 = { + match: (mockRequest: mock.requests.Post, requestConfig: mock.requests.Post) => true + }; + let head: mock.Plugin1 = { + match: (mockRequest: mock.requests.Head, requestConfig: mock.requests.Head) => true + }; + let del: mock.Plugin1 = { + match: (mockRequest: mock.requests.Delete, requestConfig: mock.requests.Delete) => true + }; + let put: mock.Plugin1 = { + match: (mockRequest: mock.requests.Put, requestConfig: mock.requests.Put) => true + }; + let patch: mock.Plugin1 = { + match: (mockRequest: mock.requests.Patch, requestConfig: mock.requests.Patch) => true + }; + let jsonp: mock.Plugin1 = { + match: (mockRequest: mock.requests.Jsonp, requestConfig: mock.requests.Jsonp) => true + }; + + mock([], ['npm', get, postData, post, head, del, put, patch, jsonp]); } function TestTeardown() { @@ -102,12 +213,10 @@ function TestGetRequestDefinitions() { let getMinium: mock.requests.Get = { 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 = { + 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 } };