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