From a133b5fb13a757be85fd6d2d053178e3e7ed3af0 Mon Sep 17 00:00:00 2001 From: Vadim Dalecky Date: Fri, 11 Oct 2019 18:09:07 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20handle=20correctly=20basi?= =?UTF-8?q?c=20falsy=20types=20in=20getType()=20(#47930)=20(#47953)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../expressions/serialize_provider.test.ts | 48 +++++++++++++++++++ .../common/expressions/serialize_provider.ts | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/plugins/expressions/common/expressions/serialize_provider.test.ts diff --git a/src/plugins/expressions/common/expressions/serialize_provider.test.ts b/src/plugins/expressions/common/expressions/serialize_provider.test.ts new file mode 100644 index 000000000000..774fb34938dd --- /dev/null +++ b/src/plugins/expressions/common/expressions/serialize_provider.test.ts @@ -0,0 +1,48 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { getType } from './serialize_provider'; + +describe('getType()', () => { + test('returns "null" string for null or undefined', () => { + expect(getType(null)).toBe('null'); + expect(getType(undefined)).toBe('null'); + }); + + test('returns basic type name', () => { + expect(getType(0)).toBe('number'); + expect(getType(1)).toBe('number'); + expect(getType(0.8)).toBe('number'); + expect(getType(Infinity)).toBe('number'); + + expect(getType(true)).toBe('boolean'); + expect(getType(false)).toBe('boolean'); + }); + + test('returns .type property value of objects', () => { + expect(getType({ type: 'foo' })).toBe('foo'); + expect(getType({ type: 'bar' })).toBe('bar'); + }); + + test('throws if object has no .type property', () => { + expect(() => getType({})).toThrow(); + expect(() => getType({ _type: 'foo' })).toThrow(); + expect(() => getType({ tipe: 'foo' })).toThrow(); + }); +}); diff --git a/src/plugins/expressions/common/expressions/serialize_provider.ts b/src/plugins/expressions/common/expressions/serialize_provider.ts index 067c59c4689e..1bd06a38a256 100644 --- a/src/plugins/expressions/common/expressions/serialize_provider.ts +++ b/src/plugins/expressions/common/expressions/serialize_provider.ts @@ -20,7 +20,7 @@ import { get, identity } from 'lodash'; export function getType(node: any) { - if (!node) return 'null'; + if (node == null) return 'null'; if (typeof node === 'object') { if (!node.type) throw new Error('Objects must have a type property'); return node.type;