Skip to content

Commit

Permalink
feat: test interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
thenick775 committed Nov 19, 2024
1 parent 791372d commit fffd7c3
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 3 deletions.
24 changes: 22 additions & 2 deletions gbajs3/src/components/controls/virtual-controls.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { VirtualControls } from './virtual-controls.tsx';
import { renderWithContext } from '../../../test/render-with-context.tsx';
import { GbaDarkTheme } from '../../context/theme/theme.tsx';
import * as contextHooks from '../../hooks/context.tsx';
import * as addCallbackHooks from '../../hooks/emulator/use-add-callbacks.tsx';
import * as quickReloadHooks from '../../hooks/emulator/use-quick-reload.tsx';
import { UploadSaveToServerModal } from '../modals/upload-save-to-server.tsx';

Expand Down Expand Up @@ -270,18 +271,26 @@ describe('<VirtualControls />', () => {

it('creates save state', async () => {
const createSaveStateSpy: (slot: number) => boolean = vi.fn(() => true);
const syncActionIfEnabledSpy = vi.fn();
const { useEmulatorContext: original } = await vi.importActual<
typeof contextHooks
>('../../hooks/context.tsx');
const { useAddCallbacks: originalCallbacks } = await vi.importActual<
typeof addCallbackHooks
>('../../hooks/emulator/use-add-callbacks.tsx');

vi.spyOn(contextHooks, 'useEmulatorContext').mockImplementation(() => ({
...original(),
emulator: {
createSaveState: createSaveStateSpy,
fsSync: vi.fn() as () => Promise<void>
createSaveState: createSaveStateSpy
} as GBAEmulator
}));

vi.spyOn(addCallbackHooks, 'useAddCallbacks').mockImplementation(() => ({
...originalCallbacks(),
syncActionIfEnabled: syncActionIfEnabledSpy
}));

const toastSuccessSpy = vi.spyOn(toast.default, 'success');

localStorage.setItem(saveStateSlotLocalStorageKey, '2');
Expand All @@ -292,16 +301,21 @@ describe('<VirtualControls />', () => {

expect(createSaveStateSpy).toHaveBeenCalledOnce();
expect(createSaveStateSpy).toHaveBeenCalledWith(2);
expect(syncActionIfEnabledSpy).toHaveBeenCalledOnce();
expect(toastSuccessSpy).toHaveBeenCalledWith('Saved slot: 2', {
id: expect.anything()
});
});

it('create save state renders error toast', async () => {
const createSaveStateSpy: (slot: number) => boolean = vi.fn(() => false);
const syncActionIfEnabledSpy = vi.fn();
const { useEmulatorContext: original } = await vi.importActual<
typeof contextHooks
>('../../hooks/context.tsx');
const { useAddCallbacks: originalCallbacks } = await vi.importActual<
typeof addCallbackHooks
>('../../hooks/emulator/use-add-callbacks.tsx');

vi.spyOn(contextHooks, 'useEmulatorContext').mockImplementation(() => ({
...original(),
Expand All @@ -310,6 +324,11 @@ describe('<VirtualControls />', () => {
} as GBAEmulator
}));

vi.spyOn(addCallbackHooks, 'useAddCallbacks').mockImplementation(() => ({
...originalCallbacks(),
syncActionIfEnabled: syncActionIfEnabledSpy
}));

const toastErrorSpy = vi.spyOn(toast.default, 'error');

localStorage.setItem(saveStateSlotLocalStorageKey, '2');
Expand All @@ -320,6 +339,7 @@ describe('<VirtualControls />', () => {

expect(createSaveStateSpy).toHaveBeenCalledOnce();
expect(createSaveStateSpy).toHaveBeenCalledWith(2);
expect(syncActionIfEnabledSpy).not.toHaveBeenCalled();
expect(toastErrorSpy).toHaveBeenCalledWith('Failed to save slot: 2', {
id: expect.anything()
});
Expand Down
21 changes: 21 additions & 0 deletions gbajs3/src/components/modals/cheats.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { describe, expect, it, vi } from 'vitest';
import { CheatsModal } from './cheats.tsx';
import { renderWithContext } from '../../../test/render-with-context.tsx';
import * as contextHooks from '../../hooks/context.tsx';
import * as addCallbackHooks from '../../hooks/emulator/use-add-callbacks.tsx';
import { productTourLocalStorageKey } from '../product-tour/consts.tsx';

import type {
Expand Down Expand Up @@ -129,7 +130,11 @@ describe('<CheatsModal />', () => {
const { useEmulatorContext: original } = await vi.importActual<
typeof contextHooks
>('../../hooks/context.tsx');
const { useAddCallbacks: originalCallbacks } = await vi.importActual<
typeof addCallbackHooks
>('../../hooks/emulator/use-add-callbacks.tsx');
const autoLoadCheatsSpy: () => boolean = vi.fn(() => true);
const syncActionIfEnabledSpy = vi.fn();

vi.spyOn(contextHooks, 'useEmulatorContext').mockImplementation(() => ({
...original(),
Expand All @@ -143,6 +148,11 @@ describe('<CheatsModal />', () => {
} as GBAEmulator
}));

vi.spyOn(addCallbackHooks, 'useAddCallbacks').mockImplementation(() => ({
...originalCallbacks(),
syncActionIfEnabled: syncActionIfEnabledSpy
}));

renderWithContext(<CheatsModal />);

await userEvent.type(screen.getByLabelText('Name'), '_edited');
Expand All @@ -159,6 +169,7 @@ describe('<CheatsModal />', () => {
enable: true
}
]);
expect(syncActionIfEnabledSpy).toHaveBeenCalledOnce();
expect(uploadCheatsSpy).toHaveBeenCalledOnce();
expect(autoLoadCheatsSpy).toHaveBeenCalledOnce();
});
Expand All @@ -174,7 +185,11 @@ describe('<CheatsModal />', () => {
const { useEmulatorContext: original } = await vi.importActual<
typeof contextHooks
>('../../hooks/context.tsx');
const { useAddCallbacks: originalCallbacks } = await vi.importActual<
typeof addCallbackHooks
>('../../hooks/emulator/use-add-callbacks.tsx');
const autoLoadCheatsSpy: () => boolean = vi.fn(() => true);
const syncActionIfEnabledSpy = vi.fn();

vi.spyOn(contextHooks, 'useEmulatorContext').mockImplementation(() => ({
...original(),
Expand All @@ -188,6 +203,11 @@ describe('<CheatsModal />', () => {
} as GBAEmulator
}));

vi.spyOn(addCallbackHooks, 'useAddCallbacks').mockImplementation(() => ({
...originalCallbacks(),
syncActionIfEnabled: syncActionIfEnabledSpy
}));

renderWithContext(<CheatsModal />);

await userEvent.click(screen.getByRole('button', { name: 'Raw' }));
Expand All @@ -200,6 +220,7 @@ describe('<CheatsModal />', () => {

await userEvent.click(screen.getByRole('button', { name: 'Submit' }));

expect(syncActionIfEnabledSpy).toHaveBeenCalledOnce();
expect(uploadCheatsSpy).toHaveBeenCalledOnce();
expect(autoLoadCheatsSpy).toHaveBeenCalledOnce();
expect(getCurrentCheatsFileNameSpy).toHaveBeenCalledOnce();
Expand Down
11 changes: 11 additions & 0 deletions gbajs3/src/components/modals/file-system.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { describe, expect, it, vi } from 'vitest';
import { FileSystemModal } from './file-system.tsx';
import { renderWithContext } from '../../../test/render-with-context.tsx';
import * as contextHooks from '../../hooks/context.tsx';
import * as addCallbackHooks from '../../hooks/emulator/use-add-callbacks.tsx';
import { productTourLocalStorageKey } from '../product-tour/consts.tsx';

import type {
Expand Down Expand Up @@ -60,10 +61,14 @@ describe('<FileSystemModal />', () => {

it('deletes file from the tree', async () => {
const deleteFileSpy: (p: string) => void = vi.fn();
const syncActionIfEnabledSpy = vi.fn();
const listAllFilesSpy = vi.fn(() => defaultFSData);
const { useEmulatorContext: original } = await vi.importActual<
typeof contextHooks
>('../../hooks/context.tsx');
const { useAddCallbacks: originalCallbacks } = await vi.importActual<
typeof addCallbackHooks
>('../../hooks/emulator/use-add-callbacks.tsx');

vi.spyOn(contextHooks, 'useEmulatorContext').mockImplementation(() => {
return {
Expand All @@ -75,6 +80,11 @@ describe('<FileSystemModal />', () => {
};
});

vi.spyOn(addCallbackHooks, 'useAddCallbacks').mockImplementation(() => ({
...originalCallbacks(),
syncActionIfEnabled: syncActionIfEnabledSpy
}));

renderWithContext(<FileSystemModal />);

listAllFilesSpy.mockClear(); // clear calls from initial render
Expand All @@ -85,6 +95,7 @@ describe('<FileSystemModal />', () => {
expect(deleteFileSpy).toHaveBeenCalledOnce();
expect(deleteFileSpy).toHaveBeenCalledWith('/data/games/rom1.gba');
expect(listAllFilesSpy).toHaveBeenCalledOnce();
expect(syncActionIfEnabledSpy).toHaveBeenCalledOnce();
});

it('downloads file from the tree', async () => {
Expand Down
11 changes: 11 additions & 0 deletions gbajs3/src/components/modals/load-rom.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { describe, expect, it, vi } from 'vitest';
import { LoadRomModal } from './load-rom.tsx';
import { renderWithContext } from '../../../test/render-with-context.tsx';
import * as contextHooks from '../../hooks/context.tsx';
import * as addCallbackHooks from '../../hooks/emulator/use-add-callbacks.tsx';
import * as runGameHooks from '../../hooks/emulator/use-run-game.tsx';
import * as listRomHooks from '../../hooks/use-list-roms.tsx';
import * as loadRomHooks from '../../hooks/use-load-rom.tsx';
Expand All @@ -25,9 +26,13 @@ describe('<LoadRomModal />', () => {
(_file, cb) => cb && cb()
);
const runGameSpy = vi.fn();
const syncActionIfEnabledSpy = vi.fn();
const { useEmulatorContext: originalEmulator } = await vi.importActual<
typeof contextHooks
>('../../hooks/context.tsx');
const { useAddCallbacks: originalCallbacks } = await vi.importActual<
typeof addCallbackHooks
>('../../hooks/emulator/use-add-callbacks.tsx');

vi.spyOn(contextHooks, 'useEmulatorContext').mockImplementation(() => ({
...originalEmulator(),
Expand All @@ -39,6 +44,11 @@ describe('<LoadRomModal />', () => {
} as GBAEmulator
}));

vi.spyOn(addCallbackHooks, 'useAddCallbacks').mockImplementation(() => ({
...originalCallbacks(),
syncActionIfEnabled: syncActionIfEnabledSpy
}));

vi.spyOn(runGameHooks, 'useRunGame').mockReturnValue(runGameSpy);

renderWithContext(<LoadRomModal />);
Expand All @@ -54,6 +64,7 @@ describe('<LoadRomModal />', () => {
await waitForElementToBeRemoved(screen.queryByText(/Loading rom:/));

expect(uploadRomSpy).toHaveBeenCalledOnce();
expect(syncActionIfEnabledSpy).toHaveBeenCalledOnce();
expect(runGameSpy).toHaveBeenCalledOnce();
expect(runGameSpy).toHaveBeenCalledWith('/games/rom1.gba');
});
Expand Down
11 changes: 11 additions & 0 deletions gbajs3/src/components/modals/load-save.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { describe, expect, it, vi } from 'vitest';
import { LoadSaveModal } from './load-save.tsx';
import { renderWithContext } from '../../../test/render-with-context.tsx';
import * as contextHooks from '../../hooks/context.tsx';
import * as addCallbackHooks from '../../hooks/emulator/use-add-callbacks.tsx';
import * as listSaveHooks from '../../hooks/use-list-saves.tsx';
import * as loadSaveHooks from '../../hooks/use-load-save.tsx';
import { productTourLocalStorageKey } from '../product-tour/consts.tsx';
Expand All @@ -22,9 +23,13 @@ describe('<LoadSaveModal />', () => {
it('loads save from the server', async () => {
const uploadSaveOrSaveStateSpy: (file: File, cb?: () => void) => void =
vi.fn((_file, cb) => cb && cb());
const syncActionIfEnabledSpy = vi.fn();
const { useEmulatorContext: originalEmulator } = await vi.importActual<
typeof contextHooks
>('../../hooks/context.tsx');
const { useAddCallbacks: originalCallbacks } = await vi.importActual<
typeof addCallbackHooks
>('../../hooks/emulator/use-add-callbacks.tsx');

vi.spyOn(contextHooks, 'useEmulatorContext').mockImplementation(() => ({
...originalEmulator(),
Expand All @@ -36,6 +41,11 @@ describe('<LoadSaveModal />', () => {
} as GBAEmulator
}));

vi.spyOn(addCallbackHooks, 'useAddCallbacks').mockImplementation(() => ({
...originalCallbacks(),
syncActionIfEnabled: syncActionIfEnabledSpy
}));

renderWithContext(<LoadSaveModal />);

expect(await screen.findByText('save1.sav')).toBeInTheDocument();
Expand All @@ -49,6 +59,7 @@ describe('<LoadSaveModal />', () => {
await waitForElementToBeRemoved(screen.queryByText(/Loading save:/));

expect(uploadSaveOrSaveStateSpy).toHaveBeenCalledOnce();
expect(syncActionIfEnabledSpy).toHaveBeenCalledOnce();
});

it('renders message when there are no saves', () => {
Expand Down
21 changes: 21 additions & 0 deletions gbajs3/src/components/modals/save-states.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { describe, expect, it, vi } from 'vitest';
import { SaveStatesModal } from './save-states.tsx';
import { renderWithContext } from '../../../test/render-with-context.tsx';
import * as contextHooks from '../../hooks/context.tsx';
import * as addCallbackHooks from '../../hooks/emulator/use-add-callbacks.tsx';
import { saveStateSlotLocalStorageKey } from '../controls/consts.tsx';
import { productTourLocalStorageKey } from '../product-tour/consts.tsx';

Expand Down Expand Up @@ -61,9 +62,13 @@ describe('<SaveStatesModal />', () => {
it('creates saves states', async () => {
const listSaveStatesSpy = vi.fn(() => ['rom0.ss0']);
const createSaveStateSpy: (s: number) => boolean = vi.fn(() => true);
const syncActionIfEnabledSpy = vi.fn();
const { useEmulatorContext: original } = await vi.importActual<
typeof contextHooks
>('../../hooks/context.tsx');
const { useAddCallbacks: originalCallbacks } = await vi.importActual<
typeof addCallbackHooks
>('../../hooks/emulator/use-add-callbacks.tsx');

vi.spyOn(contextHooks, 'useEmulatorContext').mockImplementation(() => ({
...original(),
Expand All @@ -73,6 +78,11 @@ describe('<SaveStatesModal />', () => {
} as GBAEmulator
}));

vi.spyOn(addCallbackHooks, 'useAddCallbacks').mockImplementation(() => ({
...originalCallbacks(),
syncActionIfEnabled: syncActionIfEnabledSpy
}));

renderWithContext(<SaveStatesModal />);

listSaveStatesSpy.mockClear(); // clear calls from initial render
Expand All @@ -84,6 +94,7 @@ describe('<SaveStatesModal />', () => {
expect(createSaveStateSpy).toHaveBeenCalledOnce();
expect(createSaveStateSpy).toHaveBeenCalledWith(1);
expect(listSaveStatesSpy).toHaveBeenCalledOnce();
expect(syncActionIfEnabledSpy).toHaveBeenCalledOnce();
});

it('renders error if creating save state fails', async () => {
Expand Down Expand Up @@ -112,9 +123,13 @@ describe('<SaveStatesModal />', () => {
it('deletes saves states', async () => {
const deleteSaveStateSpy: (s: number) => void = vi.fn();
const listSaveStatesSpy = vi.fn(() => ['rom0.ss0', 'rom1.ss1']);
const syncActionIfEnabledSpy = vi.fn();
const { useEmulatorContext: original } = await vi.importActual<
typeof contextHooks
>('../../hooks/context.tsx');
const { useAddCallbacks: originalCallbacks } = await vi.importActual<
typeof addCallbackHooks
>('../../hooks/emulator/use-add-callbacks.tsx');

vi.spyOn(contextHooks, 'useEmulatorContext').mockImplementation(() => {
return {
Expand All @@ -126,6 +141,11 @@ describe('<SaveStatesModal />', () => {
};
});

vi.spyOn(addCallbackHooks, 'useAddCallbacks').mockImplementation(() => ({
...originalCallbacks(),
syncActionIfEnabled: syncActionIfEnabledSpy
}));

renderWithContext(<SaveStatesModal />);

listSaveStatesSpy.mockClear(); // clear calls from initial render
Expand All @@ -135,6 +155,7 @@ describe('<SaveStatesModal />', () => {
expect(deleteSaveStateSpy).toHaveBeenCalledOnce();
expect(deleteSaveStateSpy).toHaveBeenCalledWith(1);
expect(listSaveStatesSpy).toHaveBeenCalledOnce();
expect(syncActionIfEnabledSpy).toHaveBeenCalledOnce();
});

it('loads save state', async () => {
Expand Down
Loading

0 comments on commit fffd7c3

Please sign in to comment.