@@ -2,7 +2,9 @@ const { deepStrictEqual, strictEqual } = require('assert')
2
2
const { valueByImplType, isStatusCodeError } = require ( './testutils' )
3
3
4
4
module . exports . test = function ( setup , implType ) {
5
- const queryIntType = valueByImplType ( implType , 'size_Int_max_3' , 'Int' )
5
+ const queryIntMaxType = valueByImplType ( implType , 'size_Int_max_3' , 'Int' )
6
+ const queryIntMinType = valueByImplType ( implType , 'size_Int_min_3' , 'Int' )
7
+ const queryStringMinLengthType = valueByImplType ( implType , 'search_String_minLength_3' , 'String' )
6
8
7
9
describe ( 'Variables' , function ( ) {
8
10
describe ( 'variable default value' , function ( ) {
@@ -20,7 +22,7 @@ module.exports.test = function (setup, implType) {
20
22
21
23
it ( 'should pass' , async function ( ) {
22
24
const query = /* GraphQL */ `
23
- query ($size: ${ queryIntType } = 3) {
25
+ query ($size: ${ queryIntMaxType } = 3) {
24
26
books(size: $size) {
25
27
title
26
28
}
@@ -37,7 +39,7 @@ module.exports.test = function (setup, implType) {
37
39
38
40
it ( 'should fail' , async function ( ) {
39
41
const query = /* GraphQL */ `
40
- query ($size: ${ queryIntType } = 4) {
42
+ query ($size: ${ queryIntMaxType } = 4) {
41
43
books(size: $size) {
42
44
title
43
45
}
@@ -53,5 +55,103 @@ module.exports.test = function (setup, implType) {
53
55
valueByImplType ( implType , 'Expected value of type "size_Int_max_3", found 4;' , 'Variable "$size" got invalid value 4.' ) + ' Must be no greater than 3' )
54
56
} )
55
57
} )
58
+
59
+ describe ( 'variable int falsy' , function ( ) {
60
+ before ( async function ( ) {
61
+ this . typeDefs = /* GraphQL */ `
62
+ type Query {
63
+ books (size: Int @constraint(min: 3)): [Book]
64
+ }
65
+ type Book {
66
+ title: String
67
+ }
68
+ `
69
+ this . request = await setup ( { typeDefs : this . typeDefs } )
70
+ } )
71
+
72
+ it ( 'should pass' , async function ( ) {
73
+ const query = /* GraphQL */ `
74
+ query ($size: ${ queryIntMinType } = 3) {
75
+ books(size: $size) {
76
+ title
77
+ }
78
+ }
79
+ `
80
+ const { body, statusCode } = await this . request
81
+ . post ( '/graphql' )
82
+ . set ( 'Accept' , 'application/json' )
83
+ . send ( { query } )
84
+
85
+ strictEqual ( statusCode , 200 )
86
+ deepStrictEqual ( body , { data : { books : null } } )
87
+ } )
88
+
89
+ it ( 'should fail' , async function ( ) {
90
+ const query = /* GraphQL */ `
91
+ query ($size: ${ queryIntMinType } = 0) {
92
+ books(size: $size) {
93
+ title
94
+ }
95
+ }
96
+ `
97
+ const { body, statusCode } = await this . request
98
+ . post ( '/graphql' )
99
+ . set ( 'Accept' , 'application/json' )
100
+ . send ( { query } )
101
+
102
+ isStatusCodeError ( statusCode , implType )
103
+ strictEqual ( body . errors [ 0 ] . message ,
104
+ valueByImplType ( implType , 'Expected value of type "size_Int_min_3", found 0;' , 'Variable "$size" got invalid value 0.' ) + ' Must be at least 3' )
105
+ } )
106
+ } )
107
+
108
+ describe ( 'variable string falsy' , function ( ) {
109
+ before ( async function ( ) {
110
+ this . typeDefs = /* GraphQL */ `
111
+ type Query {
112
+ books (search: String @constraint(minLength: 3)): [Book]
113
+ }
114
+ type Book {
115
+ title: String
116
+ }
117
+ `
118
+ this . request = await setup ( { typeDefs : this . typeDefs } )
119
+ } )
120
+
121
+ it ( 'should pass' , async function ( ) {
122
+ const query = /* GraphQL */ `
123
+ query ($search: ${ queryStringMinLengthType } = "test") {
124
+ books(search: $search) {
125
+ title
126
+ }
127
+ }
128
+ `
129
+ const { body, statusCode } = await this . request
130
+ . post ( '/graphql' )
131
+ . set ( 'Accept' , 'application/json' )
132
+ . send ( { query } )
133
+
134
+ strictEqual ( statusCode , 200 )
135
+ deepStrictEqual ( body , { data : { books : null } } )
136
+ } )
137
+
138
+ it ( 'should fail - despite being falsy' , async function ( ) {
139
+ const query = /* GraphQL */ `
140
+ query ($search: ${ queryStringMinLengthType } = "") {
141
+ books(search: $search) {
142
+ title
143
+ }
144
+ }
145
+ `
146
+ const { body, statusCode } = await this . request
147
+ . post ( '/graphql' )
148
+ . set ( 'Accept' , 'application/json' )
149
+ . send ( { query } )
150
+
151
+ isStatusCodeError ( statusCode , implType )
152
+ strictEqual ( body . errors [ 0 ] . message ,
153
+ valueByImplType ( implType , 'Expected value of type "search_String_minLength_3", found "";' , 'Variable "$search" got invalid value "".' ) + ' Must be at least 3 characters in length' )
154
+ } )
155
+ } )
56
156
} )
57
157
}
0 commit comments