1.5 KB47 lines
Blame
1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8import {nullthrows} from 'shared/utils';
9import {webviewPlatform} from '../webviewPlatform';
10
11(window.external as unknown as Record<string, unknown>).invoke = jest.fn();
12
13describe('webview platform', () => {
14 const external = () => {
15 return window.external as unknown as {invoke: jest.FunctionLike};
16 };
17
18 it('can send openExternal messages', () => {
19 webviewPlatform.openExternalLink('example.com');
20 expect(external().invoke).toHaveBeenCalledWith(
21 '{"cmd":"openExternal","url":"example.com","id":0}',
22 );
23 });
24
25 it('can send request and receive response messages', async () => {
26 const promise = webviewPlatform.chooseFile?.('my title', true);
27 expect(external().invoke).toHaveBeenCalledWith(
28 '{"cmd":"chooseFile","title":"my title","path":"","multi":true,"mediaOnly":true,"id":1}',
29 );
30
31 const msg = 'Hello';
32 const msg_b64 = Buffer.from(msg).toString('base64');
33 expect(msg_b64).toEqual('SGVsbG8=');
34 const msg_bytes = Buffer.from(msg);
35 expect(msg_bytes).toEqual(Buffer.from(new Uint8Array([72, 101, 108, 108, 111])));
36 window.islWebviewHandleResponse({
37 cmd: 'chooseFile',
38 files: [{name: 'file.txt', base64Content: msg_b64}],
39 id: 1,
40 });
41
42 const result = nullthrows(await promise);
43 expect(result[0].name).toEqual('file.txt');
44 expect(await result[0].size).toEqual(5);
45 });
46});
47