1fb726d48Sopenharmony_ci/* 2fb726d48Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd. 3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb726d48Sopenharmony_ci * You may obtain a copy of the License at 6fb726d48Sopenharmony_ci * 7fb726d48Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb726d48Sopenharmony_ci * 9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and 13fb726d48Sopenharmony_ci * limitations under the License. 14fb726d48Sopenharmony_ci */ 15fb726d48Sopenharmony_ci 16fb726d48Sopenharmony_ciimport { ExcelFormater } from '../../../src/base-ui/utils/ExcelFormater'; 17fb726d48Sopenharmony_ci 18fb726d48Sopenharmony_cidescribe('ExcelFormater', () => { 19fb726d48Sopenharmony_ci it('ExcelFormaterTest01', function () { 20fb726d48Sopenharmony_ci const s = 'Hello, {name}!'; 21fb726d48Sopenharmony_ci const c = { name: 'Alice' }; 22fb726d48Sopenharmony_ci const result = ExcelFormater.format(s, c); 23fb726d48Sopenharmony_ci expect(result).toBe('Hello, Alice!'); 24fb726d48Sopenharmony_ci }); 25fb726d48Sopenharmony_ci it('ExcelFormaterTest02 ', function () { 26fb726d48Sopenharmony_ci const columns = [ 27fb726d48Sopenharmony_ci { getAttribute: (attr: string) => (attr === 'data-index' ? 'name' : 'Name') }, 28fb726d48Sopenharmony_ci { getAttribute: (attr: string) => (attr === 'data-index' ? 'age' : 'Age') }, 29fb726d48Sopenharmony_ci ]; 30fb726d48Sopenharmony_ci const data = { name: 'Alice', age: 30 }; 31fb726d48Sopenharmony_ci const result = ExcelFormater.createExcelRow(columns, data); 32fb726d48Sopenharmony_ci expect(result).toContain('<Row>'); 33fb726d48Sopenharmony_ci expect(result).toContain('<Cell><Data ss:Type="String">Alice</Data></Cell>'); 34fb726d48Sopenharmony_ci expect(result).toContain('<Cell><Data ss:Type="String">30</Data></Cell>'); 35fb726d48Sopenharmony_ci }); 36fb726d48Sopenharmony_ci it('ExcelFormaterTest03 ', function () { 37fb726d48Sopenharmony_ci const columns = [ 38fb726d48Sopenharmony_ci { getAttribute: (attr: string) => (attr === 'data-index' ? 'parent' : 'Parent') }, 39fb726d48Sopenharmony_ci ]; 40fb726d48Sopenharmony_ci const data = { 41fb726d48Sopenharmony_ci parent: 'Parent 1', 42fb726d48Sopenharmony_ci children: [ 43fb726d48Sopenharmony_ci { child: 'Child 1' }, 44fb726d48Sopenharmony_ci { child: 'Child 2' }, 45fb726d48Sopenharmony_ci ], 46fb726d48Sopenharmony_ci }; 47fb726d48Sopenharmony_ci const result = ExcelFormater.createExcelRow(columns, data); 48fb726d48Sopenharmony_ci expect(result).toContain('<Row>'); 49fb726d48Sopenharmony_ci expect(result).toContain('<Cell><Data ss:Type="String">Parent 1</Data></Cell>'); 50fb726d48Sopenharmony_ci 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>'); 51fb726d48Sopenharmony_ci }); 52fb726d48Sopenharmony_ci it('ExcelFormaterTest04 ', function () { 53fb726d48Sopenharmony_ci const baseStr = 'path/to/image.jpg'; 54fb726d48Sopenharmony_ci const result = ExcelFormater.addImage(baseStr); 55fb726d48Sopenharmony_ci expect(result).toContain('<Row>'); 56fb726d48Sopenharmony_ci expect(result).toContain(`<div><img src="${baseStr}"></img></div>`); 57fb726d48Sopenharmony_ci }); 58fb726d48Sopenharmony_ci it('ExcelFormaterTest05 ', function () { 59fb726d48Sopenharmony_ci const columns = [ 60fb726d48Sopenharmony_ci { getAttribute: (attr: string) => (attr === 'data-index' ? 'name' : 'Name'), title: 'Name' }, 61fb726d48Sopenharmony_ci { getAttribute: (attr: string) => (attr === 'data-index' ? 'age' : 'Age'), title: 'Age' }, 62fb726d48Sopenharmony_ci ]; 63fb726d48Sopenharmony_ci const dataSource = [ 64fb726d48Sopenharmony_ci { name: 'Alice', age: 30 }, 65fb726d48Sopenharmony_ci { name: 'Bob', age: 40 }, 66fb726d48Sopenharmony_ci ]; 67fb726d48Sopenharmony_ci const result = ExcelFormater.createTableData(columns, dataSource); 68fb726d48Sopenharmony_ci expect(result).toContain('<thead>'); 69fb726d48Sopenharmony_ci expect(result).toContain('<td>Name</td>'); 70fb726d48Sopenharmony_ci expect(result).toContain('<td>Age</td>'); 71fb726d48Sopenharmony_ci expect(result).toContain('<tr>'); 72fb726d48Sopenharmony_ci expect(result).toContain('<td>Alice</td>'); 73fb726d48Sopenharmony_ci expect(result).toContain('<td>30</td>'); 74fb726d48Sopenharmony_ci expect(result).toContain('<tr>'); 75fb726d48Sopenharmony_ci expect(result).toContain('<td>Bob</td>'); 76fb726d48Sopenharmony_ci expect(result).toContain('<td>40</td>'); 77fb726d48Sopenharmony_ci }); 78fb726d48Sopenharmony_ci it('ExcelFormaterTest06 ', function () { 79fb726d48Sopenharmony_ci const columns = ['Name', 'Age']; 80fb726d48Sopenharmony_ci const result = ExcelFormater.createTHead(columns); 81fb726d48Sopenharmony_ci expect(result).toContain('<thead>'); 82fb726d48Sopenharmony_ci expect(result).toContain('<td>Name</td>'); 83fb726d48Sopenharmony_ci expect(result).toContain('<td>Age</td>'); 84fb726d48Sopenharmony_ci }); 85fb726d48Sopenharmony_ci it('ExcelFormaterTest07 ', function () { 86fb726d48Sopenharmony_ci const columns = ['Name', 'Age']; 87fb726d48Sopenharmony_ci const data = { name: 'Alice', age: 30 }; 88fb726d48Sopenharmony_ci const result = ExcelFormater.createTableRow(columns, data); 89fb726d48Sopenharmony_ci expect(result).toContain('<tr>'); 90fb726d48Sopenharmony_ci expect(result).toContain('<tr><td>undefined</td><td>undefined</td></tr>'); 91fb726d48Sopenharmony_ci expect(result).toContain('<tr><td>undefined</td><td>undefined</td></tr>'); 92fb726d48Sopenharmony_ci }); 93fb726d48Sopenharmony_ci});