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 { generateCommonMethod } from '../generate/generateCommonMethod'; 21 22describe('generateCommonMethod.ts file test', (): void => { 23 test('Test the generateCommonMethod function', (): void => { 24 const filePath = path.join(__dirname, './api/@ohos.account.appAccount.d.ts'); 25 const code = fs.readFileSync(filePath); 26 const sourceFile = createSourceFile(filePath, code.toString(), ScriptTarget.Latest); 27 const methodArray = [ 28 { 29 args: [ 30 { 31 paramName: 'authType', 32 paramTypeKind: 147, 33 paramTypeString: 'string' 34 }, 35 { 36 paramName: 'callerBundleName', 37 paramTypeKind: 147, 38 paramTypeString: 'string' 39 }, 40 { 41 paramName: 'options', 42 paramTypeKind: 177, 43 paramTypeString: '{ [key: string]: any }' 44 }, 45 { 46 paramName: 'callback', 47 paramTypeKind: 173, 48 paramTypeString: 'AuthenticatorCallback' 49 } 50 ], 51 functionName: { 52 name: 'addAccountImplicitly', 53 kind: 78, 54 expressionKind: -1 55 }, 56 modifiers: [], 57 returnType: { 58 returnKindName: 'void', 59 returnKind: 113 60 } 61 } 62 ]; 63 const mockApi = 'import { AsyncCallback, Callback } from \'./ohos_base\'' 64 + 'import { mockWant } from \'./ohos_app_ability_Want\'' 65 + 'import { mockRpc } from \'./ohos_rpc\''; 66 const result = generateCommonMethod('Authenticator', methodArray, sourceFile, mockApi); 67 const expectedResult = `this.addAccountImplicitly = function(...args) {console.warn('The Authenticator.addAccountImplicitly interface in the Previewer is a mocked implementation and may behave differently than on a real device.'); 68if (args && typeof args[args.length - 1] === 'function') { 69 args[args.length - 1].call(this, '[PC Preview] unknown type'); 70}}; 71`; 72 expect(result).toBe(expectedResult); 73 }); 74}); 75