@@ -12,7 +12,7 @@ import {
1212} from 'graphql' ;
1313import dedent from 'ts-dedent' ;
1414
15- import { ObjectTypeDefinitionBuilder , isGeneratedByIntrospection , topologicalSortAST , topsort } from '../src/graphql' ;
15+ import { ObjectTypeDefinitionBuilder , escapeGraphQLCharacters , isGeneratedByIntrospection , topologicalSortAST , topsort } from '../src/graphql' ;
1616
1717describe ( 'graphql' , ( ) => {
1818 describe ( 'objectTypeDefinitionBuilder' , ( ) => {
@@ -297,3 +297,65 @@ describe('isGeneratedByIntrospection function', () => {
297297 expect ( isGeneratedByIntrospection ( clientSchema ) ) . toBe ( true ) ;
298298 } ) ;
299299} ) ;
300+
301+ describe ( 'escapeGraphQLCharacters' , ( ) => {
302+ it ( 'should escape double quotes' , ( ) => {
303+ const input = 'This is a "test" string.' ;
304+ const expected = 'This is a \\\"test\\\" string.' ;
305+ expect ( escapeGraphQLCharacters ( input ) ) . toBe ( expected ) ;
306+ } ) ;
307+
308+ it ( 'should escape backslashes' , ( ) => {
309+ const input = 'This is a backslash: \\' ;
310+ const expected = 'This is a backslash: \\\\' ;
311+ expect ( escapeGraphQLCharacters ( input ) ) . toBe ( expected ) ;
312+ } ) ;
313+
314+ it ( 'should escape forward slashes' , ( ) => {
315+ const input = 'This is a forward slash: /' ;
316+ const expected = 'This is a forward slash: \\/' ;
317+ expect ( escapeGraphQLCharacters ( input ) ) . toBe ( expected ) ;
318+ } ) ;
319+
320+ it ( 'should escape backspaces' , ( ) => {
321+ const input = 'This is a backspace: \b' ;
322+ const expected = 'This is a backspace: \\b' ;
323+ expect ( escapeGraphQLCharacters ( input ) ) . toBe ( expected ) ;
324+ } ) ;
325+
326+ it ( 'should escape form feeds' , ( ) => {
327+ const input = 'This is a form feed: \f' ;
328+ const expected = 'This is a form feed: \\f' ;
329+ expect ( escapeGraphQLCharacters ( input ) ) . toBe ( expected ) ;
330+ } ) ;
331+
332+ it ( 'should escape new lines' , ( ) => {
333+ const input = 'This is a new line: \n' ;
334+ const expected = 'This is a new line: \\n' ;
335+ expect ( escapeGraphQLCharacters ( input ) ) . toBe ( expected ) ;
336+ } ) ;
337+
338+ it ( 'should escape carriage returns' , ( ) => {
339+ const input = 'This is a carriage return: \r' ;
340+ const expected = 'This is a carriage return: \\r' ;
341+ expect ( escapeGraphQLCharacters ( input ) ) . toBe ( expected ) ;
342+ } ) ;
343+
344+ it ( 'should escape horizontal tabs' , ( ) => {
345+ const input = 'This is a tab: \t' ;
346+ const expected = 'This is a tab: \\t' ;
347+ expect ( escapeGraphQLCharacters ( input ) ) . toBe ( expected ) ;
348+ } ) ;
349+
350+ it ( 'should escape multiple special characters' , ( ) => {
351+ const input = 'This is a "test" string with \n new line and \t tab.' ;
352+ const expected = 'This is a \\\"test\\\" string with \\n new line and \\t tab.' ;
353+ expect ( escapeGraphQLCharacters ( input ) ) . toBe ( expected ) ;
354+ } ) ;
355+
356+ it ( 'should not escape non-special characters' , ( ) => {
357+ const input = 'Normal string with no special characters.' ;
358+ const expected = 'Normal string with no special characters.' ;
359+ expect ( escapeGraphQLCharacters ( input ) ) . toBe ( expected ) ;
360+ } ) ;
361+ } ) ;
0 commit comments