Skip to content

Commit

Permalink
feat: Add PATCH method QA-97
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszfiszer committed Feb 13, 2020
1 parent a429531 commit 031f11a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 51 deletions.
41 changes: 24 additions & 17 deletions src/mocketeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
MockOptions,
RequestMatcher,
RequestMatcherShort,
REST_METHOD,
} from './types';
import {
addMockByPriority,
Expand Down Expand Up @@ -64,43 +63,51 @@ export class Mocketeer {
response: MockedResponse,
options?: Partial<MockOptions>
): Mock {
const filterObject = createRequestFilter(filter, {
method: REST_METHOD.GET,
});
return this.mock(filterObject, response, options);
return this.mock(createRequestFilter(filter, 'GET'), response, options);
}

public mockPOST(
filter: RequestMatcherShort,
response: MockedResponse,
options?: Partial<MockOptions>
): Mock {
const filterObject = createRequestFilter(filter, {
method: REST_METHOD.POST,
});
return this.mock(filterObject, response, options);
return this.mock(
createRequestFilter(filter, 'POST'),
response,
options
);
}

public mockPUT(
filter: RequestMatcherShort,
response: MockedResponse,
options?: Partial<MockOptions>
): Mock {
const filterObject = createRequestFilter(filter, {
method: REST_METHOD.PUT,
});
return this.mock(filterObject, response, options);
return this.mock(createRequestFilter(filter, 'PUT'), response, options);
}

public mockDELETE(
filter: RequestMatcherShort,
response: MockedResponse,
options?: Partial<MockOptions>
): Mock {
const filterObject = createRequestFilter(filter, {
method: REST_METHOD.DELETE,
});
return this.mock(filterObject, response, options);
return this.mock(
createRequestFilter(filter, 'DELETE'),
response,
options
);
}

public mockPATCH(
filter: RequestMatcherShort,
response: MockedResponse,
options?: Partial<MockOptions>
): Mock {
return this.mock(
createRequestFilter(filter, 'PATCH'),
response,
options
);
}

public removeMock(mock: Mock): Mock | void {
Expand Down
7 changes: 1 addition & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,4 @@ export interface MockOptions {
once: boolean;
}

export enum REST_METHOD {
GET = 'GET',
POST = 'POST',
PUT = 'PUT',
DELETE = 'DELETE',
}
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
13 changes: 9 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { parse } from 'url';
import { Request, Headers } from 'puppeteer';
import { ReceivedRequest, RequestMatcher, RequestMatcherObject } from './types';
import {
HttpMethod,
ReceivedRequest,
RequestMatcher,
RequestMatcherObject,
} from './types';

export function requestToPlainObject(request: Request): ReceivedRequest {
const url = request.url();
Expand Down Expand Up @@ -84,16 +89,16 @@ export function addMockByPriority<T extends { options: { priority: number } }>(

export function createRequestFilter(
input: RequestMatcher,
defaults: Omit<RequestMatcherObject, 'url'> = {}
method?: HttpMethod
): RequestMatcherObject {
if (typeof input === 'string') {
return {
...defaults,
method,
url: input,
};
} else {
return {
...defaults,
method,
...input,
};
}
Expand Down
13 changes: 10 additions & 3 deletions test/integration/mocketeer.int.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { Mocketeer } from '../../src';
import { Mocketeer, HttpMethod } from '../../src';
import { Browser, launch, Page } from 'puppeteer';
import { makeRequestFactory } from './test-helpers/make-request-factory';

const PORT = 9000;

const METHODS = ['GET', 'POST', 'PUT', 'DELETE'];
const METHODS: HttpMethod[] = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'];

type MocketeerHttpMethods =
| 'mockGET'
| 'mockPUT'
| 'mockPOST'
| 'mockDELETE'
| 'mockPATCH';

describe('Mocketeer integration', () => {
let browser: Browser;
Expand Down Expand Up @@ -75,7 +82,7 @@ describe('Mocketeer integration', () => {
test.each(METHODS)(
'matches request with .mock%s() method ',
async METHOD => {
mocketeer['mock' + METHOD]('/example', {
mocketeer[('mock' + METHOD) as MocketeerHttpMethods]('/example', {
status: 200,
body: METHOD,
});
Expand Down
38 changes: 25 additions & 13 deletions test/unit/mocketeer.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Mocketeer, RequestMatcherShort, REST_METHOD, Mock } from '../../src';
import { Mocketeer, RequestMatcherShort, Mock } from '../../src';
import { createMockPage } from './fixtures/page';
import { Request } from './fixtures/request';
jest.mock('../../src/mock');
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('Mocketeer', () => {
test('should create RestMock using GET method and filter as object', () => {
mocketeer.mockGET(filter, mockResponse);
expect(Mock).toHaveBeenCalledWith(
expect.objectContaining({ method: REST_METHOD.GET, ...filter }),
expect.objectContaining({ method: 'GET', ...filter }),
mockResponse,
expect.anything()
);
Expand All @@ -80,7 +80,7 @@ describe('Mocketeer', () => {
mocketeer.mockGET(filterWithQuery, mockResponse);
expect(Mock).toHaveBeenCalledWith(
expect.objectContaining({
method: REST_METHOD.GET,
method: 'GET',
...filterWithQuery,
}),
mockResponse,
Expand All @@ -91,7 +91,7 @@ describe('Mocketeer', () => {
test('should create RestMock using GET method and filter as url string', () => {
mocketeer.mockGET(url, mockResponse);
expect(Mock).toHaveBeenCalledWith(
expect.objectContaining({ method: REST_METHOD.GET, ...filter }),
expect.objectContaining({ method: 'GET', ...filter }),
mockResponse,
expect.anything()
);
Expand All @@ -101,7 +101,7 @@ describe('Mocketeer', () => {
mocketeer.mockPOST(filter, mockResponse);
expect(Mock).toHaveBeenCalledWith(
expect.objectContaining({
method: REST_METHOD.POST,
method: 'POST',
...filter,
}),
mockResponse,
Expand All @@ -113,7 +113,7 @@ describe('Mocketeer', () => {
mocketeer.mockPOST(filterWithQuery, mockResponse);
expect(Mock).toHaveBeenCalledWith(
expect.objectContaining({
method: REST_METHOD.POST,
method: 'POST',
...filterWithQuery,
}),
mockResponse,
Expand All @@ -125,7 +125,7 @@ describe('Mocketeer', () => {
mocketeer.mockPOST(url, mockResponse);
expect(Mock).toHaveBeenCalledWith(
expect.objectContaining({
method: REST_METHOD.POST,
method: 'POST',
...filter,
}),
mockResponse,
Expand All @@ -136,7 +136,7 @@ describe('Mocketeer', () => {
test('should create RestMock using PUT method and filter as object', () => {
mocketeer.mockPUT(filter, mockResponse);
expect(Mock).toHaveBeenCalledWith(
expect.objectContaining({ method: REST_METHOD.PUT, ...filter }),
expect.objectContaining({ method: 'PUT', ...filter }),
mockResponse,
expect.anything()
);
Expand All @@ -146,7 +146,7 @@ describe('Mocketeer', () => {
mocketeer.mockPUT(filterWithQuery, mockResponse);
expect(Mock).toHaveBeenCalledWith(
expect.objectContaining({
method: REST_METHOD.PUT,
method: 'PUT',
...filterWithQuery,
}),
mockResponse,
Expand All @@ -157,7 +157,7 @@ describe('Mocketeer', () => {
test('should create RestMock using PUT method and filter as url string', () => {
mocketeer.mockPUT(url, mockResponse);
expect(Mock).toHaveBeenCalledWith(
expect.objectContaining({ method: REST_METHOD.PUT, ...filter }),
expect.objectContaining({ method: 'PUT', ...filter }),
mockResponse,
expect.anything()
);
Expand All @@ -167,7 +167,7 @@ describe('Mocketeer', () => {
mocketeer.mockDELETE(filter, mockResponse);
expect(Mock).toHaveBeenCalledWith(
expect.objectContaining({
method: REST_METHOD.DELETE,
method: 'DELETE',
...filter,
}),
mockResponse,
Expand All @@ -179,7 +179,7 @@ describe('Mocketeer', () => {
mocketeer.mockDELETE(filterWithQuery, mockResponse);
expect(Mock).toHaveBeenCalledWith(
expect.objectContaining({
method: REST_METHOD.DELETE,
method: 'DELETE',
...filterWithQuery,
}),
mockResponse,
Expand All @@ -191,7 +191,19 @@ describe('Mocketeer', () => {
mocketeer.mockDELETE(url, mockResponse);
expect(Mock).toHaveBeenCalledWith(
expect.objectContaining({
method: REST_METHOD.DELETE,
method: 'DELETE',
...filter,
}),
mockResponse,
expect.anything()
);
});

test('should create RestMock using PATCH method and filter as url string', () => {
mocketeer.mockPATCH(url, mockResponse);
expect(Mock).toHaveBeenCalledWith(
expect.objectContaining({
method: 'PATCH',
...filter,
}),
mockResponse,
Expand Down
10 changes: 2 additions & 8 deletions test/unit/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,7 @@ describe('createRequestMatcher', () => {
});

test('return expected object when provided defaults', () => {
expect(
createRequestFilter('http://example.com', {
method: 'POST',
})
).toEqual({
expect(createRequestFilter('http://example.com', 'POST')).toEqual({
method: 'POST',
url: 'http://example.com',
});
Expand All @@ -164,9 +160,7 @@ describe('createRequestMatcher', () => {
url: 'http://foo.com',
query: { param: 'value' },
},
{
method: 'POST',
}
'POST'
)
).toEqual({
method: 'POST',
Expand Down

0 comments on commit 031f11a

Please sign in to comment.