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 { generateCommonMethodSignature } from '../generate/generateCommonMethodSignature'; 21 22describe('generateCommonMethodSignature.ts file test', (): void => { 23 test('Test the generateCommonMethodSignature function', (): void => { 24 const filePath = path.join(__dirname, './api/lifecycle.d.ts'); 25 const code = fs.readFileSync(filePath); 26 const sourceFile = createSourceFile(filePath, code.toString(), ScriptTarget.Latest); 27 const methodSignatureArray = [ 28 { 29 args: [ 30 { 31 paramName: 'want', 32 paramTypeKind: 173, 33 paramTypeString: 'Want' 34 } 35 ], 36 functionName: 'onCreate', 37 returnType: { 38 returnKindName: 'formBindingData.FormBindingData', 39 returnKind: 173 40 } 41 } 42 ]; 43 const mockApi = 'import { mockWant } from \'../../ohos_app_ability_Want\'' 44 + 'import { ResultSet } from \'../../data/rdb/resultSet\'' 45 + 'import { AbilityInfo } from \'../../bundle/abilityInfo\'' 46 + 'import { DataAbilityResult } from \'../../ability/dataAbilityResult\'' 47 + 'import { DataAbilityOperation } from \'../../ability/dataAbilityOperation\'' 48 + 'import { mockDataAbility } from \'../../ohos_data_dataAbility\'' 49 + 'import { mockFormBindingData } from \'../../ohos_application_formBindingData\'' 50 + 'import { mockFormInfo } from \'../../ohos_app_form_formInfo\'' 51 + 'import { mockRdb } from \'../../ohos_data_rdb\'' 52 + 'import { mockRpc } from \'../../ohos_rpc\'' 53 + 'import { mockResourceManager } from \'../../ohos_resourceManager\'' 54 + 'import { PacMap } from \'../../ability/dataAbilityHelper\'' 55 + 'import { AsyncCallback } from \'../../ohos_base\''; 56 const result = generateCommonMethodSignature('LifecycleForm', methodSignatureArray, sourceFile, mockApi); 57 const expectedResult = `onCreate: function(...args) {console.warn('The LifecycleForm.onCreate interface in the Previewer is a mocked implementation and may behave differently than on a real device.'); 58return mockFormBindingData().FormBindingData}, 59`; 60 expect(result).toBe(expectedResult); 61 }); 62}); 63