1/* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import '../../../../src/trace/component/chart/SpUserPluginChart'; 17import { SpUserFileChart } from '../../../../src/trace/component/chart/SpUserPluginChart'; 18import { SpSystemTrace } from '../../../../src/trace/component/SpSystemTrace'; 19import { TraceRow } from '../../../../src/trace/component/trace/base/TraceRow'; 20import { SampleStruct } from '../../../../src/trace/database/ui-worker/ProcedureWorkerBpftrace'; 21jest.mock('../../../../src/trace/component/SpSystemTrace', () => { 22 return {}; 23}); 24jest.mock('../../../../src/trace/database/ui-worker/ProcedureWorkerSnapshot', () => { 25 return {}; 26}); 27jest.mock('../../../../src/trace/database/ui-worker/ProcedureWorker', () => { 28 return {}; 29}); 30jest.mock('../../../../src/js-heap/model/DatabaseStruct', () => {}); 31 32const sqlite = require('../../../../src/trace/database/sql/SqlLite.sql'); 33jest.mock('../../../../src/trace/database/sql/SqlLite.sql'); 34 35const intersectionObserverMock = () => ({ 36 observe: () => null, 37}); 38window.IntersectionObserver = jest.fn().mockImplementation(intersectionObserverMock); 39window.ResizeObserver = 40 window.ResizeObserver || 41 jest.fn().mockImplementation(() => ({ 42 disconnect: jest.fn(), 43 observe: jest.fn(), 44 unobserve: jest.fn(), 45 })); 46describe('SpUserFileChart Test', () => { 47 global.Worker = jest.fn(); 48 let startTimeData = sqlite.queryStartTime; 49 let mockStartTime = [ 50 { 51 start_ts: 10, 52 }, 53 ]; 54 startTimeData.mockResolvedValue([]); 55 startTimeData.mockResolvedValue(mockStartTime); 56 let htmlElement: SpSystemTrace = document.createElement('sp-system-trace') as SpSystemTrace; 57 58 let file = new File([], 'test.js'); 59 let traceRow = TraceRow.skeleton<SampleStruct>(); 60 let children = [ 61 { 62 function_name: 'func1', 63 children: [], 64 detail: '', 65 depth: 1, 66 name: '1', 67 parentName: 'p0', 68 property: [], 69 }, 70 ]; 71 let treeData = [ 72 { 73 function_name: 'func0', 74 children: children, 75 detail: '', 76 depth: 0, 77 name: '0', 78 parentName: '1', 79 property: [], 80 }, 81 ]; 82 it('SpUserFileChart Test01 ', function () { 83 let segmentationChart = new SpUserFileChart(htmlElement); 84 expect(segmentationChart.init(null)).not.toBeUndefined(); 85 expect(segmentationChart.init(file)).not.toBeUndefined(); 86 }); 87 it('SpUserFileChart Test02 ', function () { 88 let segmentationChart = new SpUserFileChart(htmlElement); 89 expect(segmentationChart.initSample(10, null)).not.toBeUndefined; 90 expect(segmentationChart.initSample(10, file)).not.toBeUndefined; 91 }); 92 it('SpUserFileChart Test03 ', function () { 93 let segmentationChart = new SpUserFileChart(htmlElement); 94 expect(segmentationChart.addTraceRowEventListener(traceRow, 10)).toBeUndefined(); 95 }); 96 it('SpUserFileChart Test04 ', function () { 97 let returnValue = [ 98 { depth: 0, detail: '', name: 'func0', parentName: 'p0', property: [] }, 99 { depth: 1, detail: '', name: 'func1', parentName: 'func0', property: [] }, 100 ]; 101 let segmentationChart = new SpUserFileChart(htmlElement); 102 expect(segmentationChart.getFlattenTreeData(treeData, 0, 'p0')).toStrictEqual(returnValue); 103 }); 104 it('SpUserFileChart Test05 ', function () { 105 let segmentationChart = new SpUserFileChart(htmlElement); 106 expect(segmentationChart.resetChartData(traceRow)).toBeUndefined(); 107 }); 108}); 109