Skip to content

Commit

Permalink
tidy repeat tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed May 18, 2020
1 parent 924759e commit 4583057
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 72 deletions.
141 changes: 69 additions & 72 deletions test/specs/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,204 +14,201 @@ describe('repeat and done()', () => {
afterEach(() => fm.restore());

it('can expect a route to be called', async () => {
fm.mock('http://it.at.there1/', 200);
fm.mock('http://a.com/', 200);

expect(fm.done()).to.be.false;
expect(fm.done('http://it.at.there1/')).to.be.false;
fm.fetchHandler('http://it.at.there1/');
expect(fm.done('http://a.com/')).to.be.false;
fm.fetchHandler('http://a.com/');
expect(fm.done()).to.be.true;
expect(fm.done('http://it.at.there1/')).to.be.true;
expect(fm.done('http://a.com/')).to.be.true;
});

it('can expect a route to be called n times', async () => {
fm.mock('http://it.at.there1/', 200, { repeat: 2 });
fm.mock('http://a.com/', 200, { repeat: 2 });

fm.fetchHandler('http://it.at.there1/');
fm.fetchHandler('http://a.com/');
expect(fm.done()).to.be.false;
expect(fm.done('http://it.at.there1/')).to.be.false;
fm.fetchHandler('http://it.at.there1/');
expect(fm.done('http://a.com/')).to.be.false;
fm.fetchHandler('http://a.com/');
expect(fm.done()).to.be.true;
expect(fm.done('http://it.at.there1/')).to.be.true;
expect(fm.done('http://a.com/')).to.be.true;
});

it('regression: can expect an un-normalized url to be called n times', async () => {
fm.mock('http://it.at.there1', 200, { repeat: 2 });
fm.fetchHandler('http://it.at.there1');
fm.mock('http://a.com/', 200, { repeat: 2 });
fm.fetchHandler('http://a.com/');
expect(fm.done()).to.be.false;
fm.fetchHandler('http://it.at.there1');
fm.fetchHandler('http://a.com/');
expect(fm.done()).to.be.true;
});

it('can expect multiple routes to have been called', async () => {
fm.mock('http://it.at.there1/', 200, {
fm.mock('http://a.com/', 200, {
repeat: 2,
}).mock('http://it.at.there2/', 200, { repeat: 2 });
}).mock('http://b.com/', 200, { repeat: 2 });

fm.fetchHandler('http://it.at.there1/');
fm.fetchHandler('http://a.com/');
expect(fm.done()).to.be.false;
expect(fm.done('http://it.at.there1/')).to.be.false;
expect(fm.done('http://it.at.there2/')).to.be.false;
fm.fetchHandler('http://it.at.there1/');
expect(fm.done('http://a.com/')).to.be.false;
expect(fm.done('http://b.com/')).to.be.false;
fm.fetchHandler('http://a.com/');
expect(fm.done()).to.be.false;
expect(fm.done('http://it.at.there1/')).to.be.true;
expect(fm.done('http://it.at.there2/')).to.be.false;
fm.fetchHandler('http://it.at.there2/');
expect(fm.done('http://a.com/')).to.be.true;
expect(fm.done('http://b.com/')).to.be.false;
fm.fetchHandler('http://b.com/');
expect(fm.done()).to.be.false;
expect(fm.done('http://it.at.there1/')).to.be.true;
expect(fm.done('http://it.at.there2/')).to.be.false;
fm.fetchHandler('http://it.at.there2/');
expect(fm.done('http://a.com/')).to.be.true;
expect(fm.done('http://b.com/')).to.be.false;
fm.fetchHandler('http://b.com/');
expect(fm.done()).to.be.true;
expect(fm.done('http://it.at.there1/')).to.be.true;
expect(fm.done('http://it.at.there2/')).to.be.true;
expect(fm.done('http://a.com/')).to.be.true;
expect(fm.done('http://b.com/')).to.be.true;
});

// todo more tests for filtering
it('`done` filters on match types', async () => {
fm.once('http://it.at.here/', 200)
.once('http://it.at.there/', 200)
.once('http://it.at.thereabouts/', 200)
fm.once('http://a.com/', 200)
.once('http://b.com/', 200)
.once('http://c.com/', 200)
.catch();

await fm.fetchHandler('http://it.at.here/', { method: 'get' });
await fm.fetchHandler('http://it.at.there/', { method: 'get' });
await fm.fetchHandler('http://a.com/');
await fm.fetchHandler('http://b.com/');
expect(fm.done()).to.be.false;
expect(fm.done(true)).to.be.false;
expect(fm.done('http://it.at.here/')).to.be.true;
expect(fm.done('http://it.at.thereabouts/')).to.be.false;
expect(fm.done('http://a.com/')).to.be.true;
expect(fm.done('http://b.com/')).to.be.true;
expect(fm.done('http://c.com/')).to.be.false;
});

it("won't mock if route already matched enough times", async () => {
fm.mock('http://it.at.there1/', 200, { repeat: 1 });
fm.mock('http://a.com/', 200, { repeat: 1 });

await fm.fetchHandler('http://it.at.there1/');
await fm.fetchHandler('http://a.com/');
try {
await fm.fetchHandler('http://it.at.there1/');
await fm.fetchHandler('http://a.com/');
expect(true).to.be.false;
} catch (err) {}
});

it('falls back to second route if first route already done', async () => {
fm.mock('http://it.at.there1/', 404, {
fm.mock('http://a.com/', 404, {
repeat: 1,
}).mock('http://it.at.there1/', 200, { overwriteRoutes: false });
}).mock('http://a.com/', 200, { overwriteRoutes: false });

const res = await fm.fetchHandler('http://it.at.there1/');
const res = await fm.fetchHandler('http://a.com/');
expect(res.status).to.equal(404);

const res2 = await fm.fetchHandler('http://it.at.there1/');
const res2 = await fm.fetchHandler('http://a.com/');
expect(res2.status).to.equal(200);
});

it('resetHistory() resets count', async () => {
fm.mock('http://it.at.there1/', 200, { repeat: 1 });
await fm.fetchHandler('http://it.at.there1/');
fm.mock('http://a.com/', 200, { repeat: 1 });
await fm.fetchHandler('http://a.com/');
expect(fm.done()).to.be.true;
fm.resetHistory();
expect(fm.done()).to.be.false;
expect(fm.done('http://it.at.there1/')).to.be.false;
await fm.fetchHandler('http://it.at.there1/');
expect(fm.done('http://a.com/')).to.be.false;
await fm.fetchHandler('http://a.com/');
expect(fm.done()).to.be.true;
expect(fm.done('http://it.at.there1/')).to.be.true;
expect(fm.done('http://a.com/')).to.be.true;
});

it('logs unmatched calls', () => {
sinon.spy(console, 'warn'); //eslint-disable-line
fm.mock('http://it.at.there1/', 200).mock('http://it.at.there2/', 200, {
fm.mock('http://a.com/', 200).mock('http://b.com/', 200, {
repeat: 2,
});

fm.fetchHandler('http://it.at.there2/');
fm.fetchHandler('http://b.com/');
fm.done();
expect(console.warn.calledWith('Warning: http://it.at.there1/ not called')).to.be.true; //eslint-disable-line
expect(console.warn.calledWith('Warning: http://a.com/ not called')).to.be.true; //eslint-disable-line
expect(
console.warn.calledWith(
'Warning: http://it.at.there2/ only called 1 times, but 2 expected'
'Warning: http://b.com/ only called 1 times, but 2 expected'
)
).to.be.true; //eslint-disable-line
console.warn.resetHistory(); //eslint-disable-line
fm.done('http://it.at.there1/');
expect(console.warn.calledWith('Warning: http://it.at.there1/ not called')).to.be.true; //eslint-disable-line
fm.done('http://a.com/');
expect(console.warn.calledWith('Warning: http://a.com/ not called')).to.be.true; //eslint-disable-line
expect(
console.warn.calledWith(
'Warning: http://it.at.there2/ only called 1 times, but 2 expected'
'Warning: http://b.com/ only called 1 times, but 2 expected'
)
).to.be.false; //eslint-disable-line
console.warn.restore(); //eslint-disable-line
});

describe('sandbox isolation', () => {
it("doesn't propagate to children of global", () => {
fm.mock('http://it.at.there/', 200, { repeat: 1 });
fm.mock('http://a.com/', 200, { repeat: 1 });

const sb1 = fm.sandbox();

fm.fetchHandler('http://it.at.there/');
fm.fetchHandler('http://a.com/');

expect(fm.done()).to.be.true;
expect(sb1.done()).to.be.false;

expect(() => sb1.fetchHandler('http://it.at.there/')).not.to.throw();
expect(() => sb1.fetchHandler('http://a.com/')).not.to.throw();
});

it("doesn't propagate to global from children", () => {
fm.mock('http://it.at.there/', 200, { repeat: 1 });
fm.mock('http://a.com/', 200, { repeat: 1 });

const sb1 = fm.sandbox();

sb1.fetchHandler('http://it.at.there/');
sb1.fetchHandler('http://a.com/');

expect(fm.done()).to.be.false;
expect(sb1.done()).to.be.true;

expect(() => fm.fetchHandler('http://it.at.there/')).not.to.throw();
expect(() => fm.fetchHandler('http://a.com/')).not.to.throw();
});

it("doesn't propagate to children of sandbox", () => {
const sb1 = fm.sandbox().mock('http://it.at.there/', 200, { repeat: 1 });
const sb1 = fm.sandbox().mock('http://a.com/', 200, { repeat: 1 });

const sb2 = sb1.sandbox();

sb1.fetchHandler('http://it.at.there/');
sb1.fetchHandler('http://a.com/');

expect(sb1.done()).to.be.true;
expect(sb2.done()).to.be.false;

expect(() => sb2.fetchHandler('http://it.at.there/')).not.to.throw();
expect(() => sb2.fetchHandler('http://a.com/')).not.to.throw();
});

it("doesn't propagate to sandbox from children", () => {
const sb1 = fm.sandbox().mock('http://it.at.there/', 200, { repeat: 1 });
const sb1 = fm.sandbox().mock('http://a.com/', 200, { repeat: 1 });

const sb2 = sb1.sandbox();

sb2.fetchHandler('http://it.at.there/');
sb2.fetchHandler('http://a.com/');

expect(sb1.done()).to.be.false;
expect(sb2.done()).to.be.true;

expect(() => sb1.fetchHandler('http://it.at.there/')).not.to.throw();
expect(() => sb1.fetchHandler('http://a.com/')).not.to.throw();
});

it('Allow overwriting routes when using multiple function matchers', async () => {
function matcher1() {
return true;
}
const matcher1 = () => true;

function matcher2() {
return true;
}
const matcher2 = () => true;

const sb = fm.sandbox();

expect(() =>
sb.postOnce(matcher1, 200).postOnce(matcher2, 200)
).not.to.throw();

await sb('https://example.com', { method: 'POST' });
await sb('https://example.com/', { method: 'POST' });
expect(sb.done()).to.be.false;
expect(sb.done(matcher1)).to.be.true;
expect(sb.done(matcher2)).to.be.false;
await sb('https://example.com', { method: 'POST' });
await sb('https://example.com/', { method: 'POST' });

expect(sb.done()).to.be.true;
expect(sb.done(matcher1)).to.be.true;
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 4583057

Please sign in to comment.