@@ -54,10 +54,10 @@ test("Generate a typescript interface", () => {
54
54
55
55
export interface Foo {
56
56
'@id'?: string;
57
- id?: string;
58
57
foo: any;
59
58
foobar?: FooBar;
60
59
readonly bar: string;
60
+ id?: string;
61
61
}
62
62
` ;
63
63
expect (
@@ -106,9 +106,67 @@ test("Generate a typescript interface without references to other interfaces", (
106
106
107
107
const res = `export interface Foo {
108
108
'@id'?: string;
109
+ foo: any;
110
+ readonly bar: string;
109
111
id?: string;
112
+ }
113
+ ` ;
114
+ expect (
115
+ fs . readFileSync ( tmpobj . name + "/interfaces/foo.ts" ) . toString ( )
116
+ ) . toEqual ( res ) ;
117
+
118
+ tmpobj . removeCallback ( ) ;
119
+ } ) ;
120
+
121
+ test ( "Generate a typescript interface with an explicit id field in the readableFields" , ( ) => {
122
+ const generator = new TypescriptInterfaceGenerator ( {
123
+ templateDirectory : `${ __dirname } /../../templates`
124
+ } ) ;
125
+ const tmpobj = tmp . dirSync ( { unsafeCleanup : true } ) ;
126
+
127
+ const resource = new Resource ( "abc" , "http://example.com/foos" , {
128
+ id : "foo" ,
129
+ title : "Foo" ,
130
+ readableFields : [
131
+ new Field ( "bar" , {
132
+ id : "http://schema.org/url" ,
133
+ range : "http://www.w3.org/2001/XMLSchema#string" ,
134
+ reference : null ,
135
+ required : true ,
136
+ description : "An URL"
137
+ } ) ,
138
+ new Field ( "id" , {
139
+ id : "http://schema.org/url" ,
140
+ range : "http://www.w3.org/2001/XMLSchema#string" ,
141
+ reference : null ,
142
+ required : false ,
143
+ description : "Id"
144
+ } )
145
+ ] ,
146
+ writableFields : [
147
+ new Field ( "foo" , {
148
+ id : "http://schema.org/url" ,
149
+ range : "http://www.w3.org/2001/XMLSchema#datetime" ,
150
+ reference : null ,
151
+ required : true ,
152
+ description : "An URL"
153
+ } )
154
+ ]
155
+ } ) ;
156
+ const api = new Api ( "http://example.com" , {
157
+ entrypoint : "http://example.com:8080" ,
158
+ title : "My API" ,
159
+ resources : [ resource ]
160
+ } ) ;
161
+ generator . generate ( api , resource , tmpobj . name ) ;
162
+
163
+ expect ( fs . existsSync ( tmpobj . name + "/interfaces/foo.ts" ) ) . toBe ( true ) ;
164
+
165
+ const res = `export interface Foo {
166
+ '@id'?: string;
110
167
foo: any;
111
168
readonly bar: string;
169
+ readonly id?: string;
112
170
}
113
171
` ;
114
172
expect (
0 commit comments