From 305a5522f620caf2f8728a780cb42a6a3eed3170 Mon Sep 17 00:00:00 2001 From: Frederik Bolding Date: Fri, 7 Feb 2025 11:55:36 +0100 Subject: [PATCH] Add file input test --- .../snap-ui-renderer.test.js.snap | 63 ++++++++ .../snap-ui-renderer/snap-ui-renderer.test.js | 146 +++++++++++++++++- 2 files changed, 207 insertions(+), 2 deletions(-) diff --git a/ui/components/app/snaps/snap-ui-renderer/__snapshots__/snap-ui-renderer.test.js.snap b/ui/components/app/snaps/snap-ui-renderer/__snapshots__/snap-ui-renderer.test.js.snap index f0cc98e97313..42d019e2a1f0 100644 --- a/ui/components/app/snaps/snap-ui-renderer/__snapshots__/snap-ui-renderer.test.js.snap +++ b/ui/components/app/snaps/snap-ui-renderer/__snapshots__/snap-ui-renderer.test.js.snap @@ -346,6 +346,69 @@ exports[`SnapUIRenderer supports container backgrounds 1`] = ` `; +exports[`SnapUIRenderer supports file inputs 1`] = ` +
+
+
+
+
+
+ + +
+ +

+ Drop your file here +

+
+
+ +
+
+
+ +
+
+`; + exports[`SnapUIRenderer supports forms 1`] = `
{ name: 'form', children: [ Field({ label: 'My Input', children: Input({ name: 'input' }) }), - Field({ label: 'Checkbox', children: Checkbox({ name: 'checkbox' }) }), + Field({ + label: 'Checkbox', + children: Checkbox({ name: 'checkbox' }), + }), Button({ type: 'submit', name: 'submit', children: 'Submit' }), ], }), @@ -421,7 +426,11 @@ describe('SnapUIRenderer', () => { method: ' ', params: { context: null, - event: { name: 'checkbox', type: 'InputChangeEvent', value: true }, + event: { + name: 'checkbox', + type: 'InputChangeEvent', + value: true, + }, id: MOCK_INTERFACE_ID, }, }, @@ -548,4 +557,137 @@ describe('SnapUIRenderer', () => { expect(container).toMatchSnapshot(); }); + + it('supports file inputs', async () => { + const { container, getByRole } = renderInterface( + Box({ + children: Form({ + name: 'form', + children: [ + Field({ + label: 'My Input', + children: FileInput({ name: 'input' }), + }), + Button({ type: 'submit', name: 'submit', children: 'Submit' }), + ], + }), + }), + ); + + const file = new File(['foo'], 'foo.svg', { type: 'image/svg' }); + + // JSDOM doesn't support array buffer so we overwrite it + file.arrayBuffer = async () => { + return new Uint8Array([102, 111, 111]); + }; + + const input = container.querySelector('#input'); + await userEvent.upload(input, file); + + expect(submitRequestToBackground).toHaveBeenNthCalledWith( + 1, + 'updateInterfaceState', + [ + MOCK_INTERFACE_ID, + { + form: { + input: { + contentType: 'image/svg', + contents: 'Zm9v', + name: 'foo.svg', + size: 3, + }, + }, + }, + ], + ); + + expect(submitRequestToBackground).toHaveBeenNthCalledWith( + 2, + 'handleSnapRequest', + [ + { + handler: 'onUserInput', + origin: '', + request: { + jsonrpc: '2.0', + method: ' ', + params: { + context: null, + event: { + name: 'input', + type: 'FileUploadEvent', + file: { + contentType: 'image/svg', + contents: 'Zm9v', + name: 'foo.svg', + size: 3, + }, + }, + id: MOCK_INTERFACE_ID, + }, + }, + snapId: MOCK_SNAP_ID, + }, + ], + ); + + const button = getByRole('button'); + fireEvent.click(button); + + expect(submitRequestToBackground).toHaveBeenNthCalledWith( + 5, + 'handleSnapRequest', + [ + { + handler: 'onUserInput', + origin: '', + request: { + jsonrpc: '2.0', + method: ' ', + params: { + context: null, + event: { name: 'submit', type: 'ButtonClickEvent' }, + id: MOCK_INTERFACE_ID, + }, + }, + snapId: MOCK_SNAP_ID, + }, + ], + ); + + expect(submitRequestToBackground).toHaveBeenNthCalledWith( + 6, + 'handleSnapRequest', + [ + { + handler: 'onUserInput', + origin: '', + request: { + jsonrpc: '2.0', + method: ' ', + params: { + context: null, + event: { + name: 'form', + type: 'FormSubmitEvent', + value: { + input: { + contentType: 'image/svg', + contents: 'Zm9v', + name: 'foo.svg', + size: 3, + }, + }, + }, + id: MOCK_INTERFACE_ID, + }, + }, + snapId: MOCK_SNAP_ID, + }, + ], + ); + + expect(container).toMatchSnapshot(); + }); });