38
loading...
This website collects cookies to deliver better user experience
function onToggleGameMode() {
const shouldGoIntoEditMode = gameMode !== EDITING_GAME_MODE;
let exitEditMode = () => dispatch(enterIdleMode());
// If the words number are different we need to reset the game and
// not just resume it
if (wordsNumber !== words.length) {
setWordsNumber(words.length);
exitEditMode = onRefreshGame;
}
shouldGoIntoEditMode ? dispatch(enterEditingMode()) : exitEditMode();
}
import React from 'react';
import {render as rtlRender, fireEvent, screen} from '@testing-library/react';
import {Provider} from 'react-redux';
import {createStore, combineReducers} from 'redux';
import wordsReducer from '../reducers/words-reducer';
import WordSearchGameState from '../mobx/WordSearchGameState';
import pendingConfirmationReducer from '../reducers/pending-confirmation-reducer';
import gameModeReducer from '../reducers/game-mode-reducer';
import {EDITING_GAME_MODE, IDLE_GAME_MODE} from '../constants';
import {StateContext} from '../App';
import Masthead from './Masthead';
const combinedReducers = combineReducers({
words: wordsReducer,
});
const render = (
ui,
{initialMobxState = new WordSearchGameState(), store = createStore(combinedReducers), ...renderOptions} = {}
) => {
const Wrapper = ({children}) => (
<Provider store={store}>
<StateContext.Provider value={initialMobxState}>{children}</StateContext.Provider>
</Provider>
);
return rtlRender(ui, {wrapper: Wrapper, ...renderOptions});
};
describe('Edit toggle button', () => {
it('should toggle the "Edit" button to "Play" when the edit button is clicked', () => {
const combinedReducers = combineReducers({
words: wordsReducer,
gameMode: gameModeReducer,
});
const store = createStore(combinedReducers);
const {getByRole} = render(<Masthead />, {store});
expect(getByRole('button', {name: 'Edit'})).toBeInTheDocument();
fireEvent.click(getByRole('button', {name: 'Edit'}));
expect(getByRole('button', {name: 'Play'})).toBeInTheDocument();
});
});
it('should toggle the "Play" button back to "Edit" upon click, when in "edit" mode and there was no change in the words bank', () => {
const combinedReducers = combineReducers({
words: wordsReducer,
gameMode: gameModeReducer,
});
const store = createStore(combinedReducers);
const {getByRole} = render(<Masthead />, {store});
expect(getByRole('button', {name: 'Edit'})).toBeInTheDocument();
fireEvent.click(getByRole('button', {name: 'Edit'}));
expect(getByRole('button', {name: 'Play'})).toBeInTheDocument();
fireEvent.click(getByRole('button', {name: 'Play'}));
expect(getByRole('button', {name: 'Edit'})).toBeInTheDocument();
});
it('should not toggle back to "Edit" upon click, when in "edit" mode and there was change in the words bank', () => {
const {getByRole} = screen;
expect(getByRole('button', {name: 'Edit'})).toBeInTheDocument();
fireEvent.click(getByRole('button', {name: 'Edit'}));
expect(getByRole('button', {name: 'Play'})).toBeInTheDocument();
store.dispatch(addWord('chuck'));
fireEvent.click(getByRole('button', {name: 'Play'}));
expect(getByRole('button', {name: 'Play'})).toBeInTheDocument();
});
it('should not toggle back to "Edit" upon click, when in "edit" mode and there was change in the words bank', () => {
const combinedReducers = combineReducers({
words: wordsReducer,
gameMode: gameModeReducer,
pendingConfirmation: pendingConfirmationReducer,
});
const store = createStore(combinedReducers);
const {getByRole} = render(<Masthead />, {store});
fireEvent.click(getByRole('button', {name: 'Edit'}));
store.dispatch(addWord('chuck'));
// Check the confirmation state
let state = store.getState();
expect(state.pendingConfirmation).toBeNull();
fireEvent.click(getByRole('button', {name: 'Play'}));
expect(getByRole('button', {name: 'Play'})).toBeInTheDocument();
// Check the confirmation state
state = store.getState();
expect(state.pendingConfirmation).toBeDefined();
expect(state.pendingConfirmation?.msg).toEqual(
'All progress will reset. Are you sure you wanna refresh the game?'
);
});
it('should not toggle back to "Edit" upon click, when there was a change in the words bank but confirmation was canceled', () => {
const combinedReducers = combineReducers({
words: wordsReducer,
gameMode: gameModeReducer,
pendingConfirmation: pendingConfirmationReducer,
});
const store = createStore(combinedReducers);
const {getByRole} = render(<Masthead />, {store});
fireEvent.click(getByRole('button', {name: 'Edit'}));
// Add a word
store.dispatch(addWord('chuck'));
fireEvent.click(getByRole('button', {name: 'Play'}));
// Cancel the confirmation
store.dispatch(cancelConfirmation());
let state = store.getState();
expect(state.pendingConfirmation).toBeNull();
fireEvent.click(getByRole('button', {name: 'Play'}));
expect(getByRole('button', {name: 'Play'})).toBeInTheDocument();
// Check the confirmation state
state = store.getState();
expect(state.pendingConfirmation).toBeDefined();
expect(state.pendingConfirmation?.msg).toEqual(
'All progress will reset. Are you sure you wanna refresh the game?'
);
});
function onToggleGameMode() {
const shouldGoIntoEditMode = gameMode !== EDITING_GAME_MODE;
let exitEditMode = () => dispatch(enterIdleMode());
// If the words number are different we need to reset the game and
// not just resume it
if (wordsNumber !== words.length) {
setWordsNumber(words.length);
exitEditMode = onRefreshGame;
}
shouldGoIntoEditMode ? dispatch(enterEditingMode()) : exitEditMode();
}
setWordsNumber(words.length);
function onRefreshGame() {
const pendingConfirmationAction = resetGame();
const pendingConfirmationCallback = stateContext.reset.bind(stateContext);
const confirmResetGameAction = createConfirmAction({
pendingConfirmationAction,
msg: 'All progress will reset. Are you sure you wanna refresh the game?',
pendingConfirmationCallback,
});
dispatch(confirmResetGameAction);
}
const pendingConfirmationCallback = () => {
stateContext.reset();
setWordsNumber(words.length);
};
describe('Edit toggle button', () => {
it('should toggle the "Edit" button to "Play" when the edit button is clicked', () => {
const combinedReducers = combineReducers({
words: wordsReducer,
gameMode: gameModeReducer,
});
const store = createStore(combinedReducers);
const {getByRole} = render(<Masthead />, {store});
expect(getByRole('button', {name: 'Edit'})).toBeInTheDocument();
fireEvent.click(getByRole('button', {name: 'Edit'}));
expect(getByRole('button', {name: 'Play'})).toBeInTheDocument();
});
it('should toggle the "Play" button back to "Edit" upon click, when in "edit" mode and there was no change in the words bank', () => {
const combinedReducers = combineReducers({
words: wordsReducer,
gameMode: gameModeReducer,
});
const store = createStore(combinedReducers);
const {getByRole} = render(<Masthead />, {store});
expect(getByRole('button', {name: 'Edit'})).toBeInTheDocument();
fireEvent.click(getByRole('button', {name: 'Edit'}));
expect(getByRole('button', {name: 'Play'})).toBeInTheDocument();
fireEvent.click(getByRole('button', {name: 'Play'}));
expect(getByRole('button', {name: 'Edit'})).toBeInTheDocument();
});
it('should not toggle back to "Edit" upon click, when in "edit" mode and there was change in the words bank', () => {
const combinedReducers = combineReducers({
words: wordsReducer,
gameMode: gameModeReducer,
pendingConfirmation: pendingConfirmationReducer,
});
const store = createStore(combinedReducers);
const {getByRole} = render(<Masthead />, {store});
fireEvent.click(getByRole('button', {name: 'Edit'}));
store.dispatch(addWord('chuck'));
// Check the confirmation state
let state = store.getState();
expect(state.pendingConfirmation).toBeNull();
fireEvent.click(getByRole('button', {name: 'Play'}));
expect(getByRole('button', {name: 'Play'})).toBeInTheDocument();
// Check the confirmation state
state = store.getState();
expect(state.pendingConfirmation).toBeDefined();
expect(state.pendingConfirmation?.msg).toEqual(
'All progress will reset. Are you sure you wanna refresh the game?'
);
});
it('should not toggle back to "Edit" upon click, when there was a change in the words bank but confirmation was canceled', () => {
const combinedReducers = combineReducers({
words: wordsReducer,
gameMode: gameModeReducer,
pendingConfirmation: pendingConfirmationReducer,
});
const store = createStore(combinedReducers);
const {getByRole} = render(<Masthead />, {store});
fireEvent.click(getByRole('button', {name: 'Edit'}));
// Add a word
store.dispatch(addWord('chuck'));
fireEvent.click(getByRole('button', {name: 'Play'}));
// Cancel the confirmation
store.dispatch(cancelConfirmation());
let state = store.getState();
expect(state.pendingConfirmation).toBeNull();
fireEvent.click(getByRole('button', {name: 'Play'}));
expect(getByRole('button', {name: 'Play'})).toBeInTheDocument();
// Check the confirmation state
state = store.getState();
expect(state.pendingConfirmation).toBeDefined();
expect(state.pendingConfirmation?.msg).toEqual(
'All progress will reset. Are you sure you wanna refresh the game?'
);
});
});