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 { LitTable } from '../../../src/base-ui/table/lit-table';
17import { LitTableColumn } from '../../../src/base-ui/table/lit-table-column';
18import { TableRowObject } from '../../../src/base-ui/table/TableRowObject';
19import { LitProgressBar } from '../../../src/base-ui/progress-bar/LitProgressBar';
20import { LitIcon } from '../../../src/base-ui/icon/LitIcon';
21describe('LitTable Test', () => {
22  window.ResizeObserver =
23    window.ResizeObserver ||
24    jest.fn().mockImplementation(() => ({
25      disconnect: jest.fn(),
26      observe: jest.fn(),
27      unobserve: jest.fn(),
28    }));
29  let litTable = new LitTable();
30  litTable.selectable = true;
31  litTable.selectable = false;
32  litTable.scrollY = 'scrollY';
33
34  litTable.dataSource = [];
35
36  litTable.dataSource = [
37    {
38      id: 1,
39      name: 'name',
40    },
41    {
42      id: 2,
43      name: 'nameValue',
44    },
45  ];
46  const td = {
47    style: {
48      position: 'sticky',
49      left: '0px',
50      right: '0px',
51      boxShadow: '3px 0px 5px #33333333',
52    },
53  };
54  const placement = 'left';
55
56  const element = {
57    style: {
58      display: 'none',
59      transform: 'translateY',
60    },
61    childNodes: { forEach: true },
62    onclick: 1,
63  };
64  const rowObject = {
65    children: {
66      length: 1,
67    },
68    data: [{ isSelected: undefined }],
69    depth: 1,
70    top: 1,
71  };
72  const firstElement =
73    {
74      style: {
75        display: 'none',
76        paddingLeft: '',
77        transform: 'translateY',
78      },
79      innerHTML: '',
80      title: '',
81      firstChild: null,
82      onclick: 1,
83    } || undefined;
84
85  litTable.columns = litTable.columns || jest.fn(() => true);
86
87  litTable.tbodyElement = jest.fn(() => ({
88    innerHTML: '',
89  }));
90
91  litTable.tableColumns = jest.fn(() => []);
92
93  litTable.tableColumns.forEach = jest.fn(() => []);
94
95  it('LitTableTest01', () => {
96    expect(litTable.adoptedCallback()).toBeUndefined();
97  });
98
99  it('LitTableTest02', () => {
100    litTable.ds = [
101      {
102        name: 'StartTime',
103        value: '1s 489ms 371μs ',
104      },
105      {
106        name: 'Duration',
107        value: '6ms 440μs ',
108      },
109      {
110        name: 'State',
111        value: 'Sleeping',
112      },
113      {
114        name: 'Process',
115        value: 'hilogd [441] ',
116      },
117    ];
118    litTable.setAttribute('selectable', '123');
119    let tableColmn = document.createElement('lit-table-column') as LitTableColumn;
120    tableColmn.setAttribute('title', '621');
121    tableColmn.setAttribute('data-index', '16');
122    tableColmn.setAttribute('key', '261');
123    tableColmn.setAttribute('align', 'flex-start');
124    tableColmn.setAttribute('height', '202px');
125    let tableColmn1 = document.createElement('lit-table-column') as LitTableColumn;
126    tableColmn1.setAttribute('title', '12');
127    tableColmn1.setAttribute('data-index', '12');
128    tableColmn1.setAttribute('key', '67');
129    tableColmn1.setAttribute('align', 'flex-start');
130    tableColmn1.setAttribute('height', '120px');
131    let tableColmn2 = document.createElement('lit-table-column') as LitTableColumn;
132    tableColmn2.setAttribute('title', '13');
133    tableColmn2.setAttribute('data-index', '13');
134    tableColmn2.setAttribute('key', '163');
135    tableColmn2.setAttribute('align', 'flex-start');
136    tableColmn2.setAttribute('height', '4px');
137    litTable.tableColumns = [tableColmn, tableColmn1, tableColmn2];
138    litTable.tbodyElement = document.createElement('div');
139    expect(litTable.renderTable()).toBeUndefined();
140  });
141
142  it('LitTableTest03', () => {
143    litTable.switch = document.querySelector('#switch') as HTMLInputElement;
144    expect(litTable.connectedCallback()).toBeUndefined();
145  });
146
147  it('LitTableTest04', () => {
148    let rowLength = litTable.getCheckRows().length == 0;
149    expect(rowLength).toBeTruthy();
150  });
151
152  it('LitTableTest05', () => {
153    expect(
154      litTable.deleteRowsCondition(() => {
155        return true;
156      })
157    ).toBeUndefined();
158  });
159
160  it('LitTableTest06', () => {
161    expect(litTable.selectable).not.toBeUndefined();
162  });
163
164  it('LitTableTest07', () => {
165    litTable.selectable = true;
166    expect(litTable.selectable).toBeTruthy();
167  });
168
169  it('LitTableTest08', () => {
170    expect(litTable.scrollY).not.toBeUndefined();
171  });
172
173  it('LitTableTest09', () => {
174    expect(litTable.dataSource).not.toBeUndefined();
175  });
176
177  it('LitTableTest10', () => {
178    expect(litTable.recycleDataSource).not.toBeUndefined();
179  });
180
181  it('LitTableTest11', () => {
182    expect(litTable.meauseElementHeight()).toBe(27);
183  });
184
185  it('LitTableTest12', () => {
186    expect(litTable.meauseTreeElementHeight()).toBe(27);
187  });
188
189  it('LitTableTest13', () => {
190    document.body.innerHTML = "<lit-table id='tab' tree></lit-table>";
191    let table = document.querySelector('#tab') as LitTable;
192    let htmlElement = document.createElement('lit-table-column') as LitTableColumn;
193    htmlElement.setAttribute('title', '1');
194    htmlElement.setAttribute('data-index', '1');
195    htmlElement.setAttribute('key', '1');
196    htmlElement.setAttribute('align', 'flex-start');
197    htmlElement.setAttribute('height', '32px');
198    table!.appendChild(htmlElement);
199    setTimeout(() => {
200      table.recycleDataSource = [
201        {
202          id: 1,
203          name: 'name',
204        },
205        {
206          id: 2,
207          name: 'nameValue',
208        },
209      ];
210      expect(table.meauseTreeElementHeight()).toBe(27);
211    }, 20);
212  });
213
214  it('LitTableTest14', () => {
215    expect(litTable.createExpandBtn({ expanded: false, data: { status: true } })).not.toBeUndefined();
216  });
217
218  it('LitTableTest15', () => {
219    let newTableElement = document.createElement('div');
220    newTableElement.classList.add('tr');
221    newTableElement.style.cursor = 'pointer';
222    newTableElement.style.gridTemplateColumns = '1,2,3';
223    newTableElement.style.position = 'absolute';
224    newTableElement.style.top = '0px';
225    newTableElement.style.left = '0px';
226    litTable.currentRecycleList = [newTableElement];
227    litTable.recycleDs = [{ rowHidden: false, data: { isSearch: true } }];
228    litTable.tbodyElement = document.createElement('div');
229    litTable.treeElement = document.createElement('div');
230    litTable.tableElement = document.createElement('div');
231    litTable.theadElement = document.createElement('div');
232    expect(litTable.reMeauseHeight()).toBeUndefined();
233  });
234
235  it('LitTableTest15', () => {
236    const rowData = {
237      data: [
238        {
239          isSelected: undefined,
240        },
241      ],
242    };
243    litTable.columns.forEach = jest.fn(() => true);
244    expect(litTable.createNewTableElement(rowData)).not.toBeUndefined();
245  });
246
247  it('LitTableTest16', () => {
248    let element = document.createElement('div');
249    let ch = document.createElement('div');
250    element.appendChild(ch);
251    let rowObject = { rowHidden: false, data: { isSearch: true } };
252    let tableColmn = document.createElement('lit-table-column') as LitTableColumn;
253    tableColmn.setAttribute('data-index', '1');
254    tableColmn.setAttribute('title', '1');
255    tableColmn.setAttribute('data-index', '2');
256    tableColmn.setAttribute('align', 'flex-start');
257    tableColmn.setAttribute('height', '32px');
258    tableColmn.setAttribute('key', '2');
259    let tableColmn1 = document.createElement('lit-table-column') as LitTableColumn;
260    tableColmn1.setAttribute('align', 'flex-start');
261    tableColmn1.setAttribute('height', '32px');
262    tableColmn1.setAttribute('title', '2');
263    tableColmn1.setAttribute('data-index', '2');
264    tableColmn1.setAttribute('key', '2');
265    litTable.columns = [tableColmn, tableColmn1];
266    expect(litTable.freshCurrentLine(element, rowObject)).toBeUndefined();
267  });
268
269  it('LitTableTest16', () => {
270    litTable.recycleDs.length = 1;
271    litTable.setCurrentSelection = jest.fn(() => true);
272    expect(litTable.scrollToData()).toBeUndefined();
273  });
274
275  it('LitTableTest17', () => {
276    litTable.recycleDs = [{ rowHidden: false, data: { isSearch: true } }];
277    let dataSource = [
278      {
279        id: 11,
280        name: 'name',
281      },
282      {
283        id: 21,
284        name: 'value',
285      },
286    ];
287    expect(litTable.expandList(dataSource)).toBeUndefined();
288  });
289
290  it('LitTableTest18', () => {
291    expect(litTable.clearAllSelection()).toBeUndefined();
292  });
293
294  it('LitTableTest19', () => {
295    const mockEvent = new MouseEvent('click', { button: 0 });
296    expect(() => litTable.dispatchRowClickEvent({ data: { isSelected: '' } }, [], mockEvent)).not.toThrow();
297  });
298
299  it('LitTableTest20', () => {
300    litTable.treeElement = jest.fn(() => undefined);
301    litTable.treeElement.children = jest.fn(() => [1]);
302    litTable.columns.forEach = jest.fn(() => true);
303    litTable.treeElement.lastChild = jest.fn(() => true);
304    litTable.treeElement.lastChild.style = jest.fn(() => true);
305    expect(litTable.createNewTreeTableElement({ data: '' })).not.toBeUndefined();
306  });
307
308  it('LitTableTest21', () => {
309    litTable.tableElement = jest.fn(() => undefined);
310    litTable.tableElement.scrollTop = jest.fn(() => 1);
311    expect(litTable.move1px()).toBeUndefined();
312  });
313
314  it('LitTableTest22', () => {
315    document.body.innerHTML = `<lit-table id="aaa"></lit-table>`;
316    let litTable = document.querySelector('#aaa') as LitTable;
317    expect(litTable.setMouseIn(true, [])).toBeUndefined();
318  });
319
320  it('LitTableTest23', () => {
321    let tableIcon = document.createElement('lit-icon') as LitIcon;
322    let mouseClickEvent: MouseEvent = new MouseEvent('click', <MouseEventInit>{ movementX: 1, movementY: 2 });
323    tableIcon.dispatchEvent(mouseClickEvent);
324  });
325
326  it('LitTableTest24', () => {
327    document.body.innerHTML = `<lit-table id="aaa"></lit-table>`;
328    let litTable = document.querySelector('#aaa') as LitTable;
329    const data = {
330      isSelected: true,
331    };
332    expect(litTable.setCurrentSelection(data)).toBeUndefined();
333  });
334
335  it('LitTableTest25', () => {
336    document.body.innerHTML = `<lit-table id="aaa"></lit-table>`;
337    let litTable = document.querySelector('#aaa') as LitTable;
338    litTable.formatName = true;
339    expect(litTable.formatName).toBeTruthy();
340  });
341
342  it('LitTableTest26', () => {
343    let litTable = new LitTable();
344    expect(litTable.dataExportInit()).toBeUndefined();
345  });
346  it('LitTableTest27', () => {
347    let litTable = new LitTable();
348    let htmlElement = document.createElement('lit-table-column') as LitTableColumn;
349    htmlElement.setAttribute('title', '41');
350    htmlElement.setAttribute('data-index', '1');
351    htmlElement.setAttribute('key', '14');
352    htmlElement.setAttribute('align', 'flex-start');
353    htmlElement.setAttribute('height', '34px');
354    litTable.columns = [htmlElement];
355    document.body.innerHTML = `<lit-table id="aaa"> <lit-progress-bar id="export_progress_bar" class="progress"></lit-progress-bar></lit-table>`;
356    let progressBar = document.querySelector('#export_progress_bar') as LitProgressBar;
357    litTable.exportProgress = progressBar;
358    expect(litTable.exportData()).toBeUndefined();
359  });
360
361  it('LitTableTest28', () => {
362    expect(litTable.formatExportData()).not.toBeUndefined();
363  });
364
365  it('LitTableTest29', () => {
366    expect(litTable.setSelectedRow(true, [])).toBeUndefined();
367  });
368
369  it('LitTableTest30', () => {
370    document.body.innerHTML = `<lit-table id="aaa"></lit-table>`;
371    let litTable = document.querySelector('#aaa') as LitTable;
372    litTable.setAttribute('tree', true);
373    expect(litTable.dataSource).toStrictEqual([]);
374  });
375
376  it('LitTableTest31', () => {
377    document.body.innerHTML = `<lit-table id="aaa"></lit-table>`;
378    let litTable = document.querySelector('#aaa') as LitTable;
379    litTable.rememberScrollTop = true;
380    expect(litTable.recycleDataSource).toStrictEqual([]);
381  });
382
383  it('LitTableTest32', () => {
384    let litTable = new LitTable();
385    expect(litTable.dataExportInit()).toBeUndefined();
386  });
387
388  it('LitTableTest33', () => {
389    let tableColmn = document.createElement('lit-table-column') as LitTableColumn;
390    tableColmn.setAttribute('title', '21');
391    tableColmn.setAttribute('data-index', '13');
392    tableColmn.setAttribute('key', '4');
393    tableColmn.setAttribute('align', 'flex-start');
394    tableColmn.setAttribute('height', '32px');
395    let tableColmn1 = document.createElement('lit-table-column') as LitTableColumn;
396    tableColmn1.setAttribute('title', '52');
397    tableColmn1.setAttribute('data-index', '244');
398    tableColmn1.setAttribute('key', '25');
399    tableColmn1.setAttribute('align', 'flex-start');
400    tableColmn1.setAttribute('height', '24px');
401
402    let tableColmn2 = document.createElement('lit-table-column') as LitTableColumn;
403    tableColmn2.setAttribute('title', '53');
404    tableColmn2.setAttribute('data-index', '35');
405    tableColmn2.setAttribute('key', '35');
406    tableColmn2.setAttribute('align', 'flex-start');
407    tableColmn2.setAttribute('height', '325px');
408    litTable.columns = [tableColmn, tableColmn1, tableColmn2];
409    let dataSource = [
410      {
411        id: 22,
412        name: 'name',
413      },
414      {
415        id: 12,
416        name: 'nameValue',
417      },
418    ];
419    expect(litTable.formatExportData(dataSource)).toBeTruthy();
420  });
421
422  it('LitTableTest34', () => {
423    let list = [
424      {
425        memoryTap: 'All Heap',
426        existing: 1481,
427        existingString: '44.89 Kb',
428        freeByteString: '42.54 Kb',
429        allocCount: 461,
430        freeCount: 103,
431        freeByte: 43451,
432        totalBytes: 641,
433        totalBytesString: '4.44 Kb',
434        maxStr: '275 byte',
435        max: 264,
436        totalCount: 149,
437        existingValue: [1948, 411, 51820],
438      },
439    ];
440    LitTable.createNewTreeTableElement = jest.fn().mockResolvedValue({});
441    litTable.treeElement = document.createElement('div');
442    litTable.tableElement = document.createElement('div');
443    litTable.setAttribute('selectable', '123');
444    litTable.setAttribute('tree', '');
445    litTable.recycleDataSource = [
446      {
447        id: 1,
448        name: 'name',
449      },
450      {
451        id: 2,
452        name: 'nameValue',
453      },
454    ];
455    let tableColmn = document.createElement('lit-table-column') as LitTableColumn;
456    tableColmn.setAttribute('title', '6');
457    tableColmn.setAttribute('data-index', '22');
458    tableColmn.setAttribute('key', '29');
459    tableColmn.setAttribute('align', 'flex-start');
460    tableColmn.setAttribute('height', '42px');
461    let tableColmn1 = document.createElement('lit-table-column') as LitTableColumn;
462    tableColmn1.setAttribute('title', '125');
463    tableColmn1.setAttribute('data-index', '22');
464    tableColmn1.setAttribute('key', '12');
465    tableColmn1.setAttribute('align', 'flex-start');
466    tableColmn1.setAttribute('height', '121px');
467    let tableColmn2 = document.createElement('lit-table-column') as LitTableColumn;
468    tableColmn2.setAttribute('title', '31');
469    tableColmn2.setAttribute('data-index', '13');
470    tableColmn2.setAttribute('key', '31');
471    tableColmn2.setAttribute('align', 'flex-start');
472    tableColmn2.setAttribute('height', '12px');
473    litTable.columns = [tableColmn, tableColmn1, tableColmn2];
474    litTable.tbodyElement = document.createElement('div');
475    litTable.theadElement = document.createElement('div');
476    expect(litTable.meauseTreeRowElement(list)).toBeTruthy();
477  });
478
479  it('LitTableTest35', () => {
480    let list = [
481      {
482        memoryTap: 'All Heap',
483        existing: 43482,
484        existingString: '6.89 Kb',
485        freeByteString: '76.54 Kb',
486        allocCount: 462,
487        freeCount: 103,
488        freeByte: 456,
489        totalBytes: 622,
490        totalBytesString: '6.44 Kb',
491        maxStr: '200 byte',
492        max: 222,
493        totalCount: 12,
494        existingValue: [1348, 6662, 2220],
495      },
496    ];
497    LitTable.createNewTreeTableElement = jest.fn().mockResolvedValue({});
498    litTable.treeElement = document.createElement('div');
499    litTable.tableElement = document.createElement('div');
500    litTable.setAttribute('selectable', '123');
501    let tableColmn = document.createElement('lit-table-column') as LitTableColumn;
502    tableColmn.setAttribute('title', '103');
503    tableColmn.setAttribute('data-index', '13');
504    tableColmn.setAttribute('key', '10');
505    tableColmn.setAttribute('align', 'flex-start');
506    tableColmn.setAttribute('height', '32px');
507    let tableColmn1 = document.createElement('lit-table-column') as LitTableColumn;
508    tableColmn1.setAttribute('align', 'flex-start');
509    tableColmn1.setAttribute('height', '32px');
510    tableColmn1.setAttribute('title', '2');
511    tableColmn1.setAttribute('data-index', '23');
512    tableColmn1.setAttribute('key', '20');
513    let tableColmn2 = document.createElement('lit-table-column') as LitTableColumn;
514    tableColmn2.setAttribute('title', '31');
515    tableColmn2.setAttribute('key', '30');
516    tableColmn2.setAttribute('align', 'flex-start');
517    tableColmn2.setAttribute('data-index', '3');
518    tableColmn2.setAttribute('height', '32px');
519    litTable.columns = [tableColmn, tableColmn1, tableColmn2];
520    litTable.theadElement = document.createElement('div');
521    litTable.tbodyElement = document.createElement('div');
522    expect(litTable.meauseAllRowHeight(list)).toBeTruthy();
523  });
524
525  it('LitTableTest36', () => {
526    let tableColmn = document.createElement('lit-table-column') as LitTableColumn;
527    tableColmn.setAttribute('data-index', '14');
528    tableColmn.setAttribute('key', '141');
529    tableColmn.setAttribute('align', 'flex-start');
530    tableColmn.setAttribute('height', '32px');
531    tableColmn.setAttribute('title', '114');
532    let tableColmn1 = document.createElement('lit-table-column') as LitTableColumn;
533    tableColmn1.setAttribute('key', '214');
534    tableColmn1.setAttribute('align', 'flex-start');
535    tableColmn1.setAttribute('title', '24');
536    tableColmn1.setAttribute('data-index', '24');
537    tableColmn1.setAttribute('height', '32px');
538    let tableColmn2 = document.createElement('lit-table-column') as LitTableColumn;
539    tableColmn2.setAttribute('title', '34');
540    tableColmn2.setAttribute('key', '314');
541    tableColmn2.setAttribute('align', 'flex-start');
542    tableColmn2.setAttribute('height', '32px');
543    tableColmn2.setAttribute('data-index', '34');
544    litTable.columns = [tableColmn, tableColmn1, tableColmn2];
545    let dataSource = [
546      {
547        id: 13,
548        name: 'name',
549      },
550      {
551        id: 23,
552        name: 'nameValue',
553      },
554    ];
555    expect(litTable.formatExportCsvData(dataSource)).toBeTruthy();
556  });
557
558  it('LitTableTest37', () => {
559    let element = document.createElement('div');
560    litTable.tableElement = document.createElement('div');
561    let firstElement = document.createElement('div');
562    let ch = document.createElement('div');
563    element.appendChild(ch);
564    let rowObject = { rowHidden: false, data: { isSearch: true } };
565    let tableColmn = document.createElement('lit-table-column') as LitTableColumn;
566    tableColmn.setAttribute('height', '32px');
567    tableColmn.setAttribute('title', '16');
568    tableColmn.setAttribute('data-index', '1');
569    tableColmn.setAttribute('align', 'flex-start');
570    tableColmn.setAttribute('height', '36px');
571    tableColmn.setAttribute('key', '1');
572    let tableColmn1 = document.createElement('lit-table-column') as LitTableColumn;
573    tableColmn1.setAttribute('key', '2');
574    tableColmn1.setAttribute('align', 'flex-start');
575    tableColmn1.setAttribute('height', '32px');
576    tableColmn1.setAttribute('title', '2');
577    tableColmn1.setAttribute('data-index', '2');
578    litTable.columns = [tableColmn, tableColmn1];
579    expect(litTable.freshCurrentLine(element, rowObject, firstElement)).toBeUndefined();
580  });
581  it('LitTableTest38', () => {
582    litTable.hideDownload = true;
583    expect(litTable.hideDownload).toBeTruthy();
584  });
585  it('LitTableTest39', () => {
586    litTable.hideDownload = false;
587    expect(litTable.hideDownload).not.toBeUndefined();
588  });
589  it('LitTableTest40', () => {
590    expect(litTable.createBtn({ expanded: false, data: { status: true } })).not.toBeUndefined();
591  });
592  it('LitTableTest41', () => {
593    expect(litTable.mouseOut()).toBeUndefined();
594  });
595  it('LitTableTest42', () => {
596    litTable.isRecycleList = true;
597    expect(litTable.setCurrentHover({})).toBeUndefined();
598  });
599  it('LitTableTest43', () => {
600    expect(litTable.clearAllHover({})).toBeUndefined();
601  });
602});
603