-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathmemory.spec.js
More file actions
137 lines (113 loc) · 4.44 KB
/
memory.spec.js
File metadata and controls
137 lines (113 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const cardsArray = [
{ name: 'aquaman', img: 'aquaman.jpg' },
{ name: 'batman', img: 'batman.jpg' },
{ name: 'captain america', img: 'captain-america.jpg' },
{ name: 'fantastic four', img: 'fantastic-four.jpg' },
{ name: 'flash', img: 'flash.jpg' },
{ name: 'green arrow', img: 'green-arrow.jpg' },
{ name: 'green lantern', img: 'green-lantern.jpg' },
{ name: 'ironman', img: 'ironman.jpg' },
{ name: 'aquaman', img: 'aquaman.jpg' },
{ name: 'batman', img: 'batman.jpg' },
{ name: 'captain america', img: 'captain-america.jpg' },
{ name: 'fantastic four', img: 'fantastic-four.jpg' },
{ name: 'flash', img: 'flash.jpg' },
{ name: 'green arrow', img: 'green-arrow.jpg' },
{ name: 'green lantern', img: 'green-lantern.jpg' },
{ name: 'ironman', img: 'ironman.jpg' }
];
let memoryGame;
describe('MemoryGame', () => {
beforeEach(() => {
memoryGame = new MemoryGame(cardsArray);
});
it('should be declared', () => {
expect(typeof MemoryGame).toBe('function');
});
describe('constructor method', () => {
it('should receive `cards` as a parameter and store the cards in its own `cards` property', () => {
expect(MemoryGame.constructor.length).toBe(1);
expect(memoryGame.cards instanceof Array).toBe(true);
});
it('should have a pickedCards property starting off as an empty array', () => {
expect(memoryGame.pickedCards).toBeDefined();
expect(memoryGame.pickedCards instanceof Array).toBe(true);
expect(memoryGame.pickedCards.length).toBe(0);
});
it('should have a pairsClicked property starting off as 0', () => {
expect(memoryGame.pairsClicked).toBe(0);
});
it('should have a pairsGuessed property starting off as 0', () => {
expect(memoryGame.pairsGuessed).toBeDefined();
});
});
describe('shuffleCards method', () => {
beforeEach(() => {
memoryGame = new MemoryGame(cardsArray);
});
it('should be declared', () => {
expect(typeof memoryGame.shuffleCards).toBe('function');
});
it('should return undefined if argument (cards array) is not passed', () => {
expect(typeof new MemoryGame().shuffleCards()).toBe('undefined');
});
it('should return the shuffled (mixed) array of cards', () => {
const iterations = 100;
let isShuffled = false;
for (let i = 0; i < iterations; i++) {
const formerCards = [...memoryGame.cards];
memoryGame.shuffleCards();
const newCards = memoryGame.cards;
if (formerCards.some((card, index) => card.name !== newCards[index].name)) {
isShuffled = true;
break;
}
}
expect(isShuffled).toBe(true);
});
});
describe('checkIfPair method', () => {
it('should be declared', () => {
expect(typeof memoryGame.checkIfPair).toBe('function');
});
it('should add 1 to `pairsClicked` when we call it', () => {
memoryGame.checkIfPair('batman', 'ironman');
expect(memoryGame.pairsClicked).toBe(1);
});
it('should return true when comparing cards that are the same', () => {
expect(memoryGame.checkIfPair('ironman', 'ironman')).toBe(true);
});
it('should return false when the comparing cards are not the same', () => {
expect(memoryGame.checkIfPair('ironman', 'flash')).toBe(false);
});
it('should add 1 to pairsGuessed if they are the same card', () => {
memoryGame.pairsGuessed = 0;
memoryGame.checkIfPair('ironman', 'ironman');
expect(memoryGame.pairsGuessed).toBe(1);
});
it('should not add to pairsGuessed if the cards are not the same', () => {
memoryGame.pairsGuessed = 0;
memoryGame.checkIfPair('ironman', 'green lantern');
expect(memoryGame.pairsGuessed).toBe(0);
});
});
describe('checkIfFinished method', () => {
it('should be declared', () => {
expect(typeof memoryGame.checkIfFinished).toBe('function');
});
it('should return false at the beginning of the game', () => {
expect(memoryGame.checkIfFinished()).toBe(false);
});
it("should return false if there's still some pairs to be guessed", () => {
memoryGame.pairsGuessed = 4;
memoryGame.cards = new Array(16)
expect(memoryGame.checkIfFinished()).toBe(false);
});
it('should return true if all pairs are guessed', () => {
memoryGame.pairsGuessed = 8
memoryGame.pairsClicked = 1;
memoryGame.cards = new Array(16)
expect(memoryGame.checkIfFinished()).toBe(true);
});
});
});