Skip to content

Commit

Permalink
Merge pull request #16 from wheresrhys/methodShorthands
Browse files Browse the repository at this point in the history
add methods scoped to http methods
  • Loading branch information
wheresrhys authored May 5, 2020
2 parents 5f3e846 + 9441b39 commit 16c55e0
Show file tree
Hide file tree
Showing 11 changed files with 1,561 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
"node": true
},
"parserOptions": {
"ecmaVersion": 2017
"ecmaVersion": 2018
},
"globals": {
"expect": true
Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,21 @@ The following custom jest expectation methods, proxying through to `fetch-mock`'
- `expect(fetchMock).toHaveFetchedTimes(n, filter, options)`
- `expect(fetchMock).toBeDone(filter)`

`filter` and `options` are the same as those used by [`fetch-mock`'s inspection methods](http://www.wheresrhys.co.uk/fetch-mock/#api-inspectionfundamentals)
### Notes

- `filter` and `options` are the same as those used by [`fetch-mock`'s inspection methods](http://www.wheresrhys.co.uk/fetch-mock/#api-inspectionfundamentals)
- The obove methods can have `Fetched` replaced by any of the following verbs to scope to a particular method: + Got + Posted + Put + Deleted + FetchedHead + Patched

e.g. `expect(fetchMock).toHaveLastPatched(filter, options)`

## Tearing down mocks

`fetchMock.mockClear()` can be used to reset the call history

`fetchMock.mockReset()` can be used to remove all configured mocks

Please report any bugs in resetting mocks on the [issues board](https://github.com/wheresrhys/fetch-mock-jest/issues)

# Example

```js
Expand All @@ -81,7 +88,8 @@ test(async () => {
);

expect(await userManager.getAll()).toEqual([{ name: 'bob' }]);
expect(fetchMock).toHaveLastFetched('http://example.com/users', 'get');
expect(fetchMock).toHaveLastFetched('http://example.com/users
get');
await userManager.create({ name: true });
expect(fetchMock).toHaveLastFetched(
{
Expand Down
252 changes: 252 additions & 0 deletions __tests__/jest-extensions-delete.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
/*global jest, beforeAll, afterAll */
jest.mock('node-fetch', () => require('../server').sandbox());
const fetch = require('node-fetch');
describe('jest extensions - delete', () => {
describe('when no calls', () => {
beforeAll(() => {
fetch.mock('*', 200);
fetch('http://lala');
});
afterAll(() => fetch.reset());
it('toHaveDeleted should be falsy', () => {
expect(fetch).not.toHaveDeleted('http://example.com/path');
});

it('toHaveLastDeleted should be falsy', () => {
expect(fetch).not.toHaveLastDeleted('http://example.com/path');
});

it('toHaveNthDeleted should be falsy', () => {
expect(fetch).not.toHaveNthDeleted(1, 'http://example.com/path');
});

it('toHaveDeletedTimes should be falsy', () => {
expect(fetch).not.toHaveDeletedTimes(1, 'http://example.com/path');
});

it('toBeDone should be falsy', () => {
expect(fetch).not.toBeDone();
expect(fetch).not.toBeDone('http://example.com/path');
});
});
describe('toHaveDeleted', () => {
beforeAll(() => {
fetch.mock('*', 200);
fetch('http://example.com/path2', {
method: 'delete',
headers: {
test: 'header',
},
});
fetch('http://example.com/path', {
method: 'delete',
headers: {
test: 'header',
},
});
});
afterAll(() => fetch.reset());

it('matches with just url', () => {
expect(fetch).toHaveDeleted('http://example.com/path');
});

it('matches with fetch-mock matcher', () => {
expect(fetch).toHaveDeleted('begin:http://example.com/path');
});

it('matches with matcher and options', () => {
expect(fetch).toHaveDeleted('http://example.com/path', {
method: 'delete',
headers: {
test: 'header',
},
});
});

it("doesn't match if matcher but not options is correct", () => {
expect(fetch).not.toHaveDeleted('http://example.com/path', {
method: 'delete',
headers: {
test: 'not-header',
},
});
});

it("doesn't match if options but not matcher is correct", () => {
expect(fetch).not.toHaveDeleted('http://example-no.com/path', {
method: 'delete',
headers: {
test: 'header',
},
});
});
});
describe('toHaveLastDeleted', () => {
beforeAll(() => {
fetch.mock('*', 200);
fetch('http://example.com/path', {
method: 'delete',
headers: {
test: 'header',
},
});
});
afterAll(() => fetch.reset());

it('matches with just url', () => {
expect(fetch).toHaveLastDeleted('http://example.com/path');
});

it('matches with fetch-mock matcher', () => {
expect(fetch).toHaveLastDeleted('begin:http://example.com/path');
});

it('matches with matcher and options', () => {
expect(fetch).toHaveLastDeleted('http://example.com/path', {
method: 'delete',
headers: {
test: 'header',
},
});
});

it("doesn't match if matcher but not options is correct", () => {
expect(fetch).not.toHaveLastDeleted('http://example.com/path', {
method: 'delete',
headers: {
test: 'not-header',
},
});
});

it("doesn't match if options but not matcher is correct", () => {
expect(fetch).not.toHaveLastDeleted('http://example-no.com/path', {
method: 'delete',
headers: {
test: 'header',
},
});
});
});

describe('toHaveNthDeleted', () => {
beforeAll(() => {
fetch.mock('*', 200);
fetch('http://example1.com/path', {
method: 'delete',
headers: {
test: 'header',
},
});
fetch('http://example2.com/path', {
method: 'delete',
headers: {
test: 'header',
},
});
});
afterAll(() => fetch.reset());

it('matches with just url', () => {
expect(fetch).toHaveNthDeleted(2, 'http://example2.com/path');
});

it('matches with fetch-mock matcher', () => {
expect(fetch).toHaveNthDeleted(2, 'begin:http://example2.com/path');
});

it('matches with matcher and options', () => {
expect(fetch).toHaveNthDeleted(2, 'http://example2.com/path', {
method: 'delete',
headers: {
test: 'header',
},
});
});

it("doesn't match if matcher but not options is correct", () => {
expect(fetch).not.toHaveNthDeleted(2, 'http://example2.com/path', {
method: 'delete',
headers: {
test: 'not-header',
},
});
});

it("doesn't match if options but not matcher is correct", () => {
expect(fetch).not.toHaveNthDeleted(2, 'http://example-no.com/path', {
method: 'delete',
headers: {
test: 'header',
},
});
});

it("doesn't match if wrong n", () => {
expect(fetch).not.toHaveNthDeleted(1, 'http://example2.com/path');
});
});

describe('toHaveDeletedTimes', () => {
beforeAll(() => {
fetch.mock('*', 200);
fetch('http://example.com/path', {
method: 'delete',
headers: {
test: 'header',
},
});
fetch('http://example.com/path', {
method: 'delete',
headers: {
test: 'header',
},
});
});
afterAll(() => fetch.reset());

it('matches with just url', () => {
expect(fetch).toHaveDeletedTimes(2, 'http://example.com/path');
});

it('matches with fetch-mock matcher', () => {
expect(fetch).toHaveDeletedTimes(2, 'begin:http://example.com/path');
});

it('matches with matcher and options', () => {
expect(fetch).toHaveDeletedTimes(2, 'http://example.com/path', {
method: 'delete',
headers: {
test: 'header',
},
});
});

it("doesn't match if matcher but not options is correct", () => {
expect(fetch).not.toHaveDeletedTimes(2, 'http://example.com/path', {
method: 'delete',
headers: {
test: 'not-header',
},
});
});

it("doesn't match if options but not matcher is correct", () => {
expect(fetch).not.toHaveDeletedTimes(2, 'http://example-no.com/path', {
method: 'delete',
headers: {
test: 'header',
},
});
});

it("doesn't match if too few calls", () => {
expect(fetch).not.toHaveDeletedTimes(1, 'http://example.com/path');
});

it("doesn't match if too many calls", () => {
expect(fetch).not.toHaveDeletedTimes(3, 'http://example.com/path');
});
});
});
Loading

0 comments on commit 16c55e0

Please sign in to comment.