1
- import { generateTestsFromFixtures } from 'relay-test-utils/lib/RelayModernTestUtils'
1
+ import {
2
+ GraphQLCompilerContext ,
3
+ IRTransforms ,
4
+ transformASTSchema ,
5
+ } from 'relay-compiler'
6
+ import { generateTestsFromFixtures } from 'relay-test-utils/lib/RelayModernTestUtils'
2
7
import * as RelayTestSchema from 'relay-test-utils/lib/RelayTestSchema'
3
8
import * as parseGraphQLText from 'relay-test-utils/lib/parseGraphQLText'
4
-
5
- import { GraphQLCompilerContext , IRTransforms , transformASTSchema } from 'relay-compiler'
6
-
7
9
import * as TypeScriptGenerator from '../src/TypeScriptGenerator'
8
10
9
11
function generate ( text , options ) {
@@ -37,17 +39,19 @@ function generate(text, options) {
37
39
username: String
38
40
}
39
41
` ,
40
- ] ) ;
41
- const { definitions} = parseGraphQLText ( schema , text ) ;
42
+ ] )
43
+ const { definitions } = parseGraphQLText ( schema , text )
42
44
return new GraphQLCompilerContext ( RelayTestSchema , schema )
43
45
. addAll ( definitions )
44
46
. applyTransforms ( TypeScriptGenerator . transforms )
45
47
. documents ( )
46
- . map ( doc => TypeScriptGenerator . generate ( doc , {
47
- ...options ,
48
- schema,
49
- } ) )
50
- . join ( '\n\n' ) ;
48
+ . map ( doc =>
49
+ TypeScriptGenerator . generate ( doc , {
50
+ ...options ,
51
+ schema,
52
+ } )
53
+ )
54
+ . join ( '\n\n' )
51
55
}
52
56
53
57
describe ( 'TypeScriptGenerator with a single artifact directory' , ( ) => {
@@ -60,9 +64,9 @@ describe('TypeScriptGenerator with a single artifact directory', () => {
60
64
relayRuntimeModule : 'relay-runtime' ,
61
65
useHaste : false ,
62
66
useSingleArtifactDirectory : true ,
63
- } ) ,
64
- ) ;
65
- } ) ;
67
+ } )
68
+ )
69
+ } )
66
70
67
71
describe ( 'TypeScriptGenerator without a single artifact directory' , ( ) => {
68
72
generateTestsFromFixtures ( `${ __dirname } /fixtures/type-generator` , text =>
@@ -74,16 +78,16 @@ describe('TypeScriptGenerator without a single artifact directory', () => {
74
78
relayRuntimeModule : 'relay-runtime' ,
75
79
useHaste : false ,
76
80
useSingleArtifactDirectory : false ,
77
- } ) ,
78
- ) ;
79
- } ) ;
81
+ } )
82
+ )
83
+ } )
80
84
81
85
describe ( 'Does not add `%future added values` when the noFutureProofEnums option is set' , ( ) => {
82
86
const text = `
83
87
fragment ScalarField on User {
84
88
traits
85
89
}
86
- ` ;
90
+ `
87
91
const types = generate ( text , {
88
92
customScalars : { } ,
89
93
enumsHasteModule : null ,
@@ -92,10 +96,40 @@ describe('Does not add `%future added values` when the noFutureProofEnums option
92
96
relayRuntimeModule : 'relay-runtime' ,
93
97
useHaste : false ,
94
98
noFutureProofEnums : true ,
95
- } ) ;
99
+ } )
96
100
97
101
// Without the option, PersonalityTraits would be `"CHEERFUL" | ... | "%future added value");`
98
102
expect ( types ) . toContain (
99
- 'export type PersonalityTraits = "CHEERFUL" | "DERISIVE" | "HELPFUL" | "SNARKY";' ,
100
- ) ;
101
- } ) ;
103
+ 'export type PersonalityTraits = "CHEERFUL" | "DERISIVE" | "HELPFUL" | "SNARKY";'
104
+ )
105
+ } )
106
+
107
+ describe . each `
108
+ mapping | type
109
+ ${ 'String' } | ${ 'string' }
110
+ ${ 'Url' } | ${ 'string' }
111
+ ${ 'ID' } | ${ 'string' }
112
+ ${ 'Int' } | ${ 'number' }
113
+ ${ 'Color' } | ${ 'Color' }
114
+ ${ '{}' } | ${ '{}' }
115
+ ${ '[]' } | ${ '[]' }
116
+ ` ( 'Custom scalar mapping $mapping to $type' , ( { mapping, type } ) => {
117
+ const text = `
118
+ fragment Test on User {
119
+ color
120
+ }
121
+ `
122
+ const types = generate ( text , {
123
+ customScalars : {
124
+ Color : mapping ,
125
+ } ,
126
+ enumsHasteModule : null ,
127
+ existingFragmentNames : new Set ( [ 'PhotoFragment' ] ) ,
128
+ optionalInputFields : [ ] ,
129
+ relayRuntimeModule : 'relay-runtime' ,
130
+ useHaste : false ,
131
+ useSingleArtifactDirectory : true ,
132
+ } )
133
+
134
+ expect ( types ) . toContain ( `color: ${ type } | null` )
135
+ } )
0 commit comments