1
1
import assert from 'assert' ;
2
- import { Project , QuoteKind , SourceFile } from 'ts-morph' ;
2
+ import {
3
+ Node ,
4
+ ObjectLiteralExpression ,
5
+ Project ,
6
+ PropertyDeclaration ,
7
+ QuoteKind ,
8
+ SourceFile ,
9
+ } from 'ts-morph' ;
3
10
4
11
import { generateModel } from './generate-model' ;
5
12
import { generatorOptions , stringContains , stringNotContains } from './testing' ;
@@ -20,11 +27,11 @@ describe('generate models', () => {
20
27
const [ model ] = models ;
21
28
sourceFile = project . createSourceFile ( '_.ts' , sourceFileText ) ;
22
29
generateModel ( { model, sourceFile, projectFilePath : ( ) => '_.ts' } ) ;
23
- return sourceFile ;
30
+ sourceText = sourceFile . getText ( ) ;
24
31
}
25
32
26
33
it ( 'model' , async ( ) => {
27
- sourceFile = await getResult ( `model User {
34
+ await getResult ( `model User {
28
35
id String @id
29
36
}` ) ;
30
37
sourceText = sourceFile . getText ( ) ;
@@ -34,7 +41,7 @@ describe('generate models', () => {
34
41
} ) ;
35
42
36
43
it ( 'field nullable' , async ( ) => {
37
- sourceFile = await getResult ( `model User {
44
+ await getResult ( `model User {
38
45
id Int @id
39
46
image String?
40
47
}` ) ;
@@ -47,7 +54,7 @@ describe('generate models', () => {
47
54
} ) ;
48
55
49
56
it ( 'default value' , async ( ) => {
50
- sourceFile = await getResult ( `model User {
57
+ await getResult ( `model User {
51
58
count Int @id @default(1)
52
59
}` ) ;
53
60
const sourceText = sourceFile . getText ( ) ;
@@ -58,7 +65,7 @@ describe('generate models', () => {
58
65
} ) ;
59
66
60
67
it ( 'self relation' , async ( ) => {
61
- sourceFile = await getResult ( `
68
+ await getResult ( `
62
69
model User {
63
70
id String @id
64
71
following User[] @relation("UserFollows", references: [id])
@@ -68,7 +75,7 @@ describe('generate models', () => {
68
75
} ) ;
69
76
70
77
it ( 'extend existing class' , async ( ) => {
71
- sourceFile = await getResult (
78
+ await getResult (
72
79
`model User {
73
80
id String @id
74
81
}` ,
@@ -79,7 +86,7 @@ describe('generate models', () => {
79
86
} ) ;
80
87
81
88
it ( 'object type description' , async ( ) => {
82
- sourceFile = await getResult (
89
+ await getResult (
83
90
`/// User really
84
91
model User {
85
92
id Int @id
@@ -90,7 +97,7 @@ describe('generate models', () => {
90
97
} ) ;
91
98
92
99
it ( 'property description' , async ( ) => {
93
- sourceFile = await getResult (
100
+ await getResult (
94
101
`model User {
95
102
/// user id
96
103
id Int @id
@@ -104,7 +111,7 @@ describe('generate models', () => {
104
111
} ) ;
105
112
106
113
it ( 'update description to undefined' , async ( ) => {
107
- sourceFile = await getResult (
114
+ await getResult (
108
115
`model User {
109
116
id String @id
110
117
}` ,
@@ -117,7 +124,7 @@ describe('generate models', () => {
117
124
} ) ;
118
125
119
126
it ( 'model import scalar types' , async ( ) => {
120
- sourceFile = await getResult ( `model User {
127
+ await getResult ( `model User {
121
128
id String @id
122
129
count Int
123
130
money Float
@@ -131,7 +138,6 @@ describe('generate models', () => {
131
138
. flatMap ( ( x ) => x . getNamedImports ( ) )
132
139
. map ( ( x ) => x . getName ( ) ) ,
133
140
) ;
134
- sourceText = sourceFile . getText ( ) ;
135
141
stringContains (
136
142
'@Field(() => Boolean, { nullable: false, description: undefined }) humanoid!: boolean' ,
137
143
sourceText ,
@@ -154,4 +160,24 @@ describe('generate models', () => {
154
160
assert ( imports . has ( 'Int' ) === true , 'Imports should includes Int' ) ;
155
161
assert ( imports . has ( 'Float' ) === true , 'Imports should includes Float' ) ;
156
162
} ) ;
163
+
164
+ it ( 'model scalar json' , async ( ) => {
165
+ await getResult ( `model User {
166
+ id String @id
167
+ data Json
168
+ }` ) ;
169
+ sourceText = sourceFile . getText ( ) ;
170
+ const propertyDeclaration = sourceFile . getClass ( 'User' ) ?. getProperty ( 'data' ) ;
171
+ assert ( propertyDeclaration ) ;
172
+ stringContains ( `@Field(() => GraphQLJSON` , propertyDeclaration . getText ( ) ) ;
173
+
174
+ const importDeclaration = sourceFile . getImportDeclaration (
175
+ ( d ) => d . getModuleSpecifier ( ) . getLiteralValue ( ) === 'graphql-type-json' ,
176
+ ) ;
177
+ assert ( importDeclaration , 'import graphql-type-json should exists' ) ;
178
+ const importSpecifier = importDeclaration
179
+ . getNamedImports ( )
180
+ . find ( ( x ) => x . getName ( ) === 'GraphQLJSON' ) ;
181
+ assert ( importSpecifier , 'const GraphQLJSON should be imported' ) ;
182
+ } ) ;
157
183
} ) ;
0 commit comments