addons/isl/src/platform/__tests__/webviewPlatform.test.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import {nullthrows} from 'shared/utils';
b69ab319import {webviewPlatform} from '../webviewPlatform';
b69ab3110
b69ab3111(window.external as unknown as Record<string, unknown>).invoke = jest.fn();
b69ab3112
b69ab3113describe('webview platform', () => {
b69ab3114 const external = () => {
b69ab3115 return window.external as unknown as {invoke: jest.FunctionLike};
b69ab3116 };
b69ab3117
b69ab3118 it('can send openExternal messages', () => {
b69ab3119 webviewPlatform.openExternalLink('example.com');
b69ab3120 expect(external().invoke).toHaveBeenCalledWith(
b69ab3121 '{"cmd":"openExternal","url":"example.com","id":0}',
b69ab3122 );
b69ab3123 });
b69ab3124
b69ab3125 it('can send request and receive response messages', async () => {
b69ab3126 const promise = webviewPlatform.chooseFile?.('my title', true);
b69ab3127 expect(external().invoke).toHaveBeenCalledWith(
b69ab3128 '{"cmd":"chooseFile","title":"my title","path":"","multi":true,"mediaOnly":true,"id":1}',
b69ab3129 );
b69ab3130
b69ab3131 const msg = 'Hello';
b69ab3132 const msg_b64 = Buffer.from(msg).toString('base64');
b69ab3133 expect(msg_b64).toEqual('SGVsbG8=');
b69ab3134 const msg_bytes = Buffer.from(msg);
b69ab3135 expect(msg_bytes).toEqual(Buffer.from(new Uint8Array([72, 101, 108, 108, 111])));
b69ab3136 window.islWebviewHandleResponse({
b69ab3137 cmd: 'chooseFile',
b69ab3138 files: [{name: 'file.txt', base64Content: msg_b64}],
b69ab3139 id: 1,
b69ab3140 });
b69ab3141
b69ab3142 const result = nullthrows(await promise);
b69ab3143 expect(result[0].name).toEqual('file.txt');
b69ab3144 expect(await result[0].size).toEqual(5);
b69ab3145 });
b69ab3146});