@@ -3,7 +3,7 @@ import { LinkedList } from './linked-list';
33describe ( 'Linked List' , ( ) => {
44 let list ;
55 beforeEach ( ( ) => {
6- list = new LinkedList
6+ list = new LinkedList ;
77 } ) ;
88
99 it ( 'should create a linked list object' , ( ) => {
@@ -12,4 +12,99 @@ describe('Linked List', () => {
1212 expect ( list . head ) . toBe ( null ) ;
1313 expect ( list . isEmpty ( ) ) . toBeTruthy ( ) ;
1414 } ) ;
15- } ) ;
15+
16+ it ( 'should insert first item in the linked list' , ( ) => {
17+ spyOn ( list , 'insertFirst' ) . and . callThrough ( ) ;
18+ list . insert ( 'firstVal' ) ;
19+
20+ expect ( list . insertFirst ) . toHaveBeenCalled ( ) ;
21+ expect ( list . insertFirst ) . toHaveBeenCalledWith ( 'firstVal' ) ;
22+ expect ( list . size ( ) ) . toBe ( 1 ) ;
23+ expect ( list . isEmpty ( ) ) . toBeFalsy ( ) ;
24+ expect ( list . head . key ) . toContain ( 'firstVal' ) ;
25+ expect ( list . head . next ) . toBe ( null ) ;
26+ } ) ;
27+
28+ describe ( 'When first item is inserted' , ( ) => {
29+ beforeEach ( ( ) => {
30+ list . insert ( 'firstVal' ) ;
31+ } ) ;
32+
33+ it ( 'should insert items at the end' , ( ) => {
34+ spyOn ( list , 'insertLast' ) . and . callThrough ( ) ;
35+ expect ( list . size ( ) ) . toBe ( 1 ) ;
36+ list . insert ( 'secondVal' ) ;
37+
38+ expect ( list . insertLast ) . toHaveBeenCalled ( ) ;
39+ expect ( list . insertLast ) . toHaveBeenCalledWith ( 'secondVal' ) ;
40+ expect ( list . size ( ) ) . toBe ( 2 ) ;
41+ expect ( list . head . next . key ) . toBe ( 'secondVal' ) ;
42+ expect ( list . head . next . next ) . toBe ( null ) ;
43+ } ) ;
44+
45+ it ( 'should insert item after specified item' , ( ) => {
46+ list . insertAfter ( 'secondVal' , 'firstVal' ) ;
47+ expect ( list . size ( ) ) . toBe ( 2 ) ;
48+ expect ( list . search ( 'secondVal' ) ) . toBeTruthy ( ) ;
49+
50+ list . insertAfter ( 'thirdVal' , 'xyz' ) ;
51+ expect ( list . size ( ) ) . toBe ( 2 ) ;
52+ expect ( list . search ( 'thirdVal' ) ) . toBeFalsy ( ) ;
53+
54+ list . insertAfter ( 'thirdVal' , 'firstVal' ) ;
55+ expect ( list . size ( ) ) . toBe ( 3 ) ;
56+ expect ( list . search ( 'thirdVal' ) ) . toBeTruthy ( ) ;
57+
58+ expect ( [ ...list ] [ 1 ] ) . toContain ( 'thirdVal' ) ;
59+ expect ( [ ...list ] [ 2 ] ) . toContain ( 'secondVal' ) ;
60+ } ) ;
61+
62+ it ( 'should insert item before specified item' , ( ) => {
63+ list . insertBefore ( 'secondVal' , 'firstVal' ) ;
64+ expect ( list . size ( ) ) . toBe ( 2 ) ;
65+ expect ( list . search ( 'secondVal' ) ) . toBeTruthy ( ) ;
66+
67+ list . insertBefore ( 'thirdVal' , 'firstVal' ) ;
68+ expect ( list . size ( ) ) . toBe ( 3 ) ;
69+ expect ( list . search ( 'thirdVal' ) ) . toBeTruthy ( ) ;
70+
71+ list . insertBefore ( 'forthVal' , 'xyz' ) ;
72+ expect ( list . size ( ) ) . toBe ( 3 ) ;
73+ expect ( list . search ( 'forthVal' ) ) . toBeFalsy ( ) ;
74+ } ) ;
75+ } ) ;
76+
77+ it ( 'should search for item' , ( ) => {
78+ expect ( list . search ( 'A' ) ) . toBeFalsy ( ) ;
79+
80+ list . insert ( 'A' ) ;
81+ list . insert ( 'B' ) ;
82+ list . insert ( 'C' ) ;
83+
84+ expect ( list . search ( 'B' ) ) . toBeTruthy ( ) ;
85+ expect ( list . search ( 'C' ) ) . toBeTruthy ( ) ;
86+ expect ( list . search ( 'D' ) ) . toBeFalsy ( ) ;
87+ } ) ;
88+
89+ it ( 'should remove items from list' , ( ) => {
90+ expect ( list . size ( ) ) . toBe ( 0 ) ;
91+
92+ list . insert ( 'A' ) ;
93+ list . insert ( 'B' ) ;
94+ list . insert ( 'C' ) ;
95+ expect ( list . size ( ) ) . toBe ( 3 ) ;
96+
97+ list . remove ( 'B' ) ;
98+ expect ( list . size ( ) ) . toBe ( 2 ) ;
99+ expect ( list . search ( 'B' ) ) . toBeFalsy ( ) ;
100+
101+ list . remove ( 'A' ) ;
102+ expect ( list . size ( ) ) . toBe ( 1 ) ;
103+ expect ( list . search ( 'A' ) ) . toBeFalsy ( ) ;
104+
105+ list . remove ( 'C' ) ;
106+ expect ( list . size ( ) ) . toBe ( 0 ) ;
107+ expect ( list . search ( 'C' ) ) . toBeFalsy ( ) ;
108+ } ) ;
109+
110+ } ) ;
0 commit comments