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');
20
21const chai = require('chai');
22const sinon = require('sinon');
23const expect = chai.expect;
24
25function getActualString(componentName) {
26  const filePath = path.join(__dirname, 'testcase/build/pages', `${componentName}`, `${componentName}.js`);
27  const fileContent = fs.readFileSync(filePath, 'utf-8');
28  const fileString = fileContent.toString();
29  return fileString;
30}
31
32function getExpectJSON(componentName) {
33  const matchHashComment = /\/\*(.|\n)+\*\//;
34  const filepath = path.join(__dirname, 'expected', `${componentName}.js`);
35  const expectedContent = fs.readFileSync(filepath, 'utf-8').substring(607);
36  const expectedObj = JSON.parse(expectedContent.toString().replace(matchHashComment, ''));
37  return expectedObj;
38}
39
40function stringifyActual(json) {
41  return JSON.stringify(json, function(key, value) {
42    if (typeof value === 'function') {
43      value = value.toString();
44    }
45    return value;
46  }, ' ');
47}
48
49describe('build', () => {
50  let $app_define$;
51  let $app_bootstrap$;
52  let components;
53  let requireStub;
54  let bootstrapStub;
55
56  function expectActual(name) {
57    const actualStr = getActualString(name);
58    const fn = new Function('$app_define$', '$app_bootstrap$', actualStr);
59    fn($app_define$, $app_bootstrap$);
60    const expectJSON = getExpectJSON(name);
61    expect(JSON.parse(stringifyActual(components))).eql(expectJSON);
62    expect(components).to.include.keys($app_bootstrap$.firstCall.args[0]);
63    return actualStr;
64  }
65
66  beforeEach(() => {
67    components = {};
68    requireStub = sinon.stub();
69    bootstrapStub = sinon.stub();
70
71    $app_define$ = function(componentName, deps, factory) {
72      if (components[componentName]) {
73        throw new Error(`${componentName} is defined repeatly`);
74      }
75
76      let $app_require$ = requireStub;
77      let $app_exports$ = {};
78      let $app_module$ = {exports : $app_exports$};
79
80      factory($app_require$, $app_exports$, $app_module$);
81      components[componentName] = $app_module$.exports;
82    }
83
84    $app_bootstrap$ = bootstrapStub;
85  });
86
87  it('class', () => {
88    expectActual('class');
89  });
90  it('event', () => {
91    expectActual('event');
92  });
93  it('expression', () => {
94    expectActual('expression');
95  });
96  it('commonAttr', () => {
97    expectActual('commonAttr');
98  });
99  it('uncommonAttr', () => {
100    expectActual('uncommonAttr');
101  });
102  it('privateAttr', () => {
103    expectActual('privateAttr');
104  });
105  it('forDirective', () => {
106    expectActual('forDirective');
107  });
108  it('ifDirective', () => {
109    expectActual('ifDirective');
110  });
111  it('inlineStyle', () => {
112    expectActual('inlineStyle');
113  });
114  it('exteriorStyle', () => {
115    expectActual('exteriorStyle');
116  });
117  it('importCSS', () => {
118    expectActual('importCSS');
119  });
120  it('mediaQuery', () => {
121    expectActual('mediaQuery');
122  });
123});