diff --git a/common/util.spec.js b/common/util.spec.js index a66f12a39bd1307fbc17573f078937e37d742350..2af63766c202999af0a9ca2ccf57a341d947f0ee 100644 --- a/common/util.spec.js +++ b/common/util.spec.js @@ -1,4 +1,4 @@ -import { getIdFromFullId, strToRgb } from './util' +import { getIdFromFullId, strToRgb, verifyPositionArg } from './util' describe('common/util.js', () => { describe('getIdFromFullId', () => { @@ -76,4 +76,45 @@ describe('common/util.js', () => { }) }) + + describe('verifyPositionArg', () => { + describe('malformed input', () => { + let input + it('> if props.components[0] is string', () => { + input= 'hello world' + expect(verifyPositionArg(input)).toBeFalsy() + }) + it('> if position property is object', () => { + input={ + x: 0, + y: 0, + z: 0 + } + expect(verifyPositionArg(input)).toBeFalsy() + }) + + it('> if position property is array of incorrect length', () => { + input=[] + expect(verifyPositionArg(input)).toBeFalsy() + }) + + it('> if position property is array contain non number elements', () => { + input = [1, 2, 'hello world'] + expect(verifyPositionArg(input)).toBeFalsy() + }) + + it('> if position property is array contain NaN', () => { + input=[1,2,NaN] + expect(verifyPositionArg(input)).toBeFalsy() + }) + + }) + + describe('correct input', () => { + let input + it('> return true', () => { + expect(verifyPositionArg([1,2,3])).toBeTruthy() + }) + }) + }) }) diff --git a/src/atlasComponents/parcellationRegion/region.base.spec.ts b/src/atlasComponents/parcellationRegion/region.base.spec.ts index ca0c1af0c60e78ba5802329c79a1a88588e92c17..df343917f7bb050a4a0b3553c1fabd7f918bfaa1 100644 --- a/src/atlasComponents/parcellationRegion/region.base.spec.ts +++ b/src/atlasComponents/parcellationRegion/region.base.spec.ts @@ -471,53 +471,51 @@ describe('> region.base.ts', () => { it('> does not populate if position property is absent', () => { regionBase.region = { ...mr0 - } + } as any expect(regionBase.position).toBeFalsy() }) describe('> does not populate if position property is malformed', () => { - - it('> if position property is string', () => { - regionBase.region = { - ...mr0, - position: 'hello world' - } + it('> if region is falsy', () => { + regionBase.region = null expect(regionBase.position).toBeFalsy() }) - it('> if position property is object', () => { + it('> if props is falsy', () => { regionBase.region = { ...mr0, - position: { - x: 0, - y: 0, - z: 0 - } - } + props: null + } as any expect(regionBase.position).toBeFalsy() }) - - it('> if position property is array of incorrect length', () => { + it('> if props.components is falsy', () => { regionBase.region = { ...mr0, - position: [] - } + props: { + components: null + } + } as any expect(regionBase.position).toBeFalsy() }) - - it('> if position property is array contain non number elements', () => { + it('> if props.components[0] is falsy', () => { regionBase.region = { ...mr0, - position: [1, 2, 'hello world'] - } + props: { + components: [] + } + } as any expect(regionBase.position).toBeFalsy() }) + it('> if props.components[0].centroid is falsy', () => { - it('> if position property is array contain NaN', () => { regionBase.region = { ...mr0, - position: [1, 2, NaN] - } + props: { + components: [{ + centroid: null + }] + } + } as any expect(regionBase.position).toBeFalsy() }) }) @@ -525,8 +523,12 @@ describe('> region.base.ts', () => { it('> populates if position property is array with length 3 and non NaN element', () => { regionBase.region = { ...mr0, - position: [1, 2, 3] - } + props: { + components: [{ + centroid: [1, 2, 3] + }] + }, + } as any expect(regionBase.position).toBeTruthy() }) }) @@ -549,7 +551,7 @@ describe('> region.base.ts', () => { const regionBase = new RegionBase(mockStore) regionBase.region = { rgb: [100, 120, 140] - } + } as any expect( regionBase.rgbString ).toEqual(`rgb(100,120,140)`) @@ -560,7 +562,7 @@ describe('> region.base.ts', () => { const regionBase = new RegionBase(mockStore) regionBase.region = { labelIndex: 65535 - } + } as any expect( regionBase.rgbString ).toEqual(`rgb(255,255,255)`) @@ -576,7 +578,7 @@ describe('> region.base.ts', () => { ngId: 'foo', name: 'bar', labelIndex: 152 - } + } as any expect(strToRgbSpy).toHaveBeenCalledWith(`foo152`) }) it('> if ngId is not defined, use name', () => { @@ -585,7 +587,7 @@ describe('> region.base.ts', () => { regionBase.region = { name: 'bar', labelIndex: 152 - } + } as any expect(strToRgbSpy).toHaveBeenCalledWith(`bar152`) }) }) @@ -601,7 +603,7 @@ describe('> region.base.ts', () => { const regionBase = new RegionBase(mockStore) regionBase.region = { foo: 'bar' - } + } as any expect( regionBase.rgbString ).toEqual(`rgb(${arr.join(',')})`) @@ -613,7 +615,7 @@ describe('> region.base.ts', () => { const regionBase = new RegionBase(mockStore) regionBase.region = { foo: 'bar' - } + } as any expect( regionBase.rgbString ).toEqual(`rgb(255,200,200)`) diff --git a/src/atlasComponents/parcellationRegion/region.base.ts b/src/atlasComponents/parcellationRegion/region.base.ts index 12209ddb7c8ac740f7b5b45ee98eb71fc9f5ca8c..f596afe76f7cfbffc7e83a39c24993e0b52e7530 100644 --- a/src/atlasComponents/parcellationRegion/region.base.ts +++ b/src/atlasComponents/parcellationRegion/region.base.ts @@ -18,7 +18,7 @@ export class RegionBase { public rgbDarkmode: boolean private _region: TRegionDetail & { - context: { + context?: { atlas: IHasId template: IHasId parcellation: IHasId diff --git a/src/util/siibraApiConstants/fn.ts b/src/util/siibraApiConstants/fn.ts index d1cc7ff554d14490b1ae1d9915d10ff1dbcaba4b..0316b8c94f93b5c9bd0d65f8ed0aa957fe918847 100644 --- a/src/util/siibraApiConstants/fn.ts +++ b/src/util/siibraApiConstants/fn.ts @@ -1,7 +1,8 @@ import { TRegionDetail } from "./types"; export function getPosFromRegion(region: TRegionDetail){ - if (!region?.props) return null - if (!region.props.components?.[0]) return null - return region.props.components[0].centroid.map(v => v*1e6) as [number, number, number] + if (!!region?.props?.components?.[0]?.centroid) { + return region.props.components[0].centroid.map(v => v*1e6) as [number, number, number] + } + return null } \ No newline at end of file