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 { ExcelFormater } from '../../../src/base-ui/utils/ExcelFormater'; 17 18describe('ExcelFormater', () => { 19 it('ExcelFormaterTest01', function () { 20 const s = 'Hello, {name}!'; 21 const c = { name: 'Alice' }; 22 const result = ExcelFormater.format(s, c); 23 expect(result).toBe('Hello, Alice!'); 24 }); 25 it('ExcelFormaterTest02 ', function () { 26 const columns = [ 27 { getAttribute: (attr: string) => (attr === 'data-index' ? 'name' : 'Name') }, 28 { getAttribute: (attr: string) => (attr === 'data-index' ? 'age' : 'Age') }, 29 ]; 30 const data = { name: 'Alice', age: 30 }; 31 const result = ExcelFormater.createExcelRow(columns, data); 32 expect(result).toContain('<Row>'); 33 expect(result).toContain('<Cell><Data ss:Type="String">Alice</Data></Cell>'); 34 expect(result).toContain('<Cell><Data ss:Type="String">30</Data></Cell>'); 35 }); 36 it('ExcelFormaterTest03 ', function () { 37 const columns = [ 38 { getAttribute: (attr: string) => (attr === 'data-index' ? 'parent' : 'Parent') }, 39 ]; 40 const data = { 41 parent: 'Parent 1', 42 children: [ 43 { child: 'Child 1' }, 44 { child: 'Child 2' }, 45 ], 46 }; 47 const result = ExcelFormater.createExcelRow(columns, data); 48 expect(result).toContain('<Row>'); 49 expect(result).toContain('<Cell><Data ss:Type="String">Parent 1</Data></Cell>'); 50 expect(result).toContain('<Row><Cell><Data ss:Type=\"String\">Parent 1</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\"></Data></Cell></Row><Row><Cell><Data ss:Type=\"String\"></Data></Cell></Row>'); 51 }); 52 it('ExcelFormaterTest04 ', function () { 53 const baseStr = 'path/to/image.jpg'; 54 const result = ExcelFormater.addImage(baseStr); 55 expect(result).toContain('<Row>'); 56 expect(result).toContain(`<div><img src="${baseStr}"></img></div>`); 57 }); 58 it('ExcelFormaterTest05 ', function () { 59 const columns = [ 60 { getAttribute: (attr: string) => (attr === 'data-index' ? 'name' : 'Name'), title: 'Name' }, 61 { getAttribute: (attr: string) => (attr === 'data-index' ? 'age' : 'Age'), title: 'Age' }, 62 ]; 63 const dataSource = [ 64 { name: 'Alice', age: 30 }, 65 { name: 'Bob', age: 40 }, 66 ]; 67 const result = ExcelFormater.createTableData(columns, dataSource); 68 expect(result).toContain('<thead>'); 69 expect(result).toContain('<td>Name</td>'); 70 expect(result).toContain('<td>Age</td>'); 71 expect(result).toContain('<tr>'); 72 expect(result).toContain('<td>Alice</td>'); 73 expect(result).toContain('<td>30</td>'); 74 expect(result).toContain('<tr>'); 75 expect(result).toContain('<td>Bob</td>'); 76 expect(result).toContain('<td>40</td>'); 77 }); 78 it('ExcelFormaterTest06 ', function () { 79 const columns = ['Name', 'Age']; 80 const result = ExcelFormater.createTHead(columns); 81 expect(result).toContain('<thead>'); 82 expect(result).toContain('<td>Name</td>'); 83 expect(result).toContain('<td>Age</td>'); 84 }); 85 it('ExcelFormaterTest07 ', function () { 86 const columns = ['Name', 'Age']; 87 const data = { name: 'Alice', age: 30 }; 88 const result = ExcelFormater.createTableRow(columns, data); 89 expect(result).toContain('<tr>'); 90 expect(result).toContain('<tr><td>undefined</td><td>undefined</td></tr>'); 91 expect(result).toContain('<tr><td>undefined</td><td>undefined</td></tr>'); 92 }); 93});