Skip to content

Commit 6a5e13a

Browse files
committed
refactor: added destroy(), used beforeEach()
1 parent 27544fc commit 6a5e13a

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/_DataStructures_/Heaps/MaxHeap/MaxHeap.test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,34 @@ describe('MaxHeap', () => {
55
expect(typeof MaxHeap.prototype.constructor).toEqual('function');
66
});
77

8+
const mh = new MaxHeap();
9+
10+
beforeEach(() => {
11+
mh.destroy();
12+
});
13+
814
it('Should create an instance of MaxHeap', () => {
9-
const mh = new MaxHeap();
1015
expect(mh instanceof MaxHeap).toEqual(true);
1116
});
1217

1318
it('Should add an element to the MaxHeap', () => {
14-
const mh = new MaxHeap();
1519
mh.add(10);
1620
expect(mh.getMax()).toEqual(10);
1721
});
1822

1923
it('Should keep the largest element at the root', () => {
20-
const mh = new MaxHeap();
2124
[12, 5, 34].forEach(el => mh.add(el));
2225
expect(mh.getMax()).toEqual(34);
2326
});
2427

2528
it('Should retain Heap properties after removal of an element', () => {
26-
const mh = new MaxHeap();
2729
[12, 45, 1, 34].forEach(el => mh.add(el));
2830
expect(mh.getMax()).toEqual(45);
2931
mh.remove();
3032
expect(mh.getMax()).toEqual(34);
3133
});
3234

3335
it('Should return `null` when heap is empty', () => {
34-
const mh = new MaxHeap();
3536
[1, 34].forEach(el => mh.add(el));
3637
expect(mh.getMax()).toEqual(34);
3738
mh.remove();
@@ -40,7 +41,6 @@ describe('MaxHeap', () => {
4041
});
4142

4243
it('Should return the elelment value on `remove()`', () => {
43-
const mh = new MaxHeap();
4444
[1, 34].forEach(el => mh.add(el));
4545
expect(mh.getMax()).toEqual(34);
4646
expect(mh.remove()).toEqual(34);

src/_DataStructures_/Heaps/MaxHeap/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ class MaxHeap {
6969
this.__traverseUpAndSwap(parent);
7070
}
7171
}
72+
73+
destroy() {
74+
this.heap = [];
75+
}
7276
}
7377

7478
module.exports = MaxHeap;

0 commit comments

Comments
 (0)