Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from wheresrhys/fix-jest-methods
Browse files Browse the repository at this point in the history
fixes bugs in faking that fetch-mock is a normal jest function mock
  • Loading branch information
wheresrhys authored Apr 4, 2020
2 parents 592919f + 83fbc59 commit f92b61c
Show file tree
Hide file tree
Showing 6 changed files with 2,810 additions and 2,137 deletions.
78 changes: 43 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,39 +63,47 @@ const fetchMock = require('fetch-mock-jest');
const userManager = require('../src/user-manager');

test(async () => {
const users = [{name: 'bob'}];
fetchMock
.get('http://example.com/users', users)
.post('http://example.com/user', (url, options) => {
if (typeof options.body.name === 'string') {
users.push(options.body)
return 202
}
return 400
})
.patch({
url: 'http://example.com/user'
}, 405)

expect(await userManager.getAll()).toEqual([{name: 'bob'}])
expect(fetchMock).toHaveLastFetched('http://example.com/users', 'get')
await userManager.create({name: true})
expect(fetchMock).toHaveLastFetched({
url: 'http://example.com/user',
body: {name: true}
}, 'post')
expect(await userManager.getAll()).toEqual([{name: 'bob'}])
fetchMock.mockClear()
await userManager.create({name: 'sarah'})
expect(fetchMock).toHaveLastFetched({
url: 'http://example.com/user',
body: {name: 'sarah'}
}, 'post')
expect(await userManager.getAll()).toEqual([
{name: 'bob'},
{name: 'sarah'}
])
fetchMock.mockReset()
})

const users = [{ name: 'bob' }];
fetchMock
.get('http://example.com/users', users)
.post('http://example.com/user', (url, options) => {
if (typeof options.body.name === 'string') {
users.push(options.body);
return 202;
}
return 400;
})
.patch(
{
url: 'http://example.com/user'
},
405
);

expect(await userManager.getAll()).toEqual([{ name: 'bob' }]);
expect(fetchMock).toHaveLastFetched('http://example.com/users', 'get');
await userManager.create({ name: true });
expect(fetchMock).toHaveLastFetched(
{
url: 'http://example.com/user',
body: { name: true }
},
'post'
);
expect(await userManager.getAll()).toEqual([{ name: 'bob' }]);
fetchMock.mockClear();
await userManager.create({ name: 'sarah' });
expect(fetchMock).toHaveLastFetched(
{
url: 'http://example.com/user',
body: { name: 'sarah' }
},
'post'
);
expect(await userManager.getAll()).toEqual([
{ name: 'bob' },
{ name: 'sarah' }
]);
fetchMock.mockReset();
});
```
46 changes: 35 additions & 11 deletions __tests__/jest-built-ins.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,39 @@ describe('jest built-ins', () => {
);
});
});

// .toHaveBeenCalled()
// .toHaveBeenCalledTimes(number)
// .toHaveBeenCalledWith(arg1, arg2, ...)
// .toHaveBeenLastCalledWith(arg1, arg2, ...)
// .toHaveBeenNthCalledWith(nthCall, arg1, arg2, ....)
// .toHaveReturned()
// .toHaveReturnedTimes(number)
// .toHaveReturnedWith(value)
// .toHaveLastReturnedWith(value)
// .toHaveNthReturnedWith(nthCall, value)
describe('native jest mock function inspectors', () => {
it('.toHaveBeenCalled()', () => {
expect(() => expect(fetch).toHaveBeenCalled()).not.toThrow();
});
// Just want to get the fix out for calling fetch methods
// These will all work as the basic mechanism is fixed, but
// no time to set up all th etest cases now
it.skip('.toHaveBeenCalledTimes(number)', () => {
expect(() => expect(fetch).toHaveBeenCalledTimes(1)).not.toThrow();
});
it.skip('.toHaveBeenCalledWith(arg1, arg2, ...)', () => {
expect(() => expect(fetch).toHaveBeenCalledWith(1)).not.toThrow();
});
it.skip('.toHaveBeenLastCalledWith(arg1, arg2, ...)', () => {
expect(() => expect(fetch).toHaveBeenLastCalledWith(1)).not.toThrow();
});
it.skip('.toHaveBeenNthCalledWith(nthCall, arg1, arg2, ....)', () => {
expect(() => expect(fetch).toHaveBeenNthCalledWith(1, 1)).not.toThrow();
});
it.skip('.toHaveReturned()', () => {
expect(() => expect(fetch).toHaveReturned()).not.toThrow();
});
it.skip('.toHaveReturnedTimes(number)', () => {
expect(() => expect(fetch).toHaveReturnedTimes(1)).not.toThrow();
});
it.skip('.toHaveReturnedWith(value)', () => {
expect(() => expect(fetch).toHaveReturnedWith(1)).not.toThrow();
});
it.skip('.toHaveLastReturnedWith(value)', () => {
expect(() => expect(fetch).toHaveLastReturnedWith(1)).not.toThrow();
});
it.skip('.toHaveNthReturnedWith(nthCall, value)', () => {
expect(() => expect(fetch).toHaveNthReturnedWith(1, 1)).not.toThrow();
});
});
});
3 changes: 0 additions & 3 deletions jest.config.js

This file was deleted.

4 changes: 4 additions & 0 deletions jestify.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const jestify = fetchMockInstance => {
// make sure all the jest expectation helpers can find what they need on fetchMock.mock
Object.assign(jestifiedInstance.mock, jestifiedInstance.fetchHandler.mock);

['_isMockFunction', 'mockName', 'getMockName'].forEach(prop => {
jestifiedInstance[prop] = jestifiedInstance.fetchHandler[prop];
});

jestifiedInstance.mockClear = () => {
jestifiedInstance.fetchHandler.mockClear();
jestifiedInstance.resetHistory();
Expand Down
Loading

0 comments on commit f92b61c

Please sign in to comment.