Skip to content

Commit

Permalink
puppeteer tests working, fixed redundant client socket listeners (wow
Browse files Browse the repository at this point in the history
package-lock.json)
  • Loading branch information
phudlow committed Jun 18, 2019
1 parent 0276421 commit bc525f0
Show file tree
Hide file tree
Showing 8 changed files with 2,301 additions and 165 deletions.
27 changes: 10 additions & 17 deletions client/components/GameLobby.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,16 @@ class GameLobby extends Component {
this.onSlotTogglerClick = this.onSlotTogglerClick.bind(this);
this.onLeaveClick = this.onLeaveClick.bind(this);

socket.on('toggledslotopenclosed', gameData => {
console.log('toggledslotopenclosed', gameData);
this.props.toggledSlotOpenClosed(gameData);
});

socket.on('switchedplayerslot', gameData => {
this.props.switchedPlayerSlot(gameData);
});

socket.on('kickedfromgame', () => {
this.props.kickedFromGame();
});

socket.on('leftgame', () => {
console.log('leftgame');
this.props.leftGame();
});
this.turnSocketListeners('on');
}
componentWillUnmount() {
this.turnSocketListeners('off');
}
turnSocketListeners(which) {
socket[which]('toggledslotopenclosed', this.props.toggledSlotOpenClosed);
socket[which]('switchedplayerslot', this.props.switchedPlayerSlot);
socket[which]('kickedfromgame', this.props.kickedFromGame);
socket[which]('leftgame', this.props.leftGame);
}
onPlayerSlotClick(e) {
const slotIdx = e.nativeEvent.target.parentNode.getAttribute('idx');
Expand Down
20 changes: 9 additions & 11 deletions client/components/GamesBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@ class GamesBrowser extends Component {
this.onCreateGameClick = this.onCreateGameClick.bind(this);
this.onJoinGameClick = this.onJoinGameClick.bind(this);

socket.on('newgamesbrowserdata', games => {
this.props.newGamesBrowserData(games);
});

socket.on('createdgame', data => {
this.props.createdGame(data);
});

socket.on('joinedgame', data => {
this.props.joinedGame(data);
});
this.turnSocketListeners('on');
}
componentWillUnmount() {
this.turnSocketListeners('off');
}
turnSocketListeners(which) {
socket[which]('newgamesbrowserdata', this.props.newGamesBrowserData);
socket[which]('createdgame', this.props.createdGame);
socket[which]('joinedgame', this.props.joinedGame);
}
onCreateGameClick() {
socket.emit('creategame');
Expand Down
13 changes: 8 additions & 5 deletions client/components/Welcome.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ class Welcome extends Component {

this.onNickNameSubmit = this.onNickNameSubmit.bind(this);

// Handle nickname submission responses from socket
socket.on('nicknamedenied', nickName => {
this.turnSocketListeners('on');
}
componentWillUnmount() {
this.turnSocketListeners('off');
}
turnSocketListeners(which) {
socket[which]('nicknameconfirmed', this.props.nickNameConfirmed);
socket[which]('nicknamedenied', nickName => {
this.refs.nickName.focus();
alert(`Nickname ${nickName} denied.`);
});
socket.on('nicknameconfirmed', nickName => {
this.props.nickNameConfirmed(nickName);
});
}
onNickNameSubmit(e) {
e.preventDefault();
Expand Down
37 changes: 37 additions & 0 deletions client/components/Welcome.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

let browser;
beforeAll(async () => {
browser = await require('puppeteer').launch({
headless: false,
executablePath: '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe'
});
});

const firstNickName = 'Billy';
const secondNickName = 'Jonana291';

// === //
// e2e //
// === //
test('Cannot submit a blank nickname.', async () => {
const inputSel = '#welcome form input';

const page = await browser.newPage();
await page.goto('localhost:3000');

// Input is blank
expect(await page.$eval(inputSel, el => el.value)).toBe("");

// Submit
await page.keyboard.press('Enter');

// Welcome page is still showing
expect(await page.$('#games-browser')).toBeNull();
expect(await page.$('#welcome')).toBeTruthy();

// Input is focused
const focusedElHandle = await page.evaluateHandle(() => document.activeElement)
expect(await page.evaluate(el => el === document.activeElement, focusedElHandle)).toBe(true);

browser.close();
});
14 changes: 14 additions & 0 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,18 @@ import React from 'react';

import App from './App';

async () => {
const foo = new Promise((resolve, reject) => {
window.setTimeout(() => {
const msg = 'Two seconds have passed...';
resolve(msg);
}, 2000);
});

await foo.then((msg) => {
console.log('Promise resolved.');
console.log(msg);
})
}

ReactDOM.render(<App />, document.getElementById('root'));
Loading

0 comments on commit bc525f0

Please sign in to comment.