55 * LICENSE file in the root directory of this source tree.
66 */
77
8- import { GraphQLInt , GraphQLFloat , GraphQLString , GraphQLBoolean } from '../' ;
8+ import {
9+ GraphQLInt ,
10+ GraphQLID ,
11+ GraphQLFloat ,
12+ GraphQLString ,
13+ GraphQLBoolean ,
14+ } from '../' ;
915
1016import { describe , it } from 'mocha' ;
1117import { expect } from 'chai' ;
1218
1319describe ( 'Type System: Scalar coercion' , ( ) => {
14- it ( 'serializes output int ' , ( ) => {
20+ it ( 'serializes output as Int ' , ( ) => {
1521 expect ( GraphQLInt . serialize ( 1 ) ) . to . equal ( 1 ) ;
1622 expect ( GraphQLInt . serialize ( '123' ) ) . to . equal ( 123 ) ;
1723 expect ( GraphQLInt . serialize ( 0 ) ) . to . equal ( 0 ) ;
1824 expect ( GraphQLInt . serialize ( - 1 ) ) . to . equal ( - 1 ) ;
1925 expect ( GraphQLInt . serialize ( 1e5 ) ) . to . equal ( 100000 ) ;
26+ expect ( GraphQLInt . serialize ( false ) ) . to . equal ( 0 ) ;
27+ expect ( GraphQLInt . serialize ( true ) ) . to . equal ( 1 ) ;
28+
2029 // The GraphQL specification does not allow serializing non-integer values
2130 // as Int to avoid accidental data loss.
2231 expect ( ( ) => GraphQLInt . serialize ( 0.1 ) ) . to . throw (
@@ -49,17 +58,19 @@ describe('Type System: Scalar coercion', () => {
4958 expect ( ( ) => GraphQLInt . serialize ( 'one' ) ) . to . throw (
5059 'Int cannot represent non 32-bit signed integer value: one' ,
5160 ) ;
52- expect ( GraphQLInt . serialize ( false ) ) . to . equal ( 0 ) ;
53- expect ( GraphQLInt . serialize ( true ) ) . to . equal ( 1 ) ;
61+ // Doesn't represent number
5462 expect ( ( ) => GraphQLInt . serialize ( '' ) ) . to . throw (
5563 'Int cannot represent non 32-bit signed integer value: (empty string)' ,
5664 ) ;
5765 expect ( ( ) => GraphQLInt . serialize ( NaN ) ) . to . throw (
5866 'Int cannot represent non 32-bit signed integer value: NaN' ,
5967 ) ;
68+ expect ( ( ) => GraphQLInt . serialize ( [ 5 ] ) ) . to . throw (
69+ 'Int cannot represent an array value: [5]' ,
70+ ) ;
6071 } ) ;
6172
62- it ( 'serializes output float ' , ( ) => {
73+ it ( 'serializes output as Float ' , ( ) => {
6374 expect ( GraphQLFloat . serialize ( 1 ) ) . to . equal ( 1.0 ) ;
6475 expect ( GraphQLFloat . serialize ( 0 ) ) . to . equal ( 0.0 ) ;
6576 expect ( GraphQLFloat . serialize ( '123.5' ) ) . to . equal ( 123.5 ) ;
@@ -74,30 +85,42 @@ describe('Type System: Scalar coercion', () => {
7485 expect ( ( ) => GraphQLFloat . serialize ( NaN ) ) . to . throw (
7586 'Float cannot represent non numeric value: NaN' ,
7687 ) ;
77-
7888 expect ( ( ) => GraphQLFloat . serialize ( 'one' ) ) . to . throw (
7989 'Float cannot represent non numeric value: one' ,
8090 ) ;
81-
8291 expect ( ( ) => GraphQLFloat . serialize ( '' ) ) . to . throw (
8392 'Float cannot represent non numeric value: (empty string)' ,
8493 ) ;
94+ expect ( ( ) => GraphQLFloat . serialize ( [ 5 ] ) ) . to . throw (
95+ 'Float cannot represent an array value: [5]' ,
96+ ) ;
8597 } ) ;
8698
87- it ( 'serializes output strings' , ( ) => {
88- expect ( GraphQLString . serialize ( 'string' ) ) . to . equal ( 'string' ) ;
89- expect ( GraphQLString . serialize ( 1 ) ) . to . equal ( '1 ' ) ;
90- expect ( GraphQLString . serialize ( - 1.1 ) ) . to . equal ( '-1. 1' ) ;
91- expect ( GraphQLString . serialize ( true ) ) . to . equal ( 'true ' ) ;
92- expect ( GraphQLString . serialize ( false ) ) . to . equal ( 'false ' ) ;
93- } ) ;
99+ for ( const scalar of [ GraphQLString , GraphQLID ] ) {
100+ it ( `serializes output as ${ scalar } ` , ( ) => {
101+ expect ( scalar . serialize ( 'string' ) ) . to . equal ( 'string ' ) ;
102+ expect ( scalar . serialize ( 1 ) ) . to . equal ( '1' ) ;
103+ expect ( scalar . serialize ( - 1.1 ) ) . to . equal ( '-1.1 ' ) ;
104+ expect ( scalar . serialize ( true ) ) . to . equal ( 'true ' ) ;
105+ expect ( scalar . serialize ( false ) ) . to . equal ( 'false' ) ;
94106
95- it ( 'serializes output boolean' , ( ) => {
107+ expect ( ( ) => scalar . serialize ( [ 1 ] ) ) . to . throw (
108+ 'String cannot represent an array value: [1]' ,
109+ ) ;
110+ } ) ;
111+ }
112+
113+ it ( 'serializes output as Boolean' , ( ) => {
96114 expect ( GraphQLBoolean . serialize ( 'string' ) ) . to . equal ( true ) ;
115+ expect ( GraphQLBoolean . serialize ( 'false' ) ) . to . equal ( true ) ;
97116 expect ( GraphQLBoolean . serialize ( '' ) ) . to . equal ( false ) ;
98117 expect ( GraphQLBoolean . serialize ( 1 ) ) . to . equal ( true ) ;
99118 expect ( GraphQLBoolean . serialize ( 0 ) ) . to . equal ( false ) ;
100119 expect ( GraphQLBoolean . serialize ( true ) ) . to . equal ( true ) ;
101120 expect ( GraphQLBoolean . serialize ( false ) ) . to . equal ( false ) ;
121+
122+ expect ( ( ) => GraphQLBoolean . serialize ( [ false ] ) ) . to . throw (
123+ 'Boolean cannot represent an array value: [false]' ,
124+ ) ;
102125 } ) ;
103126} ) ;
0 commit comments