Skip to content

Commit

Permalink
DRYer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Mar 14, 2020
1 parent cf95e6e commit 11abf0e
Showing 1 changed file with 48 additions and 111 deletions.
159 changes: 48 additions & 111 deletions test/specs/shorthands.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,160 +19,97 @@ const testChainableMethod = (getFetchMock, method, args = []) => {
module.exports = fetchMock => {
describe('shorthands', () => {
let fm;
let expectRoute;
before(() => {
fm = fetchMock.createInstance();
sinon.spy(fm, 'compileRoute');
fm.config.warnOnUnmatched = false;
expectRoute = (...args) => expect(fm.compileRoute).calledWith(args);
});
afterEach(() => {
fm.compileRoute.resetHistory();
fm.restore();
});
afterEach(() => fm.restore());

after(() => fm.compileRoute.restore());

it('has once() shorthand method', () => {
sinon.spy(fm, 'compileRoute');
fm.once('a', 'b');
fm.once('c', 'd', { opt: 'e' });
expect(
fm.compileRoute.calledWith([
'a',
'b',
{
repeat: 1
}
])
).to.be.true;
expect(
fm.compileRoute.calledWith([
'c',
'd',
{
opt: 'e',
repeat: 1
}
])
).to.be.true;
fm.compileRoute.restore();
expectRoute('a', 'b', {
repeat: 1
});
expectRoute('c', 'd', {
opt: 'e',
repeat: 1
});
});

it('has any() shorthand method', () => {
sinon.spy(fm, 'compileRoute');
fm.any('a', { opt: 'b' });
expect(
fm.compileRoute.calledWith([
{},
'a',
{
opt: 'b'
}
])
).to.be.true;
fm.compileRoute.restore();
expectRoute({}, 'a', {
opt: 'b'
});
});

it('has anyOnce() shorthand method', () => {
sinon.spy(fm, 'compileRoute');
fm.anyOnce('a', { opt: 'b' });
expect(
fm.compileRoute.calledWith([
{},
'a',
{
opt: 'b',
repeat: 1
}
])
).to.be.true;
fm.compileRoute.restore();
expectRoute({}, 'a', {
opt: 'b',
repeat: 1
});
});

describe('method shorthands', () => {
['get', 'post', 'put', 'delete', 'head', 'patch'].forEach(method => {
describe(method.toUpperCase(), () => {
it(`has ${method}() shorthand`, () => {
sinon.spy(fm, 'compileRoute');
fm[method]('a', 'b');
fm[method]('c', 'd', { opt: 'e' });
expect(fm.compileRoute).calledWith([
'a',
'b',
{
method: method
}
]);
expect(fm.compileRoute).calledWith([
'c',
'd',
{
opt: 'e',
method: method
}
]);
fm.compileRoute.restore();
fm.restore();
expectRoute('a', 'b', {
method: method
});
expectRoute('c', 'd', {
opt: 'e',
method: method
});
});

testChainableMethod(() => fm, method, [/a/, 200]);

it(`has ${method}Once() shorthand`, () => {
sinon.spy(fm, 'compileRoute');
fm[method + 'Once']('a', 'b');
fm[method + 'Once']('c', 'd', { opt: 'e' });
expect(
fm.compileRoute.calledWith([
'a',
'b',
{
method: method,
repeat: 1
}
])
).to.be.true;
expect(
fm.compileRoute.calledWith([
'c',
'd',
{
opt: 'e',
method: method,
repeat: 1
}
])
).to.be.true;
fm.compileRoute.restore();
expectRoute('a', 'b', {
method: method,
repeat: 1
});
expectRoute('c', 'd', {
opt: 'e',
method: method,
repeat: 1
});
});

testChainableMethod(() => fm, `${method}Once`, [/a/, 200]);

it(`has ${method}Any() shorthand`, () => {
sinon.spy(fm, 'compileRoute');
fm[method + 'Any']('a', { opt: 'b' });
expect(
fm.compileRoute.calledWith([
{},
'a',
{
opt: 'b',
method: method
}
])
).to.be.true;
fm.compileRoute.restore();
expectRoute({}, 'a', {
opt: 'b',
method: method
});
});

testChainableMethod(() => fm, `${method}Any`, [200]);

it(`has ${method}AnyOnce() shorthand`, () => {
sinon.spy(fm, 'compileRoute');
fm[method + 'AnyOnce']('a', { opt: 'b' });
expect(
fm.compileRoute.calledWith([
{},
'a',
{
opt: 'b',
method: method,
repeat: 1
}
])
).to.be.true;
fm.compileRoute.restore();
expectRoute({}, 'a', {
opt: 'b',
method: method,
repeat: 1
});
});

testChainableMethod(() => fm, `${method}Any`, [200]);
Expand Down

0 comments on commit 11abf0e

Please sign in to comment.