11const LinkedList = require ( './linkedlist' ) ;
22
33describe ( 'LinkedList' , function ( ) {
4+ let list ;
45
56 describe ( '.addLast' , function ( ) {
6-
7- it ( 'should add elements to the tail and count size' , function ( ) {
8- const list = new LinkedList ( ) ;
7+ beforeEach ( function ( ) {
8+ list = new LinkedList ( ) ;
99 list . addLast ( 1 ) ;
1010 list . addLast ( 2 ) ;
1111 list . addLast ( 3 ) ;
1212 list . addLast ( 4 ) ;
13+ } ) ;
1314
15+ it ( 'should add elements to the tail and count size' , function ( ) {
1416 expect ( list . toString ( ) ) . toEqual ( '1 -> 2 -> 3 -> 4' ) ;
17+ } ) ;
18+
19+ it ( 'should have a size of 4' , function ( ) {
1520 expect ( list . size ( ) ) . toBe ( 4 ) ;
1621 } ) ;
22+ } ) ;
1723
18- it ( 'should add elements to the head and count size' , function ( ) {
19- const list = new LinkedList ( ) ;
24+ describe ( '.addFirst' , function ( ) {
25+ beforeEach ( function ( ) {
26+ list = new LinkedList ( ) ;
2027 list . addFirst ( 1 ) ;
2128 list . addFirst ( 2 ) ;
2229 list . addFirst ( 3 ) ;
2330 list . addFirst ( 4 ) ;
31+ } ) ;
2432
33+ it ( 'should add elements to the tail and count size' , function ( ) {
2534 expect ( list . toString ( ) ) . toEqual ( '4 -> 3 -> 2 -> 1' ) ;
35+ } ) ;
36+
37+ it ( 'should have a size of 4' , function ( ) {
2638 expect ( list . size ( ) ) . toBe ( 4 ) ;
27- } )
39+ } ) ;
2840 } ) ;
41+
42+ describe ( '.add (addFirst)' , function ( ) {
43+ beforeEach ( function ( ) {
44+ list = new LinkedList ( ) ;
45+ list . add ( 1 ) ;
46+ list . add ( 2 ) ;
47+ list . add ( 3 ) ;
48+ list . add ( 4 ) ;
49+ } ) ;
50+
51+ it ( 'should add elements to the tail and count size' , function ( ) {
52+ expect ( list . toString ( ) ) . toEqual ( '4 -> 3 -> 2 -> 1' ) ;
53+ } ) ;
54+
55+ it ( 'should have a size of 4' , function ( ) {
56+ expect ( list . size ( ) ) . toBe ( 4 ) ;
57+ } ) ;
58+ } ) ;
59+
60+ describe ( '.delete' , function ( ) {
61+ beforeEach ( function ( ) {
62+ list = new LinkedList ( ) ;
63+ list . add ( 1 ) ;
64+ list . add ( 2 ) ;
65+ list . add ( 3 ) ;
66+ list . add ( 4 ) ;
67+ } ) ;
68+
69+ it ( 'should start with size of 3 after deleting one' , function ( ) {
70+ list . delete ( 4 ) ;
71+ expect ( list . size ( ) ) . toBe ( 3 ) ;
72+ } ) ;
73+
74+ it ( 'should have a size of 4' , function ( ) {
75+ list . delete ( 4 ) ;
76+ list . delete ( 2 ) ;
77+ list . delete ( 1 ) ;
78+ list . delete ( 3 ) ;
79+ expect ( list . size ( ) ) . toBe ( 0 ) ;
80+ } ) ;
81+
82+ it ( 'should not change if element not found' , function ( ) {
83+ list . delete ( 7 ) ;
84+ expect ( list . size ( ) ) . toBe ( 4 ) ;
85+ } ) ;
86+ } ) ;
87+
2988} ) ;
0 commit comments