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 { LitChartScatter } from '../../../../src/base-ui/chart/scatter/LitChartScatter';
17import { LitChartScatterConfig } from '../../../../src/base-ui/chart/scatter/LitChartScatterConfig';
18
19// @ts-ignore
20window.ResizeObserver =
21  window.ResizeObserver ||
22  jest.fn().mockImplementation(() => ({
23    disconnect: jest.fn(),
24    observe: jest.fn(),
25    unobserve: jest.fn(),
26  }));
27
28describe('LitChartScatter Test', () => {
29  let litChartScatter = new LitChartScatter();
30  litChartScatter.canvas = litChartScatter.shadowRoot!.querySelector<HTMLCanvasElement>('#canvas');
31  litChartScatter.ctx = litChartScatter.canvas!.getContext('2d', { alpha: true });
32  litChartScatter.connectedCallback();
33  litChartScatter.options = {
34    yAxisLabel: [20000, 40000, 60000, 80000, 100000],
35    xAxisLabel: [20000, 40000, 60000, 80000, 100000, 120000],
36    axisLabel: ['负载', '算力供给'],
37    drawload: true,
38    load: [100000, 1000],
39    colorPool: () => ['#2f72f8', '#ffab67', '#a285d2'],
40    colorPoolText: () => ['Total', 'CycleA', 'CycleB'],
41    paintingData: [
42      {
43        x: 111.11939333333333,
44        y: 173.71615585811537,
45        r: 6,
46        c: [16655.818, 2983.844141884629, 1, 5.582, 5.361],
47        color: '#2f72f8',
48      },
49      {
50        x: 77.52432666666667,
51        y: 174.7453794947994,
52        r: 6,
53        c: [6577.298, 1954.6205052005942, 2, 3.365, 2.585],
54        color: '#2f72f8',
55      },
56      {
57        x: 100.43357333333333,
58        y: 174.20508773882395,
59        r: 6,
60        c: [13450.072, 2494.912261176034, 3, 5.391, 5.287],
61        color: '#2f72f8',
62      },
63    ],
64    hoverData: {},
65    globalGradient: {},
66    data: [
67      [
68        [16655.818, 2983.844141884629, 1, 5.582, 5.361],
69        [6577.298, 1954.6205052005942, 2, 3.365, 2.585],
70        [13450.072, 2494.912261176034, 3, 5.391, 5.287],
71      ],
72      [],
73      [],
74    ],
75    title: 'render_service 1155',
76    tip: (data) => {
77      return `
78      <div>
79          <span>Cycle: ${data.c[2]};</span></br>
80          <span>Comsumption: ${data.c[0]};</span></br>
81          <span>Cycle_dur: ${data.c[3]} ms;</span></br>
82          <span>Running_dur: ${data.c[4]} ms;</span></br>
83      </div>
84      `;
85    },
86  };
87  let options = litChartScatter.options!;
88  it('LitChartScatter 01', function () {
89    expect(litChartScatter.init()).toBeUndefined();
90  });
91
92  it('LitChartScatter 02', function () {
93    const drawBackgroundMock = jest.fn();
94    litChartScatter.drawBackground = drawBackgroundMock;
95    const drawScatterChartMock = jest.fn();
96    litChartScatter.drawScatterChart = drawScatterChartMock;
97    const setOffScreenMock = jest.fn();
98    litChartScatter.setOffScreen = setOffScreenMock;
99    const clearRectMock = jest.fn();
100    litChartScatter.ctx!.clearRect = clearRectMock;
101    litChartScatter.init();
102    expect(drawBackgroundMock).toHaveBeenCalled();
103    expect(drawScatterChartMock).toHaveBeenCalled();
104    expect(setOffScreenMock).toHaveBeenCalled();
105    expect(clearRectMock).toHaveBeenCalled();
106  });
107
108  it('LitChartScatter 03', function () {
109    expect(litChartScatter.setOffScreen()).toBeUndefined();
110    litChartScatter.setOffScreen();
111    expect(litChartScatter.canvas2?.height).toEqual(litChartScatter.clientHeight);
112    expect(litChartScatter.canvas2?.width).toEqual(litChartScatter.clientWidth);
113  });
114
115  it('LitChartScatter 04', function () {
116    let hoverPoint = {
117      x: 163.42456666666666,
118      y: 160.69372623574142,
119      r: 6,
120      c: [3234.737, 559.0627376425856, 95, 5.786, 3.916],
121      color: '#2f72f8',
122    };
123    expect(litChartScatter.drawBackground()).toBeUndefined();
124    expect(litChartScatter.drawScatterChart(litChartScatter.options!)).toBeUndefined();
125    expect(litChartScatter.drawAxis(litChartScatter.options!)).toBeUndefined();
126    expect(litChartScatter.drawYLabels(litChartScatter.options!)).toBeUndefined();
127    expect(litChartScatter.drawXLabels(litChartScatter.options!)).toBeUndefined();
128    expect(litChartScatter.drawData(litChartScatter.options!)).toBeUndefined();
129    expect(litChartScatter.drawCycle(2, 10, 1, 1, '#000000')).toBeUndefined();
130    expect(litChartScatter.drawLoadLine(litChartScatter.options!.load)).toBeUndefined();
131    expect(litChartScatter.drawBalanceLine(litChartScatter.options!.load)).toBeUndefined();
132    expect(litChartScatter.resetHoverWithOffScreen()).toBeUndefined();
133    expect(litChartScatter.paintHover()).toBeUndefined();
134    expect(litChartScatter.connectedCallback()).toBeUndefined();
135    expect(litChartScatter.initElements()).toBeUndefined();
136  });
137
138  it('LitChartScatter 05', function () {
139    litChartScatter.drawBackground();
140    expect(litChartScatter.ctx?.save).toHaveBeenCalled();
141    expect(litChartScatter.ctx?.fillRect).toHaveBeenCalled();
142    expect(litChartScatter.ctx?.restore).toHaveBeenCalled();
143  });
144
145  it('LitChartScatter 06', function () {
146    litChartScatter.drawAxis(litChartScatter.options!);
147    let ctx = litChartScatter.ctx!;
148    expect(ctx.font).toEqual('10px KATTI');
149    expect(ctx.fillStyle).toEqual('#000000');
150    expect(ctx.strokeStyle).toEqual('#000000');
151  });
152
153  it('LitChartScatter 07', function () {
154    litChartScatter.drawYLabels(litChartScatter.options!);
155    let ctx = litChartScatter.ctx!;
156    expect(ctx.font).toEqual('12px KATTI');
157    expect(ctx.fillStyle).toEqual('#000000');
158    expect(ctx.strokeStyle).toEqual('#000000');
159  });
160
161  it('LitChartScatter 08', function () {
162    litChartScatter.drawXLabels(litChartScatter.options!);
163    let ctx = litChartScatter.ctx!;
164    expect(ctx.fillStyle).toEqual('#000000');
165    expect(ctx.strokeStyle).toEqual('#000000');
166    expect(ctx.beginPath).toHaveBeenCalled();
167  });
168
169  it('LitChartScatter 9', function () {
170    const mockInit = jest.fn();
171    litChartScatter.init = mockInit;
172    litChartScatter.config = options;
173    expect(mockInit).toHaveBeenCalled();
174    expect(litChartScatter.options).toEqual(options);
175  });
176});
177