playwright-fluent
Version:
Fluent API around playwright
123 lines (122 loc) • 4.82 kB
TypeScript
import { FluentMock, QueryString, RequestInfos, ResponseData } from './with-mocks';
import { MissingMock } from './get-missing-mocks';
import { UpdatePolicy } from '../../../utils';
export interface UpdatePolicyOptions {
/**
* callback to update the data source of the mocked response.
* When provided, this method will be called automatically when
* 1°) the mock is found to be outdated by the helper {@link getOutdatedMocks})
* 2°) and the call to {@link lastUpdated} gives a date that is older than the {@link updatePolicy}
* @memberof FluentMock
*/
updateData: (requestInfos: RequestInfos, response: ResponseData) => void;
/**
* Optional callback to get the last update of the data source used to mock the response.
* This method will be called automatically when the mock is found to be outdated by the helper {@link getOutdatedMocks})
* Defaults to the current date.
*
* @type {Date}
* @memberof FluentMock
*/
lastUpdated?: () => Date;
/**
* Update policy for the data source of the mocked response.
* Defaults to 'always'.
*
* @type {UpdatePolicy}
* @memberof FluentMock
*/
updatePolicy?: UpdatePolicy;
}
/**
* Ceate a mock for a GET request to the specified url that returns a JSON response.
*
* @export
* @template T
* @param {string} relativeUrl
* @param {T} response - The JSON response to return.
* @param {UpdatePolicyOptions} [updatePolicy]
* @returns {Partial<FluentMock>}
* @example
* It will return this mock:
* {
* displayName: `GET ${relativeUrl}`,
* urlMatcher: (url) => url.includes(relativeUrl),
* methodMatcher: (m) => m === 'GET',
* jsonResponse: () => response,
* }
*/
export declare function mockGetWithJsonResponse<T>(relativeUrl: string, response: T, updatePolicy?: UpdatePolicyOptions): Partial<FluentMock>;
/**
* Ceate a mock for a GET request to the specified url that returns a JavaScript response.
*
* @export
* @param {string} relativeUrl
* @param {string} response - The JavaScript response to return.
* @param {UpdatePolicyOptions} [updatePolicy]
* @returns {Partial<FluentMock>}
* @example
* It will return this mock:
* {
* displayName: `GET ${relativeUrl}`,
* urlMatcher: (url) => url.includes(relativeUrl),
* methodMatcher: (m) => m === 'GET',
* responseType: 'javascript',
* rawResponse: () => response,
* }
*
*/
export declare function mockGetWithJavascriptResponse(relativeUrl: string, response: string, updatePolicy?: UpdatePolicyOptions): Partial<FluentMock>;
export declare function mockGetWithEmptyResponseAndStatus(relativeUrl: string, status?: number): Partial<FluentMock>;
/**
* Ceate a mock for a GET request to the specified url that returns an HTTP 401 status.
*
* @export
* @param {string} relativeUrl
* @returns {Partial<FluentMock>}
* @example
* It will return this mock:
* {
* displayName: `GET ${relativeUrl}`,
* urlMatcher: (url) => url.includes(relativeUrl),
* methodMatcher: (m) => m === 'GET',
* responseType: 'empty',
* status: 401,
* }
*/
export declare function mockGetWithUnauthorizedResponse(relativeUrl: string): Partial<FluentMock>;
/**
* Ceate a mock for a GET request to the specified url that returns an HTTP 403 status.
*
* @export
* @param {string} relativeUrl
* @returns {Partial<FluentMock>}
* @example
* It will return this mock:
* {
* displayName: `GET ${relativeUrl}`,
* urlMatcher: (url) => url.includes(relativeUrl),
* methodMatcher: (m) => m === 'GET',
* responseType: 'empty',
* status: 403,
* }
*/
export declare function mockGetWithForbiddenResponse(relativeUrl: string): Partial<FluentMock>;
export declare function mockPostWithEmptyResponseAndStatus(relativeUrl: string, status?: number): Partial<FluentMock>;
/**
* Ceate a mock for a GET request to the specified url that returns a JSON response depending on the query string
*
* @export
* @template T
* @param {string} relativeUrl - The url to mock
* @param {QueryString} queryString - The query string to match against the url.
* @param {T} response - The JSON response to return.
* @param {UpdatePolicyOptions} [updatePolicy]
* @returns {Partial<FluentMock>}
* @example
* const mock = mockGetWithJsonResponseDependingOnQueryString('/api/v1/yo', { foo: 'bar' }, response);
* // will mock any request to /api/v1/yo whose query string contains ?foo=bar
*/
export declare function mockGetWithJsonResponseDependingOnQueryString<T>(relativeUrl: string, queryString: QueryString, response: T, updatePolicy?: UpdatePolicyOptions): Partial<FluentMock>;
export declare function generateCodeForMissingMock(missingMock: MissingMock, targetDirectory: string): void;
export declare function generateMockCodeOf(missingMock: MissingMock, dataFileName: string): string;