-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Mocketeer): QA-20 allow to intercept requests of any type
BREAKING CHANGE: remove deprecated API methods BREAKING CHANGE: rename types BREAKING CHANGE: can mock any requests, not just FETCH/XHR calls
- Loading branch information
1 parent
ad67834
commit 4d0a644
Showing
17 changed files
with
431 additions
and
345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,28 +22,21 @@ test('can create client', async () => { | |
const mocketeer = Mocketeer.setup(page); | ||
|
||
// Set up a mock | ||
const mock = await mocketeer.mockREST( | ||
{ | ||
method: 'POST', | ||
path: '/api/user', | ||
const mock = await mocketeer.mockPost('/api/user', { | ||
status: 201, | ||
body: { | ||
userId: '123', | ||
}, | ||
{ | ||
status: 201, | ||
body: { | ||
userId: '123', | ||
}, | ||
} | ||
); | ||
}); | ||
|
||
// Do something on the page | ||
await page.type('.email', '[email protected]'); | ||
await page.click('.submit-button'); | ||
|
||
// Verify request content | ||
await expect(mock.getRequest()).resolves.toMatchObject({ | ||
body: { | ||
user_email: '[email protected]', | ||
}, | ||
// Wait for mock to be called and verify request content | ||
const request = await mock.getRequest(); | ||
expect(request.body).toEqual({ | ||
user_email: '[email protected]', | ||
}); | ||
}); | ||
``` | ||
|
@@ -64,27 +57,22 @@ import { Mocketeer } from '@hltech/mocketeer'; | |
const mocketeer = Mocketeer.setup(page); | ||
|
||
// Set up a mock | ||
const mock = await mocketeer.mockREST( | ||
{ | ||
method: 'POST', | ||
path: '/api/user', | ||
const mock = await mocketeer.mockPost('/api/user', { | ||
status: 201, | ||
body: { | ||
userId: '123', | ||
}, | ||
{ | ||
status: 201, | ||
body: { | ||
error: false, | ||
}, | ||
} | ||
); | ||
}); | ||
|
||
// Do something on the page | ||
await page.type('.email', '[email protected]'); | ||
await page.click('.submit-button'); | ||
|
||
// Wait for mock to be called and verify request content | ||
const req = await mock.getRequest(); | ||
console.log(req.method); // POST | ||
console.log(req.body); // { user_email: "[email protected]" } | ||
const request = await mock.getRequest(); | ||
expect(request.body).toEqual({ | ||
user_email: '[email protected]', | ||
}); | ||
})(); | ||
``` | ||
|
||
|
@@ -96,7 +84,7 @@ import { Mocketeer } from '@hltech/mocketeer'; | |
|
||
Factory method used to set-up request mocking on provided Puppeteer Page. It creates and returns an instance of Mocketeer | ||
|
||
Mocketeer will intercept all requests made by the page and match them to mocks set up with `mocketeer.addRestMock`. | ||
Mocketeer will intercept all requests made by the page and match them to mocks set up with `mocketeer.mock`. | ||
If request does not match any mocks, it will be responded with `404 Not Found`. | ||
|
||
###### Arguments | ||
|
@@ -113,10 +101,6 @@ const page = await browser.newPage(); | |
const mocketeer = await Mocketeer.setup(page); | ||
``` | ||
|
||
#### ~~.activate(page: Page): Promise\<void\>~~ (depracated) | ||
|
||
Activate mocketeer on a given page. | ||
|
||
###### Arguments | ||
|
||
- `page` _(Page)_ instance of Puppeteer [Page](https://pptr.dev/#?product=Puppeteer&show=api-class-page) | ||
|
@@ -133,7 +117,7 @@ const page = await browser.newPage(); | |
await mocketeer.activate(page); | ||
``` | ||
|
||
#### .mockREST(filter: RequestFilter, response: MockedResponse, options?): RestMock | ||
#### .mock(filter: RequestFilter, response: MockedResponse, options?): RestMock | ||
|
||
Respond to xhr and fetch requests that match the `filter` with provided `response`. | ||
Request are matched based on adding order - most recently added first. | ||
|
@@ -161,7 +145,7 @@ Newly created instance of `RestMock`. | |
###### Example | ||
|
||
```typescript | ||
mocketeer.mockREST( | ||
mocketeer.mock( | ||
{ | ||
method: 'POST', | ||
url: '/api/clients', | ||
|
@@ -178,7 +162,7 @@ mocketeer.mockREST( | |
###### Example with query passed as an argument | ||
|
||
```typescript | ||
mocketeer.mockREST( | ||
mocketeer.mock( | ||
{ | ||
method: 'GET', | ||
url: '/api/clients', | ||
|
@@ -199,7 +183,7 @@ mocketeer.mockREST( | |
###### Example with queryString appended to the url | ||
|
||
```typescript | ||
mocketeer.mockREST( | ||
mocketeer.mock( | ||
{ | ||
method: 'GET', | ||
url: '/api/clients?city=Bristol&limit=10', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from './mocketeer'; | ||
export * from './rest-mock'; | ||
export * from './mock'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.