From c2d439fdf75d4e4c7a10c798c2665f78ddbb3bc9 Mon Sep 17 00:00:00 2001
From: Xiao Gui <xgui3783@gmail.com>
Date: Fri, 16 Jun 2023 17:51:29 +0200
Subject: [PATCH] fix: tests

---
 src/state/atlasSelection/effects.spec.ts      | 18 +++---
 .../nehubaViewer.component.spec.ts            | 62 ++++++++++++++-----
 2 files changed, 54 insertions(+), 26 deletions(-)

diff --git a/src/state/atlasSelection/effects.spec.ts b/src/state/atlasSelection/effects.spec.ts
index fe9926671..9ba912441 100644
--- a/src/state/atlasSelection/effects.spec.ts
+++ b/src/state/atlasSelection/effects.spec.ts
@@ -158,13 +158,13 @@ describe("> effects.ts", () => {
                 },
                 previous: {
                   atlas: {
-                    "@id": IDS.ATLAES.RAT
+                    id: IDS.ATLAES.RAT
                   } as any,
                   parcellation: {
-                    "@id": IDS.PARCELLATION.WAXHOLMV4
+                    id: IDS.PARCELLATION.WAXHOLMV4
                   } as any,
                   template: {
-                    "@id": IDS.TEMPLATES.WAXHOLM
+                    id: IDS.TEMPLATES.WAXHOLM
                   } as any,
                 }
               })
@@ -186,24 +186,24 @@ describe("> effects.ts", () => {
               const obs = hook({
                 current: {
                   atlas: {
-                    "@id": IDS.ATLAES.HUMAN
+                    id: IDS.ATLAES.HUMAN
                   } as any,
                   parcellation: {
-                    "@id": IDS.PARCELLATION.JBA29
+                    id: IDS.PARCELLATION.JBA29
                   } as any,
                   template: {
-                    "@id": IDS.TEMPLATES.MNI152
+                    id: IDS.TEMPLATES.MNI152
                   } as any,
                 },
                 previous: {
                   atlas: {
-                    "@id": IDS.ATLAES.RAT
+                    id: IDS.ATLAES.RAT
                   } as any,
                   parcellation: {
-                    "@id": IDS.PARCELLATION.WAXHOLMV4
+                    id: IDS.PARCELLATION.WAXHOLMV4
                   } as any,
                   template: {
-                    "@id": IDS.TEMPLATES.WAXHOLM
+                    id: IDS.TEMPLATES.WAXHOLM
                   } as any,
                 }
               })
diff --git a/src/viewerModule/nehuba/nehubaViewer/nehubaViewer.component.spec.ts b/src/viewerModule/nehuba/nehubaViewer/nehubaViewer.component.spec.ts
index 18b8805b2..ed281a389 100644
--- a/src/viewerModule/nehuba/nehubaViewer/nehubaViewer.component.spec.ts
+++ b/src/viewerModule/nehuba/nehubaViewer/nehubaViewer.component.spec.ts
@@ -5,6 +5,7 @@ import { LoggingModule, LoggingService } from "src/logging"
 import { IMeshesToLoad, SET_MESHES_TO_LOAD } from "../constants"
 import { Subject } from "rxjs"
 import { IColorMap, SET_COLORMAP_OBS, SET_LAYER_VISIBILITY } from "../layerCtrl.service"
+import { rgbToHex } from 'common/util'
 
 describe('> nehubaViewer.component.ts', () => {
   describe('> #scanFn', () => {
@@ -305,15 +306,42 @@ describe('> nehubaViewer.component.ts', () => {
 
     describe('> # setColorMap', () => {
       let nehubaViewerSpy: any
+      let ngViewerStatechildrenGetSpy = jasmine.createSpy('get')
+      let toJsonSpy = jasmine.createSpy('toJsonSpy')
+      let restoreStateSpy = jasmine.createSpy('restoreStateSpy')
+
+      const ngId1 = 'foo-bar'
+      const ngId2 = 'hello-world'
       beforeEach(() => {
         nehubaViewerSpy = {
-          batchAddAndUpdateSegmentColors: jasmine.createSpy(),
           dispose(){
 
+          },
+          ngviewer: {
+            state: {
+              children: {
+                get: ngViewerStatechildrenGetSpy
+              }
+            }
           }
         }
+
+        ngViewerStatechildrenGetSpy.and.returnValue({
+          toJSON: toJsonSpy,
+          restoreState: restoreStateSpy,
+        })
+        toJsonSpy.and.returnValue([{
+          name: ngId1
+        }, {
+          name: ngId2
+        }])
       })
-      it('> calls nehubaViewer.batchAddAndUpdateSegmentColors', () => {
+      afterEach(() => {
+        ngViewerStatechildrenGetSpy.calls.reset()
+        toJsonSpy.calls.reset()
+        restoreStateSpy.calls.reset()
+      })
+      it('> calls nehubaViewer.restoreState', () => {
         const fixture = TestBed.createComponent(NehubaViewerUnit)
         fixture.componentInstance.nehubaViewer = nehubaViewerSpy
         fixture.detectChanges()
@@ -322,28 +350,28 @@ describe('> nehubaViewer.component.ts', () => {
         const fooBarMap = new Map()
         fooBarMap.set(1, {red: 100, green: 100, blue: 100})
         fooBarMap.set(2, {red: 200, green: 200, blue: 200})
-        mainMap.set('foo-bar', fooBarMap)
+        mainMap.set(ngId1, fooBarMap)
 
         const helloWorldMap = new Map()
         helloWorldMap.set(1, {red: 10, green: 10, blue: 10})
         helloWorldMap.set(2, {red: 20, green: 20, blue: 20})
-        mainMap.set('hello-world', helloWorldMap)
+        mainMap.set(ngId2, helloWorldMap)
 
         fixture.componentInstance['setColorMap'](mainMap)
 
-        expect(
-          nehubaViewerSpy.batchAddAndUpdateSegmentColors
-        ).toHaveBeenCalledTimes(2)
-
-        expect(nehubaViewerSpy.batchAddAndUpdateSegmentColors).toHaveBeenCalledWith(
-          fooBarMap,
-          { name: 'foo-bar' }
-        )
-
-        expect(nehubaViewerSpy.batchAddAndUpdateSegmentColors).toHaveBeenCalledWith(
-          helloWorldMap,
-          { name: 'hello-world' }
-        )
+        expect(restoreStateSpy).toHaveBeenCalledOnceWith([{
+          name: ngId1,
+          segmentColors: {
+            1: rgbToHex([100, 100, 100]),
+            2: rgbToHex([200, 200, 200]),
+          }
+        }, {
+          name: ngId2,
+          segmentColors: {
+            1: rgbToHex([10, 10, 10]),
+            2: rgbToHex([20, 20, 20]),
+          }
+        }])
       })
     })
 
-- 
GitLab