fix: 🐛 handle correctly basic falsy types in getType() (#47930) (#47953)

This commit is contained in:
Vadim Dalecky 2019-10-11 18:09:07 +02:00 committed by GitHub
parent 7a6f7dcaef
commit a133b5fb13
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 1 deletions

View file

@ -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();
});
});

View file

@ -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;