Skip to content

Commit 24c8996

Browse files
committed
refactor: Init EmulatedPerformance service
1 parent 5bf3e32 commit 24c8996

9 files changed

+416
-412
lines changed

Diff for: __tests__/emnulated-performance.spec.ts

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import EmulatedPerformance from "../src/emulated-performance";
2+
3+
/**
4+
* Emulated performance test
5+
*/
6+
describe("Emulated performance test", () => {
7+
let service;
8+
9+
beforeEach(() => {
10+
window.Date = {
11+
now: () => {
12+
return 1000;
13+
},
14+
};
15+
window.performance = {
16+
timing: {
17+
navigationStart: 0,
18+
},
19+
};
20+
service = new EmulatedPerformance();
21+
service.config = {
22+
logPrefix: "",
23+
};
24+
});
25+
26+
beforeEach(() => {
27+
spyOn(global.console, "warn").and.callThrough();
28+
spyOn(service, "now").and.callThrough();
29+
spyOn(service, "mark").and.callThrough();
30+
spyOn(service, "measure").and.callThrough();
31+
spyOn(service, "firstContentfulPaint").and.callThrough();
32+
spyOn(service, "getDurationByMetric").and.callThrough();
33+
spyOn(service, "getFirstPaint").and.callThrough();
34+
});
35+
36+
describe("when calls now()", () => {
37+
it("should return Date.now() / 1000", () => {
38+
expect(service.now()).toEqual(1);
39+
});
40+
});
41+
42+
describe("when calls mark()", () => {
43+
it("should call global.console.warn with the correct arguments", () => {
44+
service.mark("fibonacci");
45+
const text = "Timeline won't be marked for \"fibonacci\".";
46+
expect(global.console.warn.calls.count()).toEqual(1);
47+
expect(global.console.warn).toHaveBeenCalledWith(service.config.logPrefix, text);
48+
});
49+
});
50+
51+
describe("when calls measure()", () => {
52+
it("should call getDurationByMetric() with the correct arguments", () => {
53+
const metrics = {
54+
age: {
55+
end: 2018,
56+
start: 1987,
57+
},
58+
};
59+
service.measure("age", metrics);
60+
expect(service.getDurationByMetric.calls.count()).toEqual(1);
61+
expect(service.getDurationByMetric).toHaveBeenCalledWith("age", metrics);
62+
});
63+
});
64+
65+
describe("when calls firstContentfulPaint()", () => {
66+
beforeEach(() => {
67+
jest.useFakeTimers();
68+
});
69+
70+
it("should call getFirstPaint() after the setTimeout", () => {
71+
service.firstContentfulPaint(() => {
72+
return 1;
73+
});
74+
jest.runAllTimers();
75+
expect(service.getFirstPaint.calls.count()).toEqual(1);
76+
});
77+
});
78+
79+
describe("when calls getDurationByMetric()", () => {
80+
it("should return the duration", () => {
81+
const metrics = {
82+
age: {
83+
end: 2018,
84+
start: 1987,
85+
},
86+
};
87+
const duration = service.getDurationByMetric("age", metrics);
88+
expect(duration).toEqual(31);
89+
});
90+
91+
it("should return the -1 when duration is 0", () => {
92+
const metrics = {
93+
age: {
94+
end: 2018,
95+
start: 2018,
96+
},
97+
};
98+
const duration = service.getDurationByMetric("age", metrics);
99+
expect(duration).toEqual(-1);
100+
});
101+
});
102+
103+
describe("when calls getFirstPaint()", () => {
104+
it("should return 0 if PerformanceTiming.navigationStar is 0", () => {
105+
const performance = service.getFirstPaint();
106+
expect(performance).toEqual({
107+
duration: 0,
108+
entryType: "paint",
109+
name: "first-contentful-paint",
110+
startTime: 0,
111+
});
112+
});
113+
114+
it("should return performancePaintTiming", () => {
115+
window.performance.timing.navigationStart = 240;
116+
const performance = service.getFirstPaint();
117+
expect(performance).toEqual({
118+
duration: 0,
119+
entryType: "paint",
120+
name: "first-contentful-paint",
121+
startTime: 760,
122+
});
123+
});
124+
});
125+
});

0 commit comments

Comments
 (0)