• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 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
16import fs from 'fs';
17import path from 'path';
18import { describe, expect, test } from '@jest/globals';
19import { createSourceFile, ScriptTarget } from 'typescript';
20import type { SourceFile } from 'typescript';
21import { generateImportDeclaration, referenctImport2ModuleImport } from '../generate/generateMockJsFile';
22
23describe('generateModuleDeclaration.ts file test', () => {
24  test('Test the generateImportDeclaration function', () => {
25    const filePath = path.join(__dirname, './api/@ohos.ability.ability.d.ts');
26    const importEntity = {
27      importPath: 'SpecialEvent',
28      importElements: '{ TouchObject, KeyEvent, MouseEvent }'
29    };
30    const sourceFileName = 'ohos_ability_ability';
31    const heritageClausesArray = [];
32    const sourceFileList = [];
33    const result = generateImportDeclaration(
34      importEntity,
35      sourceFileName,
36      heritageClausesArray,
37      filePath,
38      sourceFileList
39    );
40    expect(result).toBe('');
41  });
42
43  test('Test the referenctImport2ModuleImport function', () => {
44    const importEntity = {
45      importPath: './ability/dataAbilityHelper',
46      importElements: '\'{ DataAbilityHelper as _DataAbilityHelper }\'',
47    };
48    const currentFilePath = path.join(__dirname, './api/global.d.ts');
49    const code = fs.readFileSync(currentFilePath);
50    const sourceFileList: SourceFile[] = [];
51    const sourceFile = createSourceFile(currentFilePath, code.toString(), ScriptTarget.Latest);
52    sourceFileList.push(sourceFile);
53    const result = referenctImport2ModuleImport(importEntity, currentFilePath, sourceFileList);
54    expect(result).toBe('');
55  });
56});
57