1
+ const Code = require ( "@hapi/code" ) ;
2
+ const Lab = require ( "@hapi/lab" ) ;
3
+ const sinon = require ( "sinon" ) ;
4
+
5
+ const { expect } = Code ;
6
+ const lab = Lab . script ( ) ;
7
+ exports . lab = lab ;
8
+ const { suite, test, beforeEach, afterEach } = lab ;
9
+
10
+ class MockPreAwardApiClient {
11
+ public baseUrl : string ;
12
+ public wreck : any ;
13
+
14
+ constructor ( baseUrl : string , wreckClient : any ) {
15
+ this . baseUrl = baseUrl ;
16
+ this . wreck = wreckClient ;
17
+ }
18
+
19
+ async createOrUpdateForm ( formData : any ) : Promise < any > {
20
+ const url = `${ this . baseUrl } /forms` ;
21
+ return await this . wreck . post ( url , {
22
+ payload : JSON . stringify ( formData ) ,
23
+ headers : { "Content-Type" : "application/json" }
24
+ } ) ;
25
+ }
26
+
27
+ async getAllForms ( ) : Promise < any [ ] > {
28
+ const url = `${ this . baseUrl } /forms` ;
29
+ const response = await this . wreck . get ( url ) ;
30
+ return JSON . parse ( ( response . payload ) . toString ( ) ) ;
31
+ }
32
+
33
+ async getFormDraft ( formId : string ) : Promise < any > {
34
+ const url = `${ this . baseUrl } /forms/${ formId } ` ;
35
+ const response = await this . wreck . get ( url ) ;
36
+ return JSON . parse ( ( response . payload ) . toString ( ) ) ;
37
+ }
38
+ }
39
+
40
+ suite ( "PreAwardApiClient" , ( ) => {
41
+ let client ;
42
+ let wreckStub ;
43
+
44
+ beforeEach ( ( ) => {
45
+ wreckStub = {
46
+ post : sinon . stub ( ) ,
47
+ get : sinon . stub ( ) ,
48
+ } ;
49
+ client = new MockPreAwardApiClient ( "https://test-api.com" , wreckStub ) ;
50
+ } ) ;
51
+
52
+ afterEach ( ( ) => {
53
+ sinon . restore ( ) ;
54
+ } ) ;
55
+
56
+ suite ( "createOrUpdateForm" , ( ) => {
57
+ test ( "should successfully create a new form" , async ( ) => {
58
+ const mockResponse = { statusCode : 201 } ;
59
+ wreckStub . post . resolves ( mockResponse ) ;
60
+
61
+ const formData = {
62
+ name : "Test Form" ,
63
+ form_json : { pages : [ ] }
64
+ } ;
65
+ const result = await client . createOrUpdateForm ( formData ) ;
66
+
67
+ expect ( wreckStub . post . calledOnce ) . to . be . true ( ) ;
68
+ expect ( result ) . to . equal ( mockResponse ) ;
69
+ } ) ;
70
+
71
+ test ( "should handle network errors" , async ( ) => {
72
+ const networkError = new Error ( "Network timeout" ) ;
73
+ wreckStub . post . rejects ( networkError ) ;
74
+
75
+ try {
76
+ await client . createOrUpdateForm ( { name : "test-id" , form_json : { } } ) ;
77
+ expect . fail ( "Should have thrown a network error" ) ;
78
+ } catch ( err : any ) {
79
+ expect ( err . message ) . to . equal ( "Network timeout" ) ;
80
+ }
81
+ } ) ;
82
+ } ) ;
83
+
84
+ suite ( "getAllForms" , ( ) => {
85
+ test ( "should successfully retrieve all forms" , async ( ) => {
86
+ const mockForms = [
87
+ { id : "form-1" , name : "Application Form" } ,
88
+ { id : "form-2" , name : "Feedback Form" }
89
+ ] ;
90
+ const mockBuffer = Buffer . from ( JSON . stringify ( mockForms ) ) ;
91
+ wreckStub . get . resolves ( { payload : mockBuffer } ) ;
92
+
93
+ const result = await client . getAllForms ( ) ;
94
+
95
+ expect ( result ) . to . equal ( mockForms ) ;
96
+ expect ( result . length ) . to . equal ( 2 ) ;
97
+ } ) ;
98
+
99
+ test ( "should handle empty forms list" , async ( ) => {
100
+ const mockBuffer = Buffer . from ( JSON . stringify ( [ ] ) ) ;
101
+ wreckStub . get . resolves ( { payload : mockBuffer } ) ;
102
+
103
+ const result = await client . getAllForms ( ) ;
104
+
105
+ expect ( result ) . to . equal ( [ ] ) ;
106
+ } ) ;
107
+ } ) ;
108
+
109
+ suite ( "getFormDraft" , ( ) => {
110
+ test ( "should successfully retrieve a specific form" , async ( ) => {
111
+ const mockForm = {
112
+ id : "test-form" ,
113
+ name : "Test Form" ,
114
+ configuration : { pages : [ ] }
115
+ } ;
116
+ const mockBuffer = Buffer . from ( JSON . stringify ( mockForm ) ) ;
117
+ wreckStub . get . resolves ( { payload : mockBuffer } ) ;
118
+
119
+ const result = await client . getFormDraft ( "test-form" ) ;
120
+
121
+ expect ( result ) . to . equal ( mockForm ) ;
122
+ } ) ;
123
+
124
+ test ( "should handle form not found" , async ( ) => {
125
+ const notFoundError = new Error ( "Form not found" ) ;
126
+ ( notFoundError as any ) . statusCode = 404 ;
127
+ wreckStub . get . rejects ( notFoundError ) ;
128
+
129
+ try {
130
+ await client . getFormDraft ( "non-existent-form" ) ;
131
+ expect . fail ( "Should have thrown a not found error" ) ;
132
+ } catch ( err : any ) {
133
+ expect ( err . message ) . to . equal ( "Form not found" ) ;
134
+ }
135
+ } ) ;
136
+ } ) ;
137
+ } ) ;
0 commit comments