1
- " use strict" ;
1
+ ' use strict'
2
2
3
- const mocker = require ( " easygraphql-mock" ) ;
4
- const schemaParser = require ( " easygraphql-parser" ) ;
5
- const assert = require ( " assert" ) ;
6
- const isObject = require ( " lodash.isobject" ) ;
7
- const { graphql, print } = require ( " graphql" ) ;
8
- const { makeExecutableSchema } = require ( " graphql-tools" ) ;
3
+ const mocker = require ( ' easygraphql-mock' )
4
+ const schemaParser = require ( ' easygraphql-parser' )
5
+ const assert = require ( ' assert' )
6
+ const isObject = require ( ' lodash.isobject' )
7
+ const { graphql, print } = require ( ' graphql' )
8
+ const { makeExecutableSchema } = require ( ' graphql-tools' )
9
9
10
- const buildGraphQLSchema = require ( " ../utils/buildGraphQLSchema" ) ;
10
+ const buildGraphQLSchema = require ( ' ../utils/buildGraphQLSchema' )
11
11
12
- const mock = require ( " ../utils/mock" ) ;
12
+ const mock = require ( ' ../utils/mock' )
13
13
14
14
class Tester {
15
15
constructor ( schema , resolvers ) {
16
16
if ( ! schema ) {
17
- throw new Error ( " The schema is require" ) ;
17
+ throw new Error ( ' The schema is require' )
18
18
}
19
19
20
- let buildedSchema = schema ;
20
+ let buildedSchema = schema
21
21
if ( resolvers ) {
22
- buildedSchema = buildGraphQLSchema ( schema ) ;
22
+ buildedSchema = buildGraphQLSchema ( schema )
23
23
buildedSchema = makeExecutableSchema ( {
24
24
typeDefs : buildedSchema ,
25
- resolvers
26
- } ) ;
25
+ resolvers,
26
+ } )
27
27
}
28
28
29
- this . schema = buildedSchema ;
30
- this . gqlSchema = buildGraphQLSchema ( schema , true ) ;
31
- this . parsedSchema = schemaParser ( schema ) ;
32
- this . mockedSchema = mocker ( schema ) ;
33
- this . fixture = null ;
34
- this . opts = { } ;
29
+ this . schema = buildedSchema
30
+ this . gqlSchema = buildGraphQLSchema ( schema , true )
31
+ this . parsedSchema = schemaParser ( schema )
32
+ this . mockedSchema = mocker ( schema )
33
+ this . fixture = null
34
+ this . opts = { }
35
35
}
36
36
37
37
mock ( opts , args = { } ) {
38
38
if ( ! opts || ( isObject ( opts ) && ! opts . query && ! opts . kind ) ) {
39
- throw new Error ( " The Query/Mutation to test is require" ) ;
39
+ throw new Error ( ' The Query/Mutation to test is require' )
40
40
}
41
41
42
- const operationOptions = { ...this . opts , ...opts } ;
42
+ const operationOptions = { ...this . opts , ...opts }
43
43
44
- let { query, variables, fixture } = operationOptions ;
44
+ let { query, variables, fixture } = operationOptions
45
45
if ( ! fixture && this . fixture ) {
46
- operationOptions . fixture = this . fixture ;
46
+ operationOptions . fixture = this . fixture
47
47
}
48
48
49
- query = query || opts ;
50
- args = variables || args ;
49
+ query = query || opts
50
+ args = variables || args
51
51
52
52
return mock (
53
53
this . gqlSchema ,
@@ -56,7 +56,7 @@ class Tester {
56
56
this . mockedSchema ,
57
57
operationOptions ,
58
58
this . parsedSchema
59
- ) ;
59
+ )
60
60
}
61
61
62
62
/**
@@ -66,50 +66,50 @@ class Tester {
66
66
* @param args - In case it is a mutation, it will the input type.
67
67
*/
68
68
test ( isValid , query , args ) {
69
- if ( typeof isValid !== " boolean" ) {
70
- throw new Error ( " isValid argument must be a boolean" ) ;
69
+ if ( typeof isValid !== ' boolean' ) {
70
+ throw new Error ( ' isValid argument must be a boolean' )
71
71
}
72
72
73
73
if ( isValid ) {
74
- this . mock ( query , args ) ;
75
- return assert . strictEqual ( isValid , true ) ;
74
+ this . mock ( query , args )
75
+ return assert . strictEqual ( isValid , true )
76
76
} else {
77
- let error ;
77
+ let error
78
78
try {
79
- this . mock ( query , args ) ;
79
+ this . mock ( query , args )
80
80
} catch ( err ) {
81
- error = err ;
81
+ error = err
82
82
}
83
83
84
84
if ( ! error ) {
85
85
throw new Error (
86
86
`Failed, there should be an error and the passed query/mutation is valid`
87
- ) ;
87
+ )
88
88
}
89
89
90
- return assert . strictEqual ( isValid , false ) ;
90
+ return assert . strictEqual ( isValid , false )
91
91
}
92
92
}
93
93
94
94
graphql ( query , rootValue , contextValue , variableValues ) {
95
95
if ( isObject ( query ) ) {
96
- query = print ( query ) ;
96
+ query = print ( query )
97
97
}
98
98
99
- return graphql ( this . schema , query , rootValue , contextValue , variableValues ) ;
99
+ return graphql ( this . schema , query , rootValue , contextValue , variableValues )
100
100
}
101
101
102
102
setFixture ( fixture , opts = { } ) {
103
103
this . opts = {
104
- autoMock : opts . autoMock
105
- } ;
106
- this . fixture = fixture ;
104
+ autoMock : opts . autoMock ,
105
+ }
106
+ this . fixture = fixture
107
107
}
108
108
109
109
clearFixture ( ) {
110
- this . opts = { } ;
111
- this . fixture = null ;
110
+ this . opts = { }
111
+ this . fixture = null
112
112
}
113
113
}
114
114
115
- module . exports = Tester ;
115
+ module . exports = Tester
0 commit comments