-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.test.js
77 lines (67 loc) · 1.96 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const Stack = require("./stack");
let stack;
beforeEach(function() {
stack = new Stack();
});
describe("push", function() {
it("places the value at the top of the stack and returns undefined", function() {
expect(stack.push(10)).toBe(undefined);
expect(stack.first.val).toBe(10);
expect(stack.last.val).toBe(10);
stack.push(100);
expect(stack.first.val).toBe(100);
expect(stack.last.val).toBe(10);
stack.push(1000);
expect(stack.first.val).toBe(1000);
expect(stack.last.val).toBe(10);
});
});
describe("pop", function() {
it("returns the value of the node removed", function() {
stack.push(10);
stack.push(100);
stack.push(1000);
var removed = stack.pop();
expect(removed).toBe(1000);
expect(stack.size).toBe(2);
stack.pop();
stack.pop();
expect(stack.size).toBe(0);
});
it("throws an error if the stack is empty", function() {
expect(() => stack.pop()).toThrow(Error);
});
});
describe("peek", function() {
it("returns the value at the start of the stack", function() {
stack.push(3);
expect(stack.peek()).toBe(3);
stack.push(5);
expect(stack.peek()).toBe(5);
});
});
describe("isEmpty", function() {
it("returns true for empty stacks", function() {
expect(stack.isEmpty()).toBe(true);
});
it("returns false for nonempty stacks", function() {
stack.push(3);
expect(stack.isEmpty()).toBe(false);
});
});
// PASS ./stack.test.js
// push
// ✓ places the value at the top of the stack and returns undefined (1ms)
// pop
// ✓ returns the value of the node removed (1ms)
// ✓ throws an error if the stack is empty (1ms)
// peek
// ✓ returns the value at the start of the stack (1ms)
// isEmpty
// ✓ returns true for empty stacks
// ✓ returns false for nonempty stacks
// Test Suites: 1 passed, 1 total
// Tests: 6 passed, 6 total
// Snapshots: 0 total
// Time: 0.617s, estimated 1s
// Ran all test suites matching /stack.test.js/i.