@@ -76,15 +76,12 @@ describe("Create Customer", () => {
7676
7777describe ( "List Customers By Name" , ( ) => {
7878 let mockServer : mockttp . Mockttp ;
79- let apiClient : VendorPortalApi ;
79+ const apiClient = new VendorPortalApi ( ) ;
80+ apiClient . apiToken = "abcd1234" ;
8081
8182 beforeEach ( async ( ) => {
8283 mockServer = mockttp . getLocal ( ) ;
8384 await mockServer . start ( ) ;
84- // Ensure server is fully ready
85- await new Promise ( resolve => setTimeout ( resolve , 5 ) ) ;
86- apiClient = new VendorPortalApi ( ) ;
87- apiClient . apiToken = "abcd1234" ;
8885 apiClient . endpoint = "http://localhost:" + mockServer . port ;
8986 } ) ;
9087
@@ -95,7 +92,6 @@ describe("List Customers By Name", () => {
9592 it ( "should return customers matching the name" , async ( ) => {
9693 const appId = "test-app-1" ;
9794 const appSlug = "test-app-1" ;
98- const customerName = "UniqueCustomerName123" ;
9995 const expectedApplications = {
10096 apps : [
10197 { id : appId , name : "Test App 1" , slug : appSlug } ,
@@ -104,75 +100,64 @@ describe("List Customers By Name", () => {
104100 } ;
105101 const customersResponse = {
106102 customers : [
107- { id : "customer-1" , name : customerName } ,
108- { id : "customer-2" , name : customerName + " Two" }
103+ { id : "customer-1" , name : "Test Customer" } ,
104+ { id : "customer-2" , name : "Test Customer Two" }
109105 ]
110106 } ;
111107
112- const appsMock = mockServer . forGet ( "/apps" ) . thenReply ( 200 , JSON . stringify ( expectedApplications ) ) ;
113- const customersMock = mockServer . forGet ( `/app/${ appId } /customers` ) . withQuery ( { name : customerName } ) . once ( ) . thenReply ( 200 , JSON . stringify ( customersResponse ) ) ;
114-
115- await appsMock ;
116- await customersMock ;
108+ await mockServer . forGet ( "/apps" ) . once ( ) . thenReply ( 200 , JSON . stringify ( expectedApplications ) ) ;
109+ await mockServer . forGet ( `/app/${ appId } /customers` ) . withQuery ( { name : "Test Customer" } ) . once ( ) . thenReply ( 200 , JSON . stringify ( customersResponse ) ) ;
117110
118- const customers : CustomerSummary [ ] = await listCustomersByName ( apiClient , appSlug , customerName ) ;
111+ const customers : CustomerSummary [ ] = await listCustomersByName ( apiClient , appSlug , "Test Customer" ) ;
119112 expect ( customers ) . toHaveLength ( 2 ) ;
120- expect ( customers [ 0 ] . name ) . toEqual ( customerName ) ;
113+ expect ( customers [ 0 ] . name ) . toEqual ( "Test Customer" ) ;
121114 expect ( customers [ 0 ] . customerId ) . toEqual ( "customer-1" ) ;
122- expect ( customers [ 1 ] . name ) . toEqual ( customerName + " Two") ;
115+ expect ( customers [ 1 ] . name ) . toEqual ( "Test Customer Two") ;
123116 expect ( customers [ 1 ] . customerId ) . toEqual ( "customer-2" ) ;
124117 } ) ;
125118
126119 it ( "should return empty array when no customers match" , async ( ) => {
127- const appId = "test-app-empty" ;
128- const appSlug = "test-app-empty" ;
129120 const expectedApplications = {
130- apps : [ { id : appId , name : "Test App Empty " , slug : appSlug } ]
121+ apps : [ { id : "1234abcd" , name : "App 1 " , slug : "app-1" } ]
131122 } ;
132123 const customersResponse = {
133124 customers : [ ]
134125 } ;
135126
136127 await mockServer . forGet ( "/apps" ) . once ( ) . thenReply ( 200 , JSON . stringify ( expectedApplications ) ) ;
137- await mockServer . forGet ( ` /app/${ appId } /customers` ) . withQuery ( { name : "NonExistent" } ) . once ( ) . thenReply ( 200 , JSON . stringify ( customersResponse ) ) ;
128+ await mockServer . forGet ( " /app/1234abcd /customers" ) . withQuery ( { name : "NonExistent" } ) . once ( ) . thenReply ( 200 , JSON . stringify ( customersResponse ) ) ;
138129
139- const customers : CustomerSummary [ ] = await listCustomersByName ( apiClient , appSlug , "NonExistent" ) ;
130+ const customers : CustomerSummary [ ] = await listCustomersByName ( apiClient , "app-1" , "NonExistent" ) ;
140131 expect ( customers ) . toHaveLength ( 0 ) ;
141132 } ) ;
142133
143134 it ( "should return empty array when customers field is undefined" , async ( ) => {
144135 const appId = "test-app-2" ;
145136 const appSlug = "test-app-2" ;
146- const customerName = "UndefinedFieldTest456" ;
147137 const expectedApplications = {
148138 apps : [ { id : appId , name : "Test App 2" , slug : appSlug } ]
149139 } ;
150140 const customersResponse = { } ;
151141
152- const appsMock = mockServer . forGet ( "/apps" ) . thenReply ( 200 , JSON . stringify ( expectedApplications ) ) ;
153- const customersMock = mockServer . forGet ( `/app/${ appId } /customers` ) . withQuery ( { name : customerName } ) . once ( ) . thenReply ( 200 , JSON . stringify ( customersResponse ) ) ;
154-
155- await appsMock ;
156- await customersMock ;
142+ await mockServer . forGet ( "/apps" ) . once ( ) . thenReply ( 200 , JSON . stringify ( expectedApplications ) ) ;
143+ await mockServer . forGet ( `/app/${ appId } /customers` ) . withQuery ( { name : "Test" } ) . once ( ) . thenReply ( 200 , JSON . stringify ( customersResponse ) ) ;
157144
158- const customers : CustomerSummary [ ] = await listCustomersByName ( apiClient , appSlug , customerName ) ;
145+ const customers : CustomerSummary [ ] = await listCustomersByName ( apiClient , appSlug , "Test" ) ;
159146 expect ( customers ) . toHaveLength ( 0 ) ;
160147 } ) ;
161148
162149 it ( "should properly URL encode customer name" , async ( ) => {
163- const appId = "test-app-encode" ;
164- const appSlug = "test-app-encode" ;
165150 const expectedApplications = {
166- apps : [ { id : appId , name : "Test App Encode " , slug : appSlug } ]
151+ apps : [ { id : "1234abcd" , name : "App 1 " , slug : "app-1" } ]
167152 } ;
168153 const customersResponse = {
169154 customers : [ { id : "customer-1" , name : "Customer & Co" } ]
170155 } ;
171156
172157 await mockServer . forGet ( "/apps" ) . once ( ) . thenReply ( 200 , JSON . stringify ( expectedApplications ) ) ;
173- await mockServer . forGet ( ` /app/${ appId } /customers` ) . withQuery ( { name : "Customer & Co" } ) . once ( ) . thenReply ( 200 , JSON . stringify ( customersResponse ) ) ;
158+ await mockServer . forGet ( " /app/1234abcd /customers" ) . withQuery ( { name : "Customer & Co" } ) . once ( ) . thenReply ( 200 , JSON . stringify ( customersResponse ) ) ;
174159
175- const customers : CustomerSummary [ ] = await listCustomersByName ( apiClient , appSlug , "Customer & Co" ) ;
160+ const customers : CustomerSummary [ ] = await listCustomersByName ( apiClient , "app-1" , "Customer & Co" ) ;
176161 expect ( customers ) . toHaveLength ( 1 ) ;
177162 expect ( customers [ 0 ] . name ) . toEqual ( "Customer & Co" ) ;
178163 expect ( customers [ 0 ] . customerId ) . toEqual ( "customer-1" ) ;
@@ -181,21 +166,17 @@ describe("List Customers By Name", () => {
181166 it ( "should throw error when API returns non-200 status" , async ( ) => {
182167 const appId = "test-app-3" ;
183168 const appSlug = "test-app-3" ;
184- const customerName = "ErrorTest789" ;
185169 const expectedApplications = {
186170 apps : [ { id : appId , name : "Test App 3" , slug : appSlug } ]
187171 } ;
188172
189- const appsMock = mockServer . forGet ( "/apps" ) . thenReply ( 200 , JSON . stringify ( expectedApplications ) ) ;
190- const customersMock = mockServer
173+ await mockServer . forGet ( "/apps" ) . once ( ) . thenReply ( 200 , JSON . stringify ( expectedApplications ) ) ;
174+ await mockServer
191175 . forGet ( `/app/${ appId } /customers` )
192- . withQuery ( { name : customerName } )
176+ . withQuery ( { name : "Test" } )
193177 . once ( )
194178 . thenReply ( 500 , JSON . stringify ( { error : "Internal Server Error" } ) ) ;
195179
196- await appsMock ;
197- await customersMock ;
198-
199- await expect ( listCustomersByName ( apiClient , appSlug , customerName ) ) . rejects . toThrow ( "Failed to list customers: Server responded with 500" ) ;
180+ await expect ( listCustomersByName ( apiClient , appSlug , "Test" ) ) . rejects . toThrow ( "Failed to list customers: Server responded with 500" ) ;
200181 } ) ;
201182} ) ;
0 commit comments