Skip to content

Commit

Permalink
fix: Proxy
Browse files Browse the repository at this point in the history
Proxy default values were not all working.
The Real functions themselves were defined, they were just
returning `undefined`.
  • Loading branch information
mmcknett committed Jul 13, 2022
1 parent 3ccf198 commit c5c2105
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 33 deletions.
42 changes: 23 additions & 19 deletions src/demo-react/adagrams-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,46 @@ const Real = {

const Proxy = {
drawLetters() {
const defaultLetters = ["H", "E", "L", "L", "O", "W", "O", "R", "L", "D"];

if (typeof Real.drawLetters === "function") {
return Real.drawLetters() || defaultLetters;
const real = Real.drawLetters();
if (typeof real === 'undefined') {
return ["H", "E", "L", "L", "O", "W", "O", "R", "L", "D"];
}

return defaultLetters;
return real;
},

usesAvailableLetters(input, lettersInHand) {
if (typeof Real.usesAvailableLetters === "function") {
return Real.usesAvailableLetters(input, lettersInHand);
const real = Real.usesAvailableLetters(input, lettersInHand);
if (typeof real === 'undefined') {
return true;
}

return true;
return real;
},

scoreWord(word) {
if (typeof Real.scoreWord === "function") {
return Real.scoreWord(word);
const real = Real.scoreWord(word);
if (typeof real === 'undefined') {
return 1;
}

return 1;
return real;
},

highestScoreFrom(words) {
if (typeof Real.highestScoreFrom === "function") {
return Real.highestScoreFrom(words);
const real = Real.highestScoreFrom(words);
if (typeof real === 'undefined') {
if (words.length < 1) {
return {};
}

return {
word: words[0],
score: Proxy.scoreWord(words[0]),
};
}

if (words.length < 1) return null;

return {
word: words[0],
score: Proxy.scoreWord(words[0]),
};
return real;
},
};

Expand Down
2 changes: 1 addition & 1 deletion src/demo-react/gamestate/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { validateGuessInput } from './rules';
import { ScreenId } from './screens';
import { timerMiddleware } from './timer';

import Adagrams from 'demo-react/adagrams';
import Adagrams from 'demo-react/adagrams-proxy';

const GO_STRAIGHT_TO_WIN = false;

Expand Down
2 changes: 1 addition & 1 deletion src/demo-react/gamestate/rules.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Actions from './action-types';
import { SetErrorAction } from './errors';

import Adagrams from 'demo-react/adagrams';
import Adagrams from 'demo-react/adagrams-proxy';

export function validateGuessInput(wrappedReducer) {
return (state, action) => {
Expand Down
8 changes: 0 additions & 8 deletions src/demo-react/gamestate/screens.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,3 @@ export class SwitchScreenAction {
this.payload = screenId;
}
}

export function onHelpScreen(state) {
return state.currentScreen === ScreenId.HOW_TO;
}

export function onSetupScreen(state) {
return state.currentScreen === ScreenId.SETUP;
}
5 changes: 4 additions & 1 deletion src/demo-react/gamestate/win-selectors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Adagrams from 'demo-react/adagrams';
import Adagrams from 'demo-react/adagrams-proxy';

export class WinScreenInfo {
playerScores;
roundWinners;

constructor(state) {
this.playerScores = state.players.map(
player => {
Expand Down
2 changes: 1 addition & 1 deletion src/demo-react/screens/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { GUESS } from '../gamestate/action-types';
import GameTimer from '../components/timer';
import { useGameStateContext } from '../components/gamestate-context';

import Adagrams from 'demo-react/adagrams';
import Adagrams from 'demo-react/adagrams-proxy';

export default function Game() {
const { state } = useGameStateContext();
Expand Down
4 changes: 2 additions & 2 deletions test/demo-react/gamestate/reducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import * as Actions from 'demo-react/gamestate/action-types';
import makeAction from 'demo-react/gamestate/generic-action';
import { ScreenId } from 'demo-react/gamestate/screens';

import Adagrams from 'demo-react/adagrams';
import Adagrams from 'demo-react/adagrams-proxy';

jest.mock('demo-react/adagrams', () => {
jest.mock('demo-react/adagrams-proxy', () => {
return {
drawLetters: jest.fn(() => ["H", "E", "L", "L", "O", "W", "O", "R", "L", "D"]),
usesAvailableLetters: jest.fn(() => true),
Expand Down

0 comments on commit c5c2105

Please sign in to comment.