@@ -23,21 +23,30 @@ import {Plan} from '../../../shared/models';
23
23
describe ( 'BillingPlanComponent' , ( ) => {
24
24
let component : BillingPlanComponent ;
25
25
let fixture : ComponentFixture < BillingPlanComponent > ;
26
- let billingplanService : BillingPlanService ;
26
+ let mockBillingPlanService : jasmine . SpyObj < BillingPlanService > ;
27
27
let location : Location ;
28
28
let route : ActivatedRoute ;
29
29
let router : Router ;
30
30
31
- class MockBillingPlanService {
32
- getBillingDetails ( filter : BackendFilter < Plan > ) : Observable < AnyObject [ ] > {
33
- return of ( [ ] ) ;
34
- }
35
- getTotalBillingPlan ( ) : Observable < Count > {
36
- return of ( { count : 0 } ) ;
37
- }
38
- }
31
+ const mockBillingData = [
32
+ {
33
+ companyName : 'Test Company' ,
34
+ userName : 'John Doe' ,
35
+ planName : 'Premium' ,
36
+ startDate : '2024-01-01' ,
37
+ endDate : '2024-12-31' ,
38
+ status : SubscriptionStatus . ACTIVE ,
39
+ } ,
40
+ ] ;
39
41
40
42
beforeEach ( async ( ) => {
43
+ mockBillingPlanService = jasmine . createSpyObj ( 'BillingPlanService' , [
44
+ 'getPlanOptions' ,
45
+ 'getTotalPlan' ,
46
+ 'getBillingDetails' ,
47
+ 'getTotalBillingPlan' ,
48
+ ] ) ;
49
+
41
50
await TestBed . configureTestingModule ( {
42
51
declarations : [ BillingPlanComponent ] ,
43
52
imports : [ ThemeModule , RouterTestingModule , MainModule ] ,
@@ -47,15 +56,14 @@ describe('BillingPlanComponent', () => {
47
56
{ provide : Location , useValue : location } ,
48
57
{ provide : ActivatedRoute , useValue : route } ,
49
58
{ provide : Router , useValue : router } ,
50
- { provide : BillingPlanService , useClass : MockBillingPlanService } ,
59
+ { provide : BillingPlanService , useValue : mockBillingPlanService } ,
51
60
] ,
52
61
} ) . compileComponents ( ) ;
53
62
} ) ;
54
63
55
64
beforeEach ( ( ) => {
56
65
fixture = TestBed . createComponent ( BillingPlanComponent ) ;
57
66
component = fixture . componentInstance ;
58
- billingplanService = TestBed . inject ( BillingPlanService ) ;
59
67
location = TestBed . inject ( Location ) ;
60
68
route = TestBed . inject ( ActivatedRoute ) ;
61
69
router = TestBed . inject ( Router ) ;
@@ -74,15 +82,70 @@ describe('BillingPlanComponent', () => {
74
82
75
83
it ( 'should call getTotal and return count' , ( ) => {
76
84
const mockCount : Count = { count : 10 } ;
77
- spyOn ( billingplanService , 'getTotalBillingPlan' ) . and . returnValue (
78
- of ( mockCount ) ,
79
- ) ;
85
+ mockBillingPlanService . getTotalBillingPlan . and . returnValue ( of ( mockCount ) ) ;
80
86
81
87
component . getTotal ( ) . subscribe ( count => {
82
88
expect ( count ) . toEqual ( mockCount ) ;
83
89
} ) ;
84
90
} ) ;
85
91
92
+ it ( 'should define correct column definitions' , ( ) => {
93
+ expect ( component . colDefs . length ) . toBe ( 6 ) ;
94
+ expect ( component . colDefs ) . toContain (
95
+ jasmine . objectContaining ( {
96
+ field : 'companyName' ,
97
+ width : 200 ,
98
+ minWidth : 20 ,
99
+ } ) ,
100
+ ) ;
101
+ expect ( component . colDefs ) . toContain (
102
+ jasmine . objectContaining ( { field : 'userName' , width : 200 , minWidth : 20 } ) ,
103
+ ) ;
104
+ expect ( component . colDefs ) . toContain (
105
+ jasmine . objectContaining ( { field : 'planName' , width : 200 , minWidth : 20 } ) ,
106
+ ) ;
107
+ expect ( component . colDefs ) . toContain (
108
+ jasmine . objectContaining ( { field : 'startDate' , width : 200 , minWidth : 20 } ) ,
109
+ ) ;
110
+ expect ( component . colDefs ) . toContain (
111
+ jasmine . objectContaining ( { field : 'endDate' , width : 200 , minWidth : 20 } ) ,
112
+ ) ;
113
+ expect ( component . colDefs ) . toContain (
114
+ jasmine . objectContaining ( { field : 'status' , width : 200 , minWidth : 20 } ) ,
115
+ ) ;
116
+ } ) ;
117
+
118
+ it ( 'should get paginated billing plans' , done => {
119
+ const page = 1 ;
120
+ const limit = 5 ;
121
+
122
+ // Make sure the spy is properly configured before the test
123
+ mockBillingPlanService . getBillingDetails . and . returnValue (
124
+ of ( mockBillingData ) ,
125
+ ) ;
126
+
127
+ component . getPaginatedBillPlans ( page , limit ) . subscribe ( {
128
+ next : data => {
129
+ expect ( mockBillingPlanService . getBillingDetails ) . toHaveBeenCalledWith ( {
130
+ offset : limit * ( page - 1 ) ,
131
+ limit : limit ,
132
+ } ) ;
133
+ expect ( data [ 0 ] ) . toEqual ( {
134
+ companyName : mockBillingData [ 0 ] . companyName ,
135
+ userName : mockBillingData [ 0 ] . userName ,
136
+ planName : mockBillingData [ 0 ] . planName ,
137
+ startDate : mockBillingData [ 0 ] . startDate ,
138
+ endDate : mockBillingData [ 0 ] . endDate ,
139
+ status : SubscriptionStatus [ mockBillingData [ 0 ] . status ] ,
140
+ } ) ;
141
+ done ( ) ;
142
+ } ,
143
+ error : error => {
144
+ done . fail ( error ) ;
145
+ } ,
146
+ } ) ;
147
+ } ) ;
148
+
86
149
it ( 'should call getPaginatedBillPlans and return transformed data' , ( ) => {
87
150
const mockPlans = [
88
151
{
@@ -103,9 +166,7 @@ describe('BillingPlanComponent', () => {
103
166
} ,
104
167
] ;
105
168
106
- spyOn ( billingplanService , 'getBillingDetails' ) . and . returnValue (
107
- of ( mockPlans ) ,
108
- ) ;
169
+ mockBillingPlanService . getBillingDetails . and . returnValue ( of ( mockPlans ) ) ;
109
170
110
171
component . getPaginatedBillPlans ( 1 , component . limit ) . subscribe ( data => {
111
172
expect ( data ) . toEqual ( [
@@ -129,7 +190,7 @@ describe('BillingPlanComponent', () => {
129
190
} ) ;
130
191
} ) ;
131
192
it ( 'should handle errors in getPaginatedBillPlans' , ( ) => {
132
- spyOn ( billingplanService , ' getBillingDetails' ) . and . returnValue (
193
+ mockBillingPlanService . getBillingDetails . and . returnValue (
133
194
throwError ( 'Error' ) ,
134
195
) ;
135
196
0 commit comments