1 | const { readFileSync } = require('fs');
|
2 | const { join } = require('path');
|
3 | const { execSync } = require('child_process');
|
4 |
|
5 | const compileCommand = '../bin/jsx2mp.js build --type component --entry ./component --dist ./dist --turn-off-check-update';
|
6 |
|
7 | let jsonContent, jsContent, axmlContent;
|
8 |
|
9 | const currentCwd = process.cwd();
|
10 | const cwd = currentCwd.indexOf('jsx2mp-cli') === -1 ? join(currentCwd, 'packages/jsx2mp-cli') : currentCwd;
|
11 |
|
12 | const execSyncWithCwd = (command) => {
|
13 | execSync(command, {
|
14 | cwd
|
15 | });
|
16 | };
|
17 |
|
18 | beforeAll(() => {
|
19 | execSyncWithCwd(`cd demo && npm install --no-package-lock && ${compileCommand}`);
|
20 |
|
21 |
|
22 | jsonContent = readFileSync(join(cwd, 'demo/dist/component.json'), {encoding: 'utf8'});
|
23 | jsContent = readFileSync(join(cwd, 'demo/dist/component.js'), {encoding: 'utf8'});
|
24 | axmlContent = readFileSync(join(cwd, 'demo/dist/component.axml'), {encoding: 'utf8'});
|
25 | });
|
26 |
|
27 | afterAll(() => {
|
28 | execSyncWithCwd('rm -rf demo/dist');
|
29 | });
|
30 |
|
31 | describe('Component compiled result', () => {
|
32 | it('should return correct axml', () => {
|
33 | expect(axmlContent).toEqual(
|
34 | `<block a:if="{{$ready}}"><view class="__rax-view">
|
35 | Hello World!
|
36 | <rax-image source="{{ uri: _d0 }}" c="{{_d1 && _d2}}" d="{{_d0 ? _d1 : _d2}}" __tagId="0" /></view></block>`);
|
37 | });
|
38 |
|
39 | it('should return correct js', () => {
|
40 | expect(jsContent).toEqual(
|
41 | '"use strict";var _jsx2mpRuntime=require("./npm/jsx2mp-runtime"),_index=require("./npm/rax-view/lib/index.js"),img="./assets/rax.png",a=0,b=1;function Index(){(0,_index.custom)(),this._updateChildProps("0",{source:{uri:img},c:a&&b,d:img?a:b}),this._updateData({_d0:img,_d1:a,_d2:b}),this._updateMethods({})}var __def__=Index;Component((0,_jsx2mpRuntime.createComponent)(__def__));'
|
42 | );
|
43 | });
|
44 |
|
45 | it('should return correct json', () => {
|
46 | expect(jsonContent).toEqual(
|
47 | `{
|
48 | "component": true,
|
49 | "usingComponents": {
|
50 | "rax-image": "./npm/rax-image/lib/miniapp/index"
|
51 | }
|
52 | }
|
53 | `
|
54 | );
|
55 | });
|
56 | });
|