• 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(importEntity, sourceFileName, heritageClausesArray, filePath, sourceFileList);
34    expect(result).toBe('');
35  });
36
37  test('Test the referenctImport2ModuleImport function', () => {
38    const importEntity = {
39      importPath: './ability/dataAbilityHelper',
40      importElements: '\'{ DataAbilityHelper as _DataAbilityHelper }\'',
41    };
42    const currentFilePath = path.join(__dirname, './api/global.d.ts');
43    const code = fs.readFileSync(currentFilePath);
44    const sourceFileList: SourceFile[] = [];
45    const sourceFile = createSourceFile(currentFilePath, code.toString(), ScriptTarget.Latest)
46    sourceFileList.push(sourceFile);
47    const result = referenctImport2ModuleImport(importEntity, currentFilePath, sourceFileList);
48    expect(result).toBe('');
49  });
50});