From f9a93e0f121ff67e88d2fcadb302006155e63658 Mon Sep 17 00:00:00 2001 From: ramfox Date: Fri, 15 Nov 2019 14:18:44 -0500 Subject: [PATCH] fix(bodyValue): account for null or undefined previous body values --- app/components/ComponentList.tsx | 1 - app/utils/bodyValue.ts | 17 +++++++---------- test/app/utils/bodyValue.test.ts | 7 +++++++ 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/app/components/ComponentList.tsx b/app/components/ComponentList.tsx index 6a62da41..56ceae07 100644 --- a/app/components/ComponentList.tsx +++ b/app/components/ComponentList.tsx @@ -126,7 +126,6 @@ function removeHiddenComponents (status: DatasetStatus) { } return (component): boolean => { - console.log(status[component.name]) return status[component.name] || !hideWhenMissing[component.name] } } diff --git a/app/utils/bodyValue.ts b/app/utils/bodyValue.ts index 927e9d90..a009abdc 100644 --- a/app/utils/bodyValue.ts +++ b/app/utils/bodyValue.ts @@ -1,16 +1,12 @@ import { PageInfo } from '../../app/models/store' export default function bodyValue (prev: Object | any[] | undefined, curr: Object | any[], pageInfo: PageInfo): Object | any[] { - // if there is no previous body value, just return the current body value - if (!prev) return curr // work through cases where the current body value is an array if (Array.isArray(curr)) { - // if the previous body value is not an array - // just return the current body value - if (!Array.isArray(prev)) { - return curr + if (curr.length === 0) { + if (!prev) return curr + return prev } - // otherwise, it is an array and we should concat the lists if (Array.isArray(curr[0])) { // for tabular/2D arrays only: @@ -23,6 +19,9 @@ export default function bodyValue (prev: Object | any[] | undefined, curr: Objec d.unshift(index) }) + // if there is no previous body value, just return the current body value + if (!prev || !Array.isArray(prev)) return curr + if (prev.length !== 0) { const currLastIndex = curr[curr.length - 1][0] const prevLastIndex = prev[prev.length - 1][0] || 0 @@ -51,14 +50,12 @@ export default function bodyValue (prev: Object | any[] | undefined, curr: Objec return [...prev, ...curr] } - - return [...prev, ...curr] } // work through cases where the current body value is an object (but not an array) if (typeof curr === 'object') { // if the previous body value is not an object (or is an object but is also an array) // return the current body value - if (!(typeof prev === 'object') && Array.isArray(prev)) { + if (!prev || (!(typeof prev === 'object') && Array.isArray(prev))) { return curr } // otherwise return the spread! diff --git a/test/app/utils/bodyValue.test.ts b/test/app/utils/bodyValue.test.ts index 8cf42e64..fc893f08 100644 --- a/test/app/utils/bodyValue.test.ts +++ b/test/app/utils/bodyValue.test.ts @@ -30,6 +30,13 @@ describe('bodyValue', () => { pageInfo: pageInfo2, expected: [] }, + { + describe: 'prev is undefined', + prev: undefined, + curr: [['a','b','c'],['d','e','f']], + pageInfo: initPageInfo, + expected: [[0, 'a','b','c'],[1, 'd','e','f']] + }, { describe: 'prev is empty', prev: [],