Skip to content

Commit

Permalink
fix(amplify-appsync-simulator): utils.toJson false value (#5797)
Browse files Browse the repository at this point in the history
  • Loading branch information
bboure committed Dec 11, 2020
1 parent 4a5b305 commit cc1a74b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { create } from '../../../velocity/util/index';
import { JavaMap } from '../../../velocity/value-mapper/map';
import { GraphQLResolveInfo } from 'graphql';
import { TemplateSentError } from '../../../velocity/util/errors';
import { hasUncaughtExceptionCaptureCallback } from 'process';
import { generalUtils } from '../../../velocity/util/general-utils';

const stubInfo = {
fieldName: 'testFieldName',
Expand Down Expand Up @@ -60,3 +61,29 @@ it('appendError_filterDataJavaMap', () => {
expect(util.errors.length).toBe(1);
expect(util.errors[0].data).toStrictEqual({ field1: 'field1Value', field2: 'field2Value' });
});

describe('$utils.toJson', () => {
it('should stringify an object', () => {
const object = { foo: 'Bar' };
expect(generalUtils.toJson(object)).toBe('{"foo":"Bar"}');
});
it('should return "null" for null values', () => {
const object = null;
expect(generalUtils.toJson(object)).toBe('null');
});

it('should return "null" for undefined values', () => {
const object = undefined;
expect(generalUtils.toJson(object)).toBe('null');
});

it('should return "true" for true values', () => {
const object = true;
expect(generalUtils.toJson(object)).toBe('true');
});

it('should return "false" for false values', () => {
const object = false;
expect(generalUtils.toJson(object)).toBe('false');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { JavaArray } from '../value-mapper/array';
import { JavaMap } from '../value-mapper/map';
import jsStringEscape from 'js-string-escape';
import { GraphQLResolveInfo, FieldNode } from 'graphql';

export const generalUtils = {
errors: [],
quiet: () => '',
Expand All @@ -30,7 +31,7 @@ export const generalUtils = {
return JSON.parse(value);
},
toJson(value) {
return value ? JSON.stringify(value) : JSON.stringify(null);
return value !== undefined ? JSON.stringify(value) : JSON.stringify(null);
},
autoId() {
return autoId();
Expand Down

0 comments on commit cc1a74b

Please sign in to comment.