diff --git a/packages/actions/src/__tests__/utils.test.ts b/packages/actions/src/__tests__/utils.test.ts index 5d0dbf689..298fe7441 100644 --- a/packages/actions/src/__tests__/utils.test.ts +++ b/packages/actions/src/__tests__/utils.test.ts @@ -3,7 +3,7 @@ import { filterByFields } from '../utils'; describe('utils', () => { describe('filterByFields', () => { - it('only fields', async () => { + it('only fields', () => { const values = filterByFields({ title: 'title1', sort: 100, @@ -14,7 +14,7 @@ describe('utils', () => { }); }); - it('except fields', async () => { + it('except fields', () => { const values = filterByFields({ title: 'title1', sort: 100, @@ -28,7 +28,7 @@ describe('utils', () => { }); }); - it('only and except fields', async () => { + it('only and except fields', () => { const values = filterByFields({ title: 'title1', sort: 100, @@ -42,7 +42,7 @@ describe('utils', () => { }); }); - it('only and except fields with array', async () => { + it('only and except fields with array', () => { const values = filterByFields({ title: 'title1', comments: [ @@ -61,7 +61,7 @@ describe('utils', () => { }); }); - it('only and except fields with array', async () => { + it('only and except fields with array', () => { const values = filterByFields({ title: 'title1', user: { name: 'aaa', profile: { email: 'email' } }, @@ -83,5 +83,26 @@ describe('utils', () => { ] }); }); + + it('empty values', () => { + const values = filterByFields({}, { + only: ['a'] + }); + expect(values).toEqual({}); + }); + + it('null values', () => { + const values = filterByFields(null, { + only: ['a'] + }); + expect(values).toBe(null); + }); + + it('undefined values', () => { + const values = filterByFields(undefined, { + only: ['a'] + }); + expect(values).toBeUndefined(); + }); }); }); diff --git a/packages/actions/src/utils.ts b/packages/actions/src/utils.ts index 62f318bf2..5dc1e3f09 100644 --- a/packages/actions/src/utils.ts +++ b/packages/actions/src/utils.ts @@ -1,6 +1,10 @@ import _ from 'lodash'; export function filterByFields(data: any, fields: any = {}): any { + if (data == null) { + return data; + } + const { only = Array.isArray(fields) ? fields : null, except = []