10000 Merge pull request #3 from Sanoobk/add-jest · Sanoobk/github-actions-for-ci@47d3532 · GitHub
[go: up one dir, main page]

Skip to content

Commit 47d3532

Browse files
authored
Merge pull request #3 from Sanoobk/add-jest
Add jest tests
2 parents 489efd6 + bec4e2d commit 47d3532

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`App Contains the compiled JavaScript 1`] = `"!function(t){var e={};function n(r){if(e[r])return e[r].exports;var l=e[r]={i:r,l:!1,exports:{}};return t[r].call(l.exports,l,l.exports,n),l.l=!0,l.exports}n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(t,\\"__esModule\\",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&\\"object\\"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,\\"default\\",{enumerable:!0,value:t}),2&e&&\\"string\\"!=typeof t)for(var l in t)n.d(r,l,function(e){return t[e]}.bind(null,l));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,\\"a\\",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p=\\"\\",n(n.s=1)}([function(t,e,n){\\"use strict\\";n.r(e),n.d(e,\\"default\\",(function(){return r}));class r{constructor(t,e){this.p1=t,this.p2=e,this.board=[[null,null,null],[null,null,null],[null,null,null]],this.player=Math.random()<.5?this.p1:this.p2,this.sym=\\"X\\"}turn(t,e){e=e||t,this.board[t][e]=this.sym}nextPlayer(){this.player=this.player===this.p1?this.p2:this.p1,this.sym=\\"X\\"===this.sym?\\"O\\":\\"X\\"}hasWinner(){return this.rowWin()||this.colWin()||this.diagWin()}rowWin(){let t=!1;for(let e=0;e<3;e++){const n=this.board[e];null!==n[0]&&(t=t||n[0]===n[1]&&n[0]===n[2])}return t}colWin(){let t=!1;for(let e=0;e<3;e++){const n=this.board;null!==n[0][e]&&(t=t||n[0][e]===n[1][e]&&n[0][e]===n[2][e])}return t}diagWin(){const t=this.board;return null!==t[0][0]&&t[0][0]===t[1][1]&&t[0][0]===t[2][2]||null!==t[0][2]&&t[0][2]===t[1][1]&&t[0][2]===t[2][0]}}},function(t,e,n){n(2),t.exports=n(0)},function(t,e,n){\\"use strict\\";n.r(e);var r=n(0);let l,o;for(;!l;)l=window.prompt(\\"Enter player 1 name:\\");for(;!o&&l!==o;)o=window.prompt(l===o?\`Please enter a different name than \${l}.\`:\\"Enter player 2 name:\\");window.onload=()=>{const t=new r.default(l,o),e=document.getElementById(\\"turn\\"),n=document.getElementById(\\"player\\");n.innerText=t.player,document.querySelectorAll(\\"td\\").forEach(r=>{r.onclick=l=>{r.onclick=void 0,l.target.innerText=t.sym,l.target.onclick=void 0;const[o,i]=l.target.classList;t.turn(o,i),t.hasWinner()?(e.innerText=\`\${t.player} wins!\`,document.querySelectorAll(\\"td\\").forEach(t=>{t.onclick=void 0})):(t.nextPlayer(),n.innerText=t.player)}})}}]);"`;

__test__/game.test.js

Lines changed: 108 additions & 0 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
const Game = require('../src/game').default
2+
const fs = require('fs')
3+
4+
describe('App', () => {
5+
it('Contains the compiled JavaScript', async (done) => {
6+
fs.readFile('./public/main.js', 'utf8', (err, data) => {
7+
expect(err).toBe(null)
8+
expect(data).toMatchSnapshot()
9+
done()
10+
})
11+
})
12+
})
13+
14+
describe('Game', () => {
15+
let game, p1, p2
16+
beforeEach(() => {
17+
p1 = 'Salem'
18+
p2 = 'Nate'
19+
game = new Game(p1, p2)
20+
})
21+
22+
describe('Game', () => {
23+
it('Initializes with two players', async () => {
24+
expect(game.p1).toBe('Salem')
25+
expect(game.p2).toBe('Nate')
26+
})
27+
28+
it('Initializes with an empty board', async () => {
29+
for (let r = 0; r < game.board.length; r++) {
30+
for (let c = 0; c < game.board[r].lenght; c++) {
31+
expect(game.board[r][c]).toBeUndefined()
32+
}
33+
}
34+
})
35+
36+
it('Starts the game with a random player', async () => {
37+
Math.random = () => 0.4
38+
expect(new Game(p1, p2).player).toBe('Salem')
39+
40+
Math.random = () => 0.6
41+
expect(new Game(p1, p2).player).toBe('Nate')
42+
})
43+
})
44+
45+
describe('turn', () => {
46+
it("Inserts an 'X' into the top center", async () => {
47+
game.turn(0, 1)
48+
expect(game.board[0][1]).toBe('X')
49+
})
50+
51+
it("Inserts an 'X' into the top left", async () => {
52+
game.turn(0)
53+
expect(game.board[0][0]).toBe('X')
54+
})
55+
})
56+
57+
describe('nextPlayer', () => {
58+
it('Sets the current player to be whoever it is not', async () => {
59+
Math.random = () => 0.4
60+
const game = new Game(p1, p2)
61+
expect(game.player).toBe('Salem')
62+
game.nextPlayer()
63+
expect(game.player).toBe('Nate')
64+
})
65+
})
66+
67+
describe('hasWinner', () => {
68+
it('Wins if any row is filled', async () => {
69+
for (let r = 0; r < game.board.length; r++) {
70+
for (let c = 0; c < game.board[r].length; c++) {
71+
game.board[r][c] = 'X'
72+
}
73+
expect(game.hasWinner()).toBe(true)
74+
75+
for (let c = 0; c < game.board[r].length; c++) {
76+
game.board[r][c] = null
77+
}
78+
}
79+
})
80+
81+
it('Wins if any column is filled', async () => {
82+
for (let r = 0; r < game.board.length; r++) {
83+
for (let c = 0; c < game.board[r].length; c++) {
84+
game.board[c][r] = 'X'
85+
}
86+
expect(game.hasWinner()).toBe(true)
87+
88+
for (let c = 0; c < game.board[r].length; c++) {
89+
game.board[c][r] = null
90+
}
91+
}
92+
})
93+
94+
it('Wins if down-left diagonal is filled', async () => {
95+
for (let r = 0; r < game.board.length; r++) {
96+
game.board[r][r] = 'X'
97+
}
98+
expect(game.hasWinner()).toBe(true)
99+
})
100+
101+
it('Wins if up-right diagonal is filled', async () => {
102+
for (let r = 0; r < game.board.length; r++) {
103+
game.board[2 - r][r] = 'X'
104+
}
105+
expect(game.hasWinner()).toBe(true)
106+
})
107+
})
108+
})

src/game.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default class Game {
22
constructor (p1, p2) {
33
this.p1 = p1
4-
this.p2 = p2
4+
this.p2 = 'Bananas'
55
this.board = [[null, null, null], [null, null, null], [null, null, null]]
66
this.player = Math.random() < 0.5 ? this.p1 : this.p2
77
this.sym = 'X'

0 commit comments

Comments
 (0)
0