@@ -2,14 +2,16 @@ import Seller from '../../src/models/Seller';
2
2
import SellerItem from '../../src/models/SellerItem' ;
3
3
import {
4
4
getAllSellers ,
5
+ registerOrUpdateSeller ,
5
6
getAllSellerItems ,
6
7
addOrUpdateSellerItem ,
7
8
deleteSellerItem ,
8
9
getSellersWithinSanctionedRegion
9
10
} from '../../src/services/seller.service' ;
11
+ import User from '../../src/models/User' ;
10
12
import { SellerType } from '../../src/models/enums/sellerType' ;
11
13
import { RestrictedArea , RestrictedAreaBoundaries } from '../../src/models/enums/restrictedArea' ;
12
- import { ISanctionedRegion , ISeller , ISellerItem } from '../../src/types' ;
14
+ import { IUser , ISeller , ISellerItem , ISanctionedRegion } from '../../src/types' ;
13
15
14
16
describe ( 'getAllSellers function' , ( ) => {
15
17
const mockBoundingBox = {
@@ -93,6 +95,114 @@ describe('getAllSellers function', () => {
93
95
} ) ;
94
96
} ) ;
95
97
98
+ describe ( 'registerOrUpdateSeller function' , ( ) => {
99
+ // Helper function to convert Mongoose document to a plain object and normalize values accordingly
100
+ const convertToPlainObject = ( seller : ISeller ) : any => {
101
+ const plainObject = seller . toObject ( ) ;
102
+
103
+ if ( plainObject . sell_map_center ) {
104
+ plainObject . sell_map_center = JSON . stringify ( plainObject . sell_map_center ) ;
105
+ }
106
+
107
+ if ( plainObject . average_rating ) {
108
+ plainObject . average_rating = plainObject . average_rating . toString ( ) ;
109
+ }
110
+
111
+ return plainObject ;
112
+ } ;
113
+
114
+ const assertSeller = ( actual : any , expected : any ) => {
115
+ const { _id, __v, createdAt, updatedAt, order_online_enabled_pref, ...filteredActual } = actual ; // ignore DB values.
116
+ expect ( filteredActual ) . toEqual ( expect . objectContaining ( expected ) ) ;
117
+ } ;
118
+
119
+ it ( 'should add new seller if the seller does not exist' , async ( ) => {
120
+ const userData = await User . findOne ( { pi_username : 'TestUser13' } ) as IUser ;
121
+
122
+ const formData = {
123
+ seller_id : "0m0m0m-0m0m-0m0m" ,
124
+ name : 'Test Seller 13' ,
125
+ description : "Test Seller 13 Description" ,
126
+ address : "Test Seller 13 Address" ,
127
+ image : "http://example.com/testThirteen.jpg" ,
128
+ seller_type : "activeSeller" ,
129
+ sell_map_center : JSON . stringify ( {
130
+ type : "Point" ,
131
+ coordinates : [ 24.1234 , 24.1234 ]
132
+ } ) ,
133
+ average_rating : "5" ,
134
+ fulfillment_method : "Delivered to buyer" ,
135
+ fulfillment_description : "Test Seller 13 Fulfillment Description"
136
+ } as unknown as ISeller ;
137
+
138
+ const sellerData = ( await registerOrUpdateSeller ( userData , formData ) ) as ISeller ;
139
+
140
+ // Convert `sellerData` to a plain object if it's a Mongoose document
141
+ const plainObject = await convertToPlainObject ( sellerData ) ;
142
+
143
+ assertSeller ( plainObject , {
144
+ seller_id : formData . seller_id ,
145
+ name : formData . name ,
146
+ description : formData . description ,
147
+ address : formData . address ,
148
+ image : formData . image ,
149
+ seller_type : formData . seller_type ,
150
+ sell_map_center : formData . sell_map_center ,
151
+ average_rating : formData . average_rating ,
152
+ fulfillment_method : formData . fulfillment_method ,
153
+ fulfillment_description : formData . fulfillment_description
154
+ } ) ;
155
+ } ) ;
156
+
157
+ it ( 'should update existing seller if the seller does exist' , async ( ) => {
158
+ const userData = await User . findOne ( { pi_username : 'TestUser3' } ) as IUser ;
159
+
160
+ const formData = {
161
+ seller_id : "0c0c0c-0c0c-0c0c" ,
162
+ name : 'Test Vendor 3 Updated' ,
163
+ description : "Test Vendor 3 Description Updated" ,
164
+ address : "Test Vendor 3 Address Updated" ,
165
+ fulfillment_method : "Delivered to buyer" ,
166
+ fulfillment_description : "Test Vendor 3 Fulfillment Description"
167
+ } as unknown as ISeller ;
168
+
169
+ const sellerData = ( await registerOrUpdateSeller ( userData , formData ) ) as ISeller ;
170
+
171
+ // Convert `sellerData` to a plain object if it's a Mongoose document
172
+ const plainObject = await convertToPlainObject ( sellerData ) ;
173
+
174
+ assertSeller ( plainObject , {
175
+ seller_id : formData . seller_id ,
176
+ name : formData . name ,
177
+ description : formData . description ,
178
+ fulfillment_method : formData . fulfillment_method ,
179
+ fulfillment_description : formData . fulfillment_description
180
+ } ) ;
181
+ } ) ;
182
+
183
+ it ( 'should throw an error when an exception occurs' , async ( ) => {
184
+ const userData = await User . findOne ( { pi_username : 'TestUser3' } ) as IUser ;
185
+
186
+ const formData = {
187
+ seller_id : "0c0c0c-0c0c-0c0c" ,
188
+ name : 'Test Vendor 3 Updated' ,
189
+ description : "Test Vendor 3 Description Updated" ,
190
+ address : "Test Vendor 3 Address Updated" ,
191
+ fulfillment_method : "Delivered to buyer" ,
192
+ fulfillment_description : "Test Vendor 3 Fulfillment Description"
193
+ } as unknown as ISeller ;
194
+
195
+ // Mock the Seller model to throw an error
196
+ jest . spyOn ( Seller , 'findOne' ) . mockImplementationOnce ( ( ) => {
197
+ throw new Error ( 'Mock database error' ) ;
198
+ } ) ;
199
+
200
+ await expect ( registerOrUpdateSeller ( userData , formData ) ) . rejects . toThrow (
201
+ 'Failed to register or update seller; please try again later'
202
+ ) ;
203
+ } ) ;
204
+ } ) ;
205
+
96
206
describe ( 'getAllSellerItems function' , ( ) => {
97
207
it ( 'should return all existing seller items associated with the seller' , async ( ) => {
98
208
const sellerItemsData = await getAllSellerItems ( '0a0a0a-0a0a-0a0a' ) ;
0 commit comments