Skip to content

Commit 5196dbd

Browse files
farhan-saucechristian-bromann
authored andcommitted
fix data mutation issue (#12)
* fix data mutation issue * update unit tests
1 parent 29ddf4d commit 5196dbd

3 files changed

Lines changed: 75 additions & 50 deletions

File tree

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ export default class Tracelib {
5151
throw new Error('MainTrack is missing in traceLog')
5252
}
5353

54+
// We are facing data mutaion issue in devtools, to avoid it cloning syncEvents
55+
const syncEvents = mainTrack.syncEvents().slice()
56+
5457
return {
5558
...timelineUtils.statsForTimeRange(
56-
mainTrack.syncEvents(), startTime, endTime
59+
syncEvents, startTime, endTime
5760
),
5861
startTime,
5962
endTime,

tests/__snapshots__/index.test.ts.snap

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`getSummary: should get summary data 1`] = `
3+
exports[`getSummary should get summary data 1`] = `
44
Object {
55
"endTime": 289961229.717,
66
"idle": 52.38300037384033,
@@ -12,7 +12,31 @@ Object {
1212
}
1313
`;
1414

15-
exports[`getWarningCounts: should get warning counts 1`] = `
15+
exports[`getSummary should get summary data between passed range 1`] = `
16+
Object {
17+
"endTime": 289960729.717,
18+
"idle": 0.6339998841285706,
19+
"other": 4.653000295162201,
20+
"painting": 34.8999999165535,
21+
"rendering": 425.89399832487106,
22+
"scripting": 208.0020015835762,
23+
"startTime": 289960055.634,
24+
}
25+
`;
26+
27+
exports[`getSummary should not throw error on second call of getSummary 1`] = `
28+
Object {
29+
"endTime": 289961229.717,
30+
"idle": 52.38300037384033,
31+
"other": 9.896000564098358,
32+
"painting": 69.94999980926514,
33+
"rendering": 847.373997092247,
34+
"scripting": 394.4800021648407,
35+
"startTime": 289959855.634,
36+
}
37+
`;
38+
39+
exports[`getWarningCounts should get warning counts 1`] = `
1640
Object {
1741
"ForcedLayout": 4683,
1842
"ForcedStyle": 4684,
@@ -119,15 +143,3 @@ Object {
119143
},
120144
}
121145
`;
122-
123-
exports[`should get summary data between passed range 1`] = `
124-
Object {
125-
"endTime": 289960729.717,
126-
"idle": 0.6339998841285706,
127-
"other": 4.653000295162201,
128-
"painting": 34.8999999165535,
129-
"rendering": 425.89399832487106,
130-
"scripting": 208.0020015835762,
131-
"startTime": 289960055.634,
132-
}
133-
`;

tests/index.test.ts

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,71 @@
11
import Tracelib from '../src/index'
22
import JANK_TRACE_LOG from './__fixtures__/jankTraceLog.json'
33

4+
let trace: Tracelib
5+
beforeAll(() => {
6+
trace = new Tracelib(JANK_TRACE_LOG)
7+
})
8+
49
test('should contain traceLog', () => {
510
const sampleTrace = new Tracelib({ foo: 'bar' })
611
expect(sampleTrace.tracelog).toEqual({ foo: 'bar' })
712
})
813

914
test('should get FPS', () => {
10-
const trace = new Tracelib(JANK_TRACE_LOG)
1115
const result = trace.getFPS()
1216
expect(result).toMatchSnapshot()
1317
})
1418

15-
test('getSummary: should get summary data', () => {
16-
const trace = new Tracelib(JANK_TRACE_LOG)
17-
const result = trace.getSummary()
18-
expect(result).toMatchSnapshot()
19-
})
19+
describe('getSummary', () => {
20+
it('should get summary data', () => {
21+
const result = trace.getSummary()
22+
expect(result).toMatchSnapshot()
23+
})
2024

21-
test('getSummary: should throw error if main track is missing', () => {
22-
const trace = new Tracelib([])
23-
expect(() => trace.getSummary())
24-
.toThrow(new Error('MainTrack is missing in traceLog'))
25-
})
25+
it('should not throw error on second call of getSummary', () => {
26+
const result = trace.getSummary()
27+
expect(result).toMatchSnapshot()
28+
})
2629

27-
test('should get summary data between passed range', () => {
28-
const trace = new Tracelib(JANK_TRACE_LOG)
29-
const result = trace.getSummary(289960055.634, 289960729.717)
30-
expect(result).toMatchSnapshot()
31-
})
30+
it('should throw error if main track is missing', () => {
31+
const tracelib = new Tracelib([])
32+
expect(() => tracelib.getSummary())
33+
.toThrow(new Error('MainTrack is missing in traceLog'))
34+
})
3235

33-
test('getWarningCounts: should get warning counts', () => {
34-
const trace = new Tracelib(JANK_TRACE_LOG)
35-
const result = trace.getWarningCounts()
36-
expect(result).toMatchSnapshot()
36+
it('should get summary data between passed range', () => {
37+
const result = trace.getSummary(289960055.634, 289960729.717)
38+
expect(result).toMatchSnapshot()
39+
})
3740
})
3841

39-
test('getWarningCounts: should throw error if main track is missing', () => {
40-
const trace = new Tracelib([])
41-
expect(() => trace.getWarningCounts())
42-
.toThrow(new Error('MainTrack is missing in traceLog'))
42+
describe('getWarningCounts', () => {
43+
it('should get warning counts', () => {
44+
const result = trace.getWarningCounts()
45+
expect(result).toMatchSnapshot()
46+
})
47+
48+
it('should throw error if main track is missing', () => {
49+
const tracelib = new Tracelib([])
50+
expect(() => tracelib.getWarningCounts())
51+
.toThrow(new Error('MainTrack is missing in traceLog'))
52+
})
4353
})
4454

4555
test('should get memory counters', () => {
46-
const trace = new Tracelib(JANK_TRACE_LOG)
4756
const result = trace.getMemoryCounters()
4857
expect(result).toMatchSnapshot()
4958
})
5059

51-
test('mainTrackEvents: should get events', () => {
52-
const trace = new Tracelib(JANK_TRACE_LOG)
53-
const result = trace.getMainTrackEvents()
54-
expect(result.length).toEqual(56244)
55-
})
60+
describe('mainTrackEvents', () => {
61+
it('should get events', () => {
62+
const result = trace.getMainTrackEvents()
63+
expect(result.length).toEqual(56244)
64+
})
5665

57-
test('mainTrackEvents: should throws error if main track is missing', () => {
58-
const trace = new Tracelib([])
59-
expect(() => trace.getMainTrackEvents())
60-
.toThrow(new Error('MainTrack is missing in traceLog'))
61-
})
66+
it('should throws error if main track is missing', () => {
67+
const tracelib = new Tracelib([])
68+
expect(() => tracelib.getMainTrackEvents())
69+
.toThrow(new Error('MainTrack is missing in traceLog'))
70+
})
71+
})

0 commit comments

Comments
 (0)