Skip to content

Commit

Permalink
split up tests to better describe expected behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
leggsimon committed Sep 10, 2019
1 parent 996c2fe commit b2189c3
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions test/specs/routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,23 +553,38 @@ module.exports = fetchMock => {
});

describe('body matching', () => {
it('should match against a JSON body', async () => {
it('should not match if no body provided in request', async () => {
fm.mock('http://it.at.there/', 200, { body: { foo: 'bar' } }).catch();

await fm.fetchHandler('http://it.at.there/', {
method: 'POST'
});
expect(fm.calls(true).length).to.equal(0);
});

it('should not match if the content type in request isn’t application/json', async () => {
fm.mock('http://it.at.there/', 200, { body: { foo: 'bar' } }).catch();

await fm.fetchHandler('http://it.at.there/', {
method: 'POST',
body: JSON.stringify({ baz: 'blah' })
body: JSON.stringify({ foo: 'bar' })
});
expect(fm.calls(true).length).to.equal(0);
});

it('should not match if no body provided in request', async () => {
fm.mock('http://it.at.there/', 200, { body: { foo: 'bar' } }).catch();

await fm.fetchHandler('http://it.at.there/', {
method: 'POST',
body: JSON.stringify({ foo: 'bar' })
});
expect(fm.calls(true).length).to.equal(0);
});

it('should match if body sent matches expected body', async () => {
fm.mock('http://it.at.there/', 200, { body: { foo: 'bar' } }).catch();

await fm.fetchHandler('http://it.at.there/', {
method: 'POST',
body: JSON.stringify({ foo: 'bar' }),
Expand All @@ -578,6 +593,17 @@ module.exports = fetchMock => {
expect(fm.calls(true).length).to.equal(1);
});

it('should not match if body sent doesn’t match expected body', async () => {
fm.mock('http://it.at.there/', 200, { body: { foo: 'bar' } }).catch();

await fm.fetchHandler('http://it.at.there/', {
method: 'POST',
body: JSON.stringify({ foo: 'woah!!!' }),
headers: { 'Content-Type': 'application/json' }
});
expect(fm.calls(true).length).to.equal(0);
});

it('should ignore the order of the keys in the body', async () => {
fm.mock('http://it.at.there/', 200, {
body: {
Expand Down

0 comments on commit b2189c3

Please sign in to comment.