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