import {CachedTargetingGetter} from "lib/ASRouterTargeting.jsm"; // Note that tests for the ASRouterTargeting environment can be found in // test/functional/mochitest/browser_asrouter_targeting.js describe("#CachedTargetingGetter", () => { const sixHours = 6 * 60 * 60 * 1000; let sandbox; let clock; let frecentStub; let topsitesCache; beforeEach(() => { sandbox = sinon.sandbox.create(); clock = sinon.useFakeTimers(); frecentStub = sandbox.stub(global.NewTabUtils.activityStreamProvider, "getTopFrecentSites"); sandbox.stub(global.Cu, "reportError"); topsitesCache = new CachedTargetingGetter("getTopFrecentSites"); }); afterEach(() => { sandbox.restore(); clock.restore(); }); it("should only make a request every 6 hours", async () => { frecentStub.resolves(); clock.tick(sixHours); await topsitesCache.get(); await topsitesCache.get(); assert.calledOnce(global.NewTabUtils.activityStreamProvider.getTopFrecentSites); clock.tick(sixHours); await topsitesCache.get(); assert.calledTwice(global.NewTabUtils.activityStreamProvider.getTopFrecentSites); }); it("should report errors", async () => { frecentStub.rejects(new Error("fake error")); clock.tick(sixHours); // assert.throws expect a function as the first parameter, try/catch is a // workaround try { await topsitesCache.get(); assert.isTrue(false); } catch (e) { assert.calledOnce(global.Cu.reportError); } }); });