From fee3c1fd544fce9bf951870c30bd9d601d5b2126 Mon Sep 17 00:00:00 2001 From: Nicholas VanCise <40526638+thenick775@users.noreply.github.com> Date: Mon, 18 Nov 2024 20:50:55 -0800 Subject: [PATCH] feat: standardize modal behavior (#215) --- .../components/modals/upload-cheats.spec.tsx | 41 +++++++++++-------- .../src/components/modals/upload-cheats.tsx | 29 +++---------- .../upload-public-external-roms.spec.tsx | 2 - .../modals/upload-public-external-roms.tsx | 19 ++------- .../components/modals/upload-saves.spec.tsx | 41 +++++++++++-------- gbajs3/src/components/modals/upload-saves.tsx | 29 +++---------- gbajs3/src/components/shared/styled.tsx | 4 -- 7 files changed, 64 insertions(+), 101 deletions(-) diff --git a/gbajs3/src/components/modals/upload-cheats.spec.tsx b/gbajs3/src/components/modals/upload-cheats.spec.tsx index 4019ac9f..63040509 100644 --- a/gbajs3/src/components/modals/upload-cheats.spec.tsx +++ b/gbajs3/src/components/modals/upload-cheats.spec.tsx @@ -11,15 +11,17 @@ import { productTourLocalStorageKey } from '../product-tour/consts.tsx'; import type { GBAEmulator } from '../../emulator/mgba/mgba-emulator.tsx'; describe('', () => { - it('uploads file', async () => { + it('uploads file and closes modal', async () => { const uploadCheatsSpy: (file: File, cb?: () => void) => void = vi.fn( (_file, cb) => cb && cb() ); const syncActionIfEnabledSpy = vi.fn(); + const setIsModalOpenSpy = vi.fn(); - const { useEmulatorContext: originalEmulator } = await vi.importActual< - typeof contextHooks - >('../../hooks/context.tsx'); + const { + useEmulatorContext: originalEmulator, + useModalContext: originalModal + } = await vi.importActual('../../hooks/context.tsx'); const { useAddCallbacks: originalCallbacks } = await vi.importActual< typeof addCallbackHooks >('../../hooks/emulator/use-add-callbacks.tsx'); @@ -36,6 +38,11 @@ describe('', () => { syncActionIfEnabled: syncActionIfEnabledSpy })); + vi.spyOn(contextHooks, 'useModalContext').mockImplementation(() => ({ + ...originalModal(), + setIsModalOpen: setIsModalOpenSpy + })); + const testCheatFile = new File(['Some cheat file contents'], 'rom1.cheats'); renderWithContext(); @@ -57,21 +64,20 @@ describe('', () => { expect.anything() ); expect(syncActionIfEnabledSpy).toHaveBeenCalledOnce(); - - expect(screen.getByText('Upload complete!')).toBeVisible(); - expect(screen.queryByText('File to upload:')).not.toBeInTheDocument(); - expect(screen.queryByText('rom1.cheats')).not.toBeInTheDocument(); + expect(setIsModalOpenSpy).toHaveBeenCalledWith(false); }); - it('uploads multiple files', async () => { + it('uploads multiple files and closes modal', async () => { const uploadCheatsSpy: (file: File, cb?: () => void) => void = vi.fn( (_file, cb) => cb && cb() ); const syncActionIfEnabledSpy = vi.fn(); + const setIsModalOpenSpy = vi.fn(); - const { useEmulatorContext: originalEmulator } = await vi.importActual< - typeof contextHooks - >('../../hooks/context.tsx'); + const { + useEmulatorContext: originalEmulator, + useModalContext: originalModal + } = await vi.importActual('../../hooks/context.tsx'); const { useAddCallbacks: originalCallbacks } = await vi.importActual< typeof addCallbackHooks >('../../hooks/emulator/use-add-callbacks.tsx'); @@ -88,6 +94,11 @@ describe('', () => { syncActionIfEnabled: syncActionIfEnabledSpy })); + vi.spyOn(contextHooks, 'useModalContext').mockImplementation(() => ({ + ...originalModal(), + setIsModalOpen: setIsModalOpenSpy + })); + const testCheatFiles = [ new File(['Some cheat file contents 1'], 'rom1.cheats'), new File(['Some cheat file contents 2'], 'rom2.cheats') @@ -117,11 +128,7 @@ describe('', () => { expect.anything() ); expect(syncActionIfEnabledSpy).toHaveBeenCalledOnce(); - - expect(screen.getByText('Upload complete!')).toBeVisible(); - expect(screen.queryByText('Files to upload:')).not.toBeInTheDocument(); - expect(screen.queryByText('rom1.cheats')).not.toBeInTheDocument(); - expect(screen.queryByText('rom2.cheats')).not.toBeInTheDocument(); + expect(setIsModalOpenSpy).toHaveBeenCalledWith(false); }); it('renders form validation error', async () => { diff --git a/gbajs3/src/components/modals/upload-cheats.tsx b/gbajs3/src/components/modals/upload-cheats.tsx index 8cd9ff07..4550b442 100644 --- a/gbajs3/src/components/modals/upload-cheats.tsx +++ b/gbajs3/src/components/modals/upload-cheats.tsx @@ -11,9 +11,7 @@ import { EmbeddedProductTour, type TourSteps } from '../product-tour/embedded-product-tour.tsx'; -import { CircleCheckButton } from '../shared/circle-check-button.tsx'; import { DragAndDropInput } from '../shared/drag-and-drop-input.tsx'; -import { CenteredTextContainer } from '../shared/styled.tsx'; type InputProps = { cheatFiles: File[]; @@ -24,13 +22,7 @@ const validFileExtensions = ['.cheats']; export const UploadCheatsModal = () => { const { setIsModalOpen } = useModalContext(); const { emulator } = useEmulatorContext(); - const { - reset, - handleSubmit, - setValue, - formState: { isSubmitSuccessful }, - control - } = useForm(); + const { reset, handleSubmit, setValue, control } = useForm(); const { syncActionIfEnabled } = useAddCallbacks(); const cheatsFormId = useId(); @@ -52,8 +44,8 @@ export const UploadCheatsModal = () => { ) ); - reset(); - syncActionIfEnabled(); + await syncActionIfEnabled(); + setIsModalOpen(false); }; const tourSteps: TourSteps = [ @@ -101,7 +93,6 @@ export const UploadCheatsModal = () => { validFileExtensions={validFileExtensions} error={error?.message} hideAcceptedFiles={!value?.length} - hideErrors={isSubmitSuccessful} multiple >

@@ -112,20 +103,12 @@ export const UploadCheatsModal = () => { )} /> - {isSubmitSuccessful && ( - -

Upload complete!

- - )} - + diff --git a/gbajs3/src/components/modals/upload-public-external-roms.spec.tsx b/gbajs3/src/components/modals/upload-public-external-roms.spec.tsx index 8d18abd2..1132bdda 100644 --- a/gbajs3/src/components/modals/upload-public-external-roms.spec.tsx +++ b/gbajs3/src/components/modals/upload-public-external-roms.spec.tsx @@ -84,8 +84,6 @@ describe('', () => { expect(onLoadOrDismissSpy).toHaveBeenCalledOnce(); expect(onLoadOrDismissSpy).toHaveBeenCalledWith('loaded'); expect(setIsModalOpenSpy).toHaveBeenCalledWith(false); - - expect(await screen.findByText('Upload complete!')).toBeVisible(); }); it('renders external rom error', async () => { diff --git a/gbajs3/src/components/modals/upload-public-external-roms.tsx b/gbajs3/src/components/modals/upload-public-external-roms.tsx index ee911745..9249897d 100644 --- a/gbajs3/src/components/modals/upload-public-external-roms.tsx +++ b/gbajs3/src/components/modals/upload-public-external-roms.tsx @@ -21,7 +21,6 @@ import { LoadingIndicator, PacmanIndicator } from '../shared/loading-indicator.tsx'; -import { CenteredTextContainer } from '../shared/styled.tsx'; import type { PublicRomUploadStatus } from '../../hooks/use-show-load-public-roms.tsx'; @@ -60,7 +59,6 @@ export const UploadPublicExternalRomsModal = ({ const theme = useTheme(); const { setIsModalOpen } = useModalContext(); const { emulator } = useEmulatorContext(); - const [hasCompletedUpload, setHasCompletedUpload] = useState(false); const [currentRomURL, setCurrentRomURL] = useState(null); const uploadRomButtonId = useId(); const runGame = useRunGame(); @@ -87,7 +85,6 @@ export const UploadPublicExternalRomsModal = ({ }; emulator?.uploadRom(externalRomFile, runCallback); setCurrentRomURL(null); - setHasCompletedUpload(true); } }, [ onLoadOrDismiss, @@ -113,13 +110,9 @@ export const UploadPublicExternalRomsModal = ({ indicator={} loadingCopy="Loading rom from url:" > - {!hasCompletedUpload && ( - <> -

A public rom URL has been shared with you.

-

You can load it using the upload button!

-

Make sure you trust the provider before uploading:

- - )} +

A public rom URL has been shared with you.

+

You can load it using the upload button!

+

Make sure you trust the provider before uploading:

{!!externalRomLoadError && ( )} - {hasCompletedUpload && ( - -

Upload complete!

-
- )} diff --git a/gbajs3/src/components/shared/styled.tsx b/gbajs3/src/components/shared/styled.tsx index d23ef9a4..f4f2f6f8 100644 --- a/gbajs3/src/components/shared/styled.tsx +++ b/gbajs3/src/components/shared/styled.tsx @@ -1,10 +1,6 @@ import { BiPlus } from 'react-icons/bi'; import { styled } from 'styled-components'; -export const CenteredTextContainer = styled.div` - text-align: center; -`; - export const CenteredText = styled.p` text-align: center; margin: 0;