UNPKG

3.7 kBJavaScriptView Raw
1describe('flux-interactive-perf', function() {
2 before(function(done) {
3 var self = this;
4 require([
5 'flux-interactive-perf',
6 'flux-print'
7 ], function(interactivePerfCreator, print) {
8 self.interactivePerfCreator = interactivePerfCreator;
9 self.print = print;
10 done();
11 });
12 });
13
14 beforeEach(function() {
15 this.performanceSpy = sinon.stub(window.performance, 'now');
16 this.logSpy = sinon.spy(this.print, 'log');
17 this.debugSpy = sinon.spy(this.print, 'debug');
18 });
19
20 afterEach(function() {
21 this.performanceSpy.restore();
22 this.logSpy.restore();
23 this.debugSpy.restore();
24 });
25
26 describe('interactivePerfCreator', function() {
27 it('should be able to create independent interactivePerfs', function() {
28 var interactivePerf1 = this.interactivePerfCreator();
29 var interactivePerf2 = this.interactivePerfCreator();
30
31 interactivePerf1.startRecording();
32
33 this.performanceSpy.returns(0);
34 interactivePerf1.begin();
35
36 this.performanceSpy.returns(500);
37 interactivePerf1.end();
38 interactivePerf1.begin();
39 interactivePerf2.begin();
40
41 this.performanceSpy.returns(1000);
42 interactivePerf1.end();
43 interactivePerf2.end('INTERACTIVE PERF 2');
44
45 interactivePerf1.stopRecording();
46
47 expect(this.logSpy).to.have.been.calledWith('Average duration: 500 ms');
48 expect(this.logSpy).to.have.been.calledWith('Average FPS: 2.00');
49 expect(this.logSpy).to.have.been.calledWith('Number of samples: 2');
50 expect(this.logSpy).to.have.been.calledWith('INTERACTIVE PERF 2: 500 ms');
51 });
52 });
53
54 describe('interactivePerf', function() {
55 beforeEach(function() {
56 this.interactivePerf = this.interactivePerfCreator();
57 });
58
59 describe('when not recording', function() {
60 it('should log the time difference between #begin and #end', function() {
61 this.performanceSpy.returns(100);
62 this.interactivePerf.begin();
63
64 this.performanceSpy.returns(500);
65 this.interactivePerf.end('TEST MESSAGE');
66
67 expect(this.logSpy).to.have.been.calledWith('TEST MESSAGE: 400 ms');
68 });
69
70 it('#end should log a default message', function() {
71 this.performanceSpy.returns(0);
72 this.interactivePerf.begin();
73
74 this.performanceSpy.returns(500);
75 this.interactivePerf.end();
76
77 expect(this.debugSpy).to.have.been.calledWith('Unrecorded: 500 ms');
78 });
79 });
80
81 describe('when recording', function() {
82 beforeEach(function() {
83 this.interactivePerf.startRecording();
84 });
85
86 describe('if there are no samples', function() {
87 it('should log that there are no results', function() {
88 this.interactivePerf.stopRecording();
89
90 expect(this.logSpy).to.have.been.calledWith('Finished recording with no results');
91 });
92 });
93
94 describe('if there are samples', function() {
95 beforeEach(function() {
96 this.performanceSpy.returns(0);
97 this.interactivePerf.begin();
98
99 this.performanceSpy.returns(1000);
100 this.interactivePerf.end();
101
102 this.performanceSpy.returns(2000);
103 this.interactivePerf.begin();
104
105 this.performanceSpy.returns(5000);
106 this.interactivePerf.end();
107 });
108
109 it('should log the results', function() {
110 this.interactivePerf.stopRecording();
111
112 expect(this.logSpy).to.have.been.calledWith('Average duration: 2.00 seconds');
113 expect(this.logSpy).to.have.been.calledWith('Average FPS: 0.50');
114 expect(this.logSpy).to.have.been.calledWith('Number of samples: 2');
115 });
116 });
117 });
118 });
119});