• 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 { getSourceFileAssembly } from '../declaration-node/sourceFileElementsAssemply';
21import { generateCommonFunction } from '../generate/generateCommonFunction';
22
23describe('generateCommonFunction.ts file test', (): void => {
24  test('Test the generateCommonFunction function', (): void => {
25    const filePath = path.join(__dirname, './api/global.d.ts');
26    const code = fs.readFileSync(filePath);
27    const sourceFile = createSourceFile(filePath, code.toString(), ScriptTarget.Latest);
28    const sourceFileEntity = getSourceFileAssembly(sourceFile, 'global');
29    const mockApi = 'import { TouchObject, KeyEvent, MouseEvent } from "../component/ets/common"';
30    const functionArray = sourceFileEntity.functionDeclarations.get('setInterval') ?? [];
31    const result = generateCommonFunction('setInterval', functionArray, sourceFile, mockApi, true);
32    const expectedResult = `export const setInterval = function(...args) {console.warn('The setInterval.setInterval interface in the Previewer is a mocked implementation and may behave differently than on a real device.');
33return 0;};
34      if (!global.setInterval) {
35        global.setInterval = setInterval;
36      }
37    `;
38    expect(result).toBe(expectedResult);
39  });
40});
41