@@ -54,10 +54,10 @@ test("Generate a typescript interface", () => {
5454
5555export interface Foo {
5656 '@id'?: string;
57- id?: string;
5857 foo: any;
5958 foobar?: FooBar;
6059 readonly bar: string;
60+ id?: string;
6161}
6262` ;
6363 expect (
@@ -106,9 +106,67 @@ test("Generate a typescript interface without references to other interfaces", (
106106
107107 const res = `export interface Foo {
108108 '@id'?: string;
109+ foo: any;
110+ readonly bar: string;
109111 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;
110167 foo: any;
111168 readonly bar: string;
169+ readonly id?: string;
112170}
113171` ;
114172 expect (
0 commit comments