-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathStack.test.js
57 lines (46 loc) · 1.3 KB
/
Stack.test.js
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
const Stack = require('.');
describe('Data Structure : Stack', () => {
it('Should be class', () => {
expect(typeof Stack.prototype.constructor).toEqual('function');
});
describe('Stack API', () => {
let stack = null;
beforeEach(() => {
stack = new Stack();
});
it('Should add() element to a stack', () => {
stack.push(5);
expect(stack.data).toEqual([5]);
});
it('Should remove() an element from the stack', () => {
stack.push(2);
stack.push(3);
expect(stack.pop()).toEqual(3);
expect(stack.data).toEqual([2]);
});
describe('peek()', () => {
beforeEach(() => {
stack.push(2);
stack.push(5);
});
it('Should return the elemet to be removed using peek()', () => {
expect(stack.peek()).toEqual(5);
});
it('Should not remove the element', () => {
expect(stack.peek()).toEqual(5);
expect(stack.pop()).toEqual(5);
});
});
it('Should maintain the FILO order of elements', () => {
// first in last out
stack.push(2);
stack.push(1);
stack.push(4);
stack.push(3);
expect(stack.pop()).toEqual(3);
expect(stack.pop()).toEqual(4);
expect(stack.pop()).toEqual(1);
expect(stack.pop()).toEqual(2);
});
});
});