1/* 2 * Copyright (c) 2021 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 16"use strict"; 17 18const fs = require("fs"); 19const path = require("path"); 20const sinon = require("sinon"); 21const { expect } = require('chai'); 22 23function getJsBundle(componentName) { 24 const filePath = path.join( 25 __dirname, 26 "testcase/build/pages", 27 `${componentName}`, 28 `${componentName}.js` 29 ); 30 const fileContent = fs.readFileSync(filePath, "utf-8"); 31 const fileString = fileContent.toString(); 32 return fileString; 33} 34 35function getExpectedObj(componentName) { 36 const matchHashComment = /\/\*(.|\n|\r)+\*\//; 37 const filepath = path.join(__dirname, "expected", `${componentName}.js`); 38 const expectedContent = fs.readFileSync(filepath, "utf-8"); 39 const expectedObj = JSON.parse(expectedContent.toString().replace(matchHashComment, '')); 40 return expectedObj; 41} 42 43function getActualObj(data) { 44 const actualString = JSON.stringify(data, function (key, value) { 45 if (typeof value === "function") { 46 value = value.toString(); 47 } 48 return value; 49 }); 50 const actualObj = JSON.parse(actualString); 51 return actualObj; 52} 53 54describe("build", () => { 55 let viewModelOptions; 56 function ViewModel(options) { 57 viewModelOptions = options; 58 } 59 const requireNative = sinon.stub(); 60 function expectActual(componentName) { 61 const actualStr = getJsBundle(componentName); 62 const fn = new Function("ViewModel", "requireNative", actualStr); 63 fn(ViewModel, requireNative); 64 expect(getActualObj(viewModelOptions)).eql( 65 getExpectedObj(componentName) 66 ); 67 } 68 it("attribute test", () => { 69 expectActual("attribute"); 70 }); 71 it("class test", () => { 72 expectActual("class"); 73 }); 74 it("event test", () => { 75 expectActual("event"); 76 }); 77 it("expression test", () => { 78 expectActual("expression"); 79 }); 80 it("exteriorStyle test", () => { 81 expectActual("exteriorStyle"); 82 }); 83 it("for directive", () => { 84 expectActual("forDirective"); 85 }); 86 it("if directive", () => { 87 expectActual("ifDirective"); 88 }); 89 it("importJS test", () => { 90 expectActual("importJS"); 91 }); 92 it("inlineStyle test", () => { 93 expectActual("inlineStyle"); 94 }); 95 it("bubbleEvent test", () => { 96 expectActual("bubble"); 97 }); 98 it("globalization performance optimization music test", () => { 99 expectActual("music"); 100 }); 101 it("globalization performance optimization sick test", () => { 102 expectActual("sick"); 103 }); 104}); 105