@@ -2,36 +2,90 @@ import { transformer } from '../src';
2
2
import * as ts from 'typescript' ;
3
3
import * as path from 'path' ;
4
4
import { Options } from '../src/Options' ;
5
+ import { generateTestsFromFixtures } from 'relay-test-utils/lib/RelayModernTestUtils'
5
6
6
- function transformWithOptions ( options : Options , contents : string , fileName : string ) : ts . TranspileOutput {
7
- return ts . transpileModule ( contents , {
8
- compilerOptions : {
9
- target : ts . ScriptTarget . ES5 ,
10
- sourceMap : true ,
11
- } ,
12
- fileName : fileName ,
13
- transformers : {
14
- before : [ transformer ( options ) ] ,
15
- } ,
16
- } ) ;
7
+ function transformWithOptions ( options : Options , fileName : string ) {
8
+ return ( text , providedFileName ?: string ) =>
9
+ ts . transpileModule ( text , {
10
+ compilerOptions : {
11
+ target : ts . ScriptTarget . ES2017 ,
12
+ jsx : ts . JsxEmit . Preserve ,
13
+ sourceMap : false ,
14
+ } ,
15
+ fileName : fileName ,
16
+ transformers : {
17
+ before : [ transformer ( options ) ] ,
18
+ } ,
19
+ } ) . outputText ;
17
20
}
18
21
19
- const schemaPath = path . resolve ( __dirname , 'testschema.rfc.graphql' ) ;
22
+ const schemaPath = path . resolve ( __dirname , 'testschema.graphql' ) ;
23
+ const oldSchemaPath = path . resolve ( __dirname , 'testschema.old.graphql' ) ;
20
24
21
25
describe ( 'TSTransform' , ( ) => {
22
- it ( 'Modern should compile' , async ( ) => {
23
- const text = 'createFragmentContainer(MyComponent, {todo: graphql`fragment MyFragment_todo on Node { id }`})' ;
26
+ generateTestsFromFixtures ( `${ __dirname } /fixtures-modern` , transformWithOptions ( { } , '/test/MyComponent.tsx' ) ) ;
24
27
25
- expect ( transformWithOptions ( { isDevVariable : 'IS_DEV' , artifactDirectory : '/testing/artifacts' } , text , '/test/MyComponent.ts' ) ) . toMatchSnapshot ( 'modern test' ) ;
26
- } ) ;
27
- it ( 'Compat should compile' , async ( ) => {
28
- const text = 'createFragmentContainer(MyComponent, {todo: graphql`fragment MyFragment_todo on Node { id }`})' ;
28
+ generateTestsFromFixtures (
29
+ `${ __dirname } /fixtures-compat` ,
30
+ transformWithOptions ( {
31
+ compat : true ,
32
+ schema : schemaPath ,
33
+ substituteVariables : true ,
34
+ } , '/test/MyComponent.tsx' ) ,
35
+ ) ;
29
36
30
- expect ( transformWithOptions ( { isDevVariable : 'IS_DEV' , artifactDirectory : '/testing/artifacts' , schema : schemaPath , compat : true } , text , '/test/MyComponent.ts' ) ) . toMatchSnapshot ( 'compat test' ) ;
31
- } ) ;
32
- it ( 'Classic should compile' , async ( ) => {
33
- const text = 'createFragmentContainer(MyComponent, {todo: () => Relay.QL`fragment on Node { id }`})' ;
37
+ generateTestsFromFixtures (
38
+ `${ __dirname } /fixtures-compat` ,
39
+ transformWithOptions ( {
40
+ compat : true ,
41
+ schema : schemaPath ,
42
+ substituteVariables : true ,
43
+ } , '/test/MyComponent.tsx' ) ,
44
+ ) ;
45
+
46
+ generateTestsFromFixtures (
47
+ `${ __dirname } /fixtures-classic` ,
48
+ transformWithOptions ( {
49
+ schema : oldSchemaPath ,
50
+ substituteVariables : true ,
51
+ } , '/test/MyComponent.tsx' ) ,
52
+ ) ;
53
+
54
+ describe ( '`development` option' , ( ) => {
55
+ it ( 'tests the hash when `development` is set' , ( ) => {
56
+ expect (
57
+ transformWithOptions ( { isDevelopment : true } , '/test/TestFrag.ts' ) (
58
+ 'graphql`fragment TestFrag on Node { id }`' ,
59
+ ) ,
60
+ ) . toMatchSnapshot ( ) ;
61
+ } ) ;
62
+
63
+ it ( 'tests the hash when `isDevVariable` is set' , ( ) => {
64
+ expect (
65
+ transformWithOptions ( { isDevVariable : 'IS_DEV' } , '/test/TestFrag.ts' ) (
66
+ 'graphql`fragment TestFrag on Node { id }`' ,
67
+ ) ,
68
+ ) . toMatchSnapshot ( ) ;
69
+ } ) ;
70
+
71
+ it ( 'uses a custom build command in message' , ( ) => {
72
+ expect (
73
+ transformWithOptions (
74
+ {
75
+ buildCommand : 'relay-build' ,
76
+ isDevelopment : true ,
77
+ } ,
78
+ '/test/TestFrag.ts' ,
79
+ ) ( 'graphql`fragment TestFrag on Node { id }`' ) ,
80
+ ) . toMatchSnapshot ( ) ;
81
+ } ) ;
34
82
35
- expect ( transformWithOptions ( { isDevVariable : 'IS_DEV' , artifactDirectory : '/testing/artifacts' , schema : schemaPath } , text , '/test/MyComponent.ts' ) ) . toMatchSnapshot ( 'classic test' ) ;
83
+ it ( 'does not test the hash when `development` is not set' , ( ) => {
84
+ expect (
85
+ transformWithOptions ( { } , '/test/TestFrag.ts' ) (
86
+ 'graphql`fragment TestFrag on Node { id }`' ,
87
+ ) ,
88
+ ) . toMatchSnapshot ( ) ;
89
+ } ) ;
36
90
} ) ;
37
91
} ) ;
0 commit comments