|
| 1 | +import React from 'react'; |
| 2 | +import { act, fireEvent, render, screen } from '@testing-library/react'; |
| 3 | +import { createActor } from 'xstate'; |
| 4 | + |
| 5 | +import RoomContext from '../widgets/components/RoomContext'; |
| 6 | +import machines from '../widgets/machines'; |
| 7 | +import EditorContainer from '../widgets/pages/game/EditorContainer'; |
| 8 | + |
| 9 | +const { state, dispatch, checkSolution, editorMode } = vi.hoisted(() => ({ |
| 10 | + state: { |
| 11 | + user: { currentUserId: 1, users: { 1: { subscriptionType: 'premium' } } }, |
| 12 | + game: { |
| 13 | + players: { 1: { id: 1, result: 'lost', isBanned: false } }, |
| 14 | + gameStatus: { gameId: 10, state: 'playing', mode: 'standard' }, |
| 15 | + }, |
| 16 | + editor: { meta: { 1: { currentLangSlug: 'js' } } }, |
| 17 | + }, |
| 18 | + dispatch: vi.fn(), |
| 19 | + checkSolution: vi.fn(), |
| 20 | + editorMode: { value: 'load_active_editor' }, |
| 21 | +})); |
| 22 | + |
| 23 | +vi.mock('react-redux', () => ({ |
| 24 | + useDispatch: () => dispatch, |
| 25 | + useSelector: (selector: (value: unknown) => unknown) => selector(state), |
| 26 | +})); |
| 27 | + |
| 28 | +vi.mock('../widgets/middlewares/Room', () => ({ |
| 29 | + checkGameSolution: checkSolution, |
| 30 | + connectToEditor: (service: ReturnType<typeof createActor>) => () => { |
| 31 | + service.send({ type: editorMode.value }); |
| 32 | + return () => {}; |
| 33 | + }, |
| 34 | +})); |
| 35 | + |
| 36 | +vi.mock('../widgets/pages/game/EditorToolbar', () => ({ |
| 37 | + default: ({ actionBtnsProps }: any) => ( |
| 38 | + <button |
| 39 | + disabled={actionBtnsProps.checkBtnStatus !== 'enabled'} |
| 40 | + onClick={actionBtnsProps.checkResult} |
| 41 | + > |
| 42 | + Check |
| 43 | + </button> |
| 44 | + ), |
| 45 | +})); |
| 46 | + |
| 47 | +describe('game editor solution checks', () => { |
| 48 | + beforeEach(() => { |
| 49 | + vi.clearAllMocks(); |
| 50 | + state.game.gameStatus.state = 'playing'; |
| 51 | + state.game.players[1].isBanned = false; |
| 52 | + editorMode.value = 'load_active_editor'; |
| 53 | + }); |
| 54 | + |
| 55 | + const renderEditor = (mainService: ReturnType<typeof createActor>) => |
| 56 | + render( |
| 57 | + <RoomContext.Provider value={{ mainService, taskService: mainService }}> |
| 58 | + <EditorContainer id={1} type="current_user" editorMachine={machines.editor}> |
| 59 | + {() => null} |
| 60 | + </EditorContainer> |
| 61 | + </RoomContext.Provider>, |
| 62 | + ); |
| 63 | + |
| 64 | + test('keeps checking available when an active game finishes', () => { |
| 65 | + const mainService = createActor(machines.game); |
| 66 | + mainService.start(); |
| 67 | + mainService.send({ type: 'LOAD_GAME', payload: { state: 'playing' } }); |
| 68 | + renderEditor(mainService); |
| 69 | + |
| 70 | + act(() => { |
| 71 | + state.game.gameStatus.state = 'game_over'; |
| 72 | + mainService.send({ type: 'user:check_complete', payload: { state: 'game_over' } }); |
| 73 | + }); |
| 74 | + |
| 75 | + expect(screen.getByRole('button', { name: 'Check' })).toBeEnabled(); |
| 76 | + fireEvent.click(screen.getByRole('button', { name: 'Check' })); |
| 77 | + expect(checkSolution).toHaveBeenCalledOnce(); |
| 78 | + act(() => mainService.stop()); |
| 79 | + }); |
| 80 | + |
| 81 | + test('allows Ctrl+Enter when reopening a live finished game', () => { |
| 82 | + state.game.gameStatus.state = 'game_over'; |
| 83 | + const mainService = createActor(machines.game); |
| 84 | + mainService.start(); |
| 85 | + mainService.send({ type: 'LOAD_GAME', payload: { state: 'game_over' } }); |
| 86 | + renderEditor(mainService); |
| 87 | + |
| 88 | + fireEvent.keyDown(window, { key: 'Enter', ctrlKey: true }); |
| 89 | + expect(checkSolution).toHaveBeenCalledOnce(); |
| 90 | + act(() => mainService.stop()); |
| 91 | + }); |
| 92 | + |
| 93 | + test.each(['timeout', 'stored', 'replay', 'banned'])('disables checking for %s', (mode) => { |
| 94 | + state.game.gameStatus.state = mode === 'timeout' ? 'timeout' : 'game_over'; |
| 95 | + if (mode === 'stored') editorMode.value = 'load_stored_editor'; |
| 96 | + if (mode === 'banned') editorMode.value = 'load_banned_editor'; |
| 97 | + const mainService = createActor(machines.game, { |
| 98 | + input: { subscriptionType: 'premium' }, |
| 99 | + }); |
| 100 | + mainService.start(); |
| 101 | + mainService.send({ type: 'LOAD_GAME', payload: { state: state.game.gameStatus.state } }); |
| 102 | + if (mode === 'replay') { |
| 103 | + mainService.send({ type: 'START_LOADING_PLAYBOOK' }); |
| 104 | + mainService.send({ type: 'LOAD_PLAYBOOK', payload: {} }); |
| 105 | + } |
| 106 | + renderEditor(mainService); |
| 107 | + |
| 108 | + expect(screen.getByRole('button', { name: 'Check' })).toBeDisabled(); |
| 109 | + fireEvent.keyDown(window, { key: 'Enter', ctrlKey: true }); |
| 110 | + expect(checkSolution).not.toHaveBeenCalled(); |
| 111 | + act(() => mainService.stop()); |
| 112 | + }); |
| 113 | +}); |
0 commit comments