@@ -5,33 +5,34 @@ describe('MaxHeap', () => {
5
5
expect ( typeof MaxHeap . prototype . constructor ) . toEqual ( 'function' ) ;
6
6
} ) ;
7
7
8
+ const mh = new MaxHeap ( ) ;
9
+
10
+ beforeEach ( ( ) => {
11
+ mh . destroy ( ) ;
12
+ } ) ;
13
+
8
14
it ( 'Should create an instance of MaxHeap' , ( ) => {
9
- const mh = new MaxHeap ( ) ;
10
15
expect ( mh instanceof MaxHeap ) . toEqual ( true ) ;
11
16
} ) ;
12
17
13
18
it ( 'Should add an element to the MaxHeap' , ( ) => {
14
- const mh = new MaxHeap ( ) ;
15
19
mh . add ( 10 ) ;
16
20
expect ( mh . getMax ( ) ) . toEqual ( 10 ) ;
17
21
} ) ;
18
22
19
23
it ( 'Should keep the largest element at the root' , ( ) => {
20
- const mh = new MaxHeap ( ) ;
21
24
[ 12 , 5 , 34 ] . forEach ( el => mh . add ( el ) ) ;
22
25
expect ( mh . getMax ( ) ) . toEqual ( 34 ) ;
23
26
} ) ;
24
27
25
28
it ( 'Should retain Heap properties after removal of an element' , ( ) => {
26
- const mh = new MaxHeap ( ) ;
27
29
[ 12 , 45 , 1 , 34 ] . forEach ( el => mh . add ( el ) ) ;
28
30
expect ( mh . getMax ( ) ) . toEqual ( 45 ) ;
29
31
mh . remove ( ) ;
30
32
expect ( mh . getMax ( ) ) . toEqual ( 34 ) ;
31
33
} ) ;
32
34
33
35
it ( 'Should return `null` when heap is empty' , ( ) => {
34
- const mh = new MaxHeap ( ) ;
35
36
[ 1 , 34 ] . forEach ( el => mh . add ( el ) ) ;
36
37
expect ( mh . getMax ( ) ) . toEqual ( 34 ) ;
37
38
mh . remove ( ) ;
@@ -40,7 +41,6 @@ describe('MaxHeap', () => {
40
41
} ) ;
41
42
42
43
it ( 'Should return the elelment value on `remove()`' , ( ) => {
43
- const mh = new MaxHeap ( ) ;
44
44
[ 1 , 34 ] . forEach ( el => mh . add ( el ) ) ;
45
45
expect ( mh . getMax ( ) ) . toEqual ( 34 ) ;
46
46
expect ( mh . remove ( ) ) . toEqual ( 34 ) ;
0 commit comments