tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

perf-service.test.js (2706B)


      1 /* globals assert, beforeEach, describe, it */
      2 import { _PerfService } from "content-src/lib/perf-service";
      3 import { FakePerformance } from "test/unit/utils.js";
      4 
      5 let perfService;
      6 
      7 describe("_PerfService", () => {
      8  let sandbox;
      9  let fakePerfObj;
     10 
     11  beforeEach(() => {
     12    sandbox = sinon.createSandbox();
     13    fakePerfObj = new FakePerformance();
     14    perfService = new _PerfService({ performanceObj: fakePerfObj });
     15  });
     16 
     17  afterEach(() => {
     18    sandbox.restore();
     19  });
     20 
     21  describe("#absNow", () => {
     22    it("should return a number > the time origin", () => {
     23      const absNow = perfService.absNow();
     24 
     25      assert.isAbove(absNow, perfService.timeOrigin);
     26    });
     27  });
     28  describe("#getEntriesByName", () => {
     29    it("should call getEntriesByName on the appropriate Window.performance", () => {
     30      sandbox.spy(fakePerfObj, "getEntriesByName");
     31 
     32      perfService.getEntriesByName("monkey", "mark");
     33 
     34      assert.calledOnce(fakePerfObj.getEntriesByName);
     35      assert.calledWithExactly(fakePerfObj.getEntriesByName, "monkey", "mark");
     36    });
     37 
     38    it("should return entries with the given name", () => {
     39      sandbox.spy(fakePerfObj, "getEntriesByName");
     40      perfService.mark("monkey");
     41      perfService.mark("dog");
     42 
     43      let marks = perfService.getEntriesByName("monkey", "mark");
     44 
     45      assert.isArray(marks);
     46      assert.lengthOf(marks, 1);
     47      assert.propertyVal(marks[0], "name", "monkey");
     48    });
     49  });
     50 
     51  describe("#getMostRecentAbsMarkStartByName", () => {
     52    it("should throw an error if there is no mark with the given name", () => {
     53      function bogusGet() {
     54        perfService.getMostRecentAbsMarkStartByName("rheeeet");
     55      }
     56 
     57      assert.throws(bogusGet, Error, /No marks with the name/);
     58    });
     59 
     60    it("should return the Number from the most recent mark with the given name + the time origin", () => {
     61      perfService.mark("dog");
     62      perfService.mark("dog");
     63 
     64      let absMarkStart = perfService.getMostRecentAbsMarkStartByName("dog");
     65 
     66      // 2 because we want the result of the 2nd call to mark, and an instance
     67      // of FakePerformance just returns the number of time mark has been
     68      // called.
     69      assert.equal(absMarkStart - perfService.timeOrigin, 2);
     70    });
     71  });
     72 
     73  describe("#mark", () => {
     74    it("should call the wrapped version of mark", () => {
     75      sandbox.spy(fakePerfObj, "mark");
     76 
     77      perfService.mark("monkey");
     78 
     79      assert.calledOnce(fakePerfObj.mark);
     80      assert.calledWithExactly(fakePerfObj.mark, "monkey");
     81    });
     82  });
     83 
     84  describe("#timeOrigin", () => {
     85    it("should get the origin of the wrapped performance object", () => {
     86      assert.equal(perfService.timeOrigin, fakePerfObj.timeOrigin);
     87    });
     88  });
     89 });