From 458305709ea3ef0cdb7005af848a31805cd91760 Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Mon, 18 May 2020 21:23:47 +0100 Subject: [PATCH] tidy repeat tests --- test/specs/repeat.test.js | 141 +++++++++--------- ...sponses.test.js => responses_TODO.test.js} | 0 .../{routing.test.js => routing_TODO.test.js} | 0 3 files changed, 69 insertions(+), 72 deletions(-) rename test/specs/{responses.test.js => responses_TODO.test.js} (100%) rename test/specs/{routing.test.js => routing_TODO.test.js} (100%) diff --git a/test/specs/repeat.test.js b/test/specs/repeat.test.js index b51abc90..a2a48c12 100644 --- a/test/specs/repeat.test.js +++ b/test/specs/repeat.test.js @@ -14,126 +14,127 @@ 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 @@ -141,65 +142,61 @@ describe('repeat and done()', () => { 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(); @@ -207,11 +204,11 @@ describe('repeat and done()', () => { 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; diff --git a/test/specs/responses.test.js b/test/specs/responses_TODO.test.js similarity index 100% rename from test/specs/responses.test.js rename to test/specs/responses_TODO.test.js diff --git a/test/specs/routing.test.js b/test/specs/routing_TODO.test.js similarity index 100% rename from test/specs/routing.test.js rename to test/specs/routing_TODO.test.js