From 80019afe8bf940f94ebf998c00332ce3abe73b47 Mon Sep 17 00:00:00 2001
From: Xiao Gui <xgui3783@gmail.com>
Date: Tue, 21 Jun 2022 09:28:29 +0200
Subject: [PATCH] chore: on switch species, navigation resets

---
 src/atlasComponents/sapi/constants.ts    |  9 ++-
 src/state/atlasSelection/effects.spec.ts | 98 +++++++++++++++++++++++-
 src/state/atlasSelection/effects.ts      | 11 ++-
 3 files changed, 110 insertions(+), 8 deletions(-)

diff --git a/src/atlasComponents/sapi/constants.ts b/src/atlasComponents/sapi/constants.ts
index e9822b4a3..b31cfc1b1 100644
--- a/src/atlasComponents/sapi/constants.ts
+++ b/src/atlasComponents/sapi/constants.ts
@@ -1,13 +1,16 @@
 export const IDS = {
   ATLAES: {
-    HUMAN: "juelich/iav/atlas/v1.0.0/1"
+    HUMAN: "juelich/iav/atlas/v1.0.0/1",
+    RAT: "minds/core/parcellationatlas/v1.0.0/522b368e-49a3-49fa-88d3-0870a307974a",
   },
   TEMPLATES: {
     BIG_BRAIN: "minds/core/referencespace/v1.0.0/a1655b99-82f1-420f-a3c2-fe80fd4c8588",
     MNI152: "minds/core/referencespace/v1.0.0/dafcffc5-4826-4bf1-8ff6-46b8a31ff8e2",
-    COLIN27: "minds/core/referencespace/v1.0.0/7f39f7be-445b-47c0-9791-e971c0b6d992"
+    COLIN27: "minds/core/referencespace/v1.0.0/7f39f7be-445b-47c0-9791-e971c0b6d992",
+    WAXHOLM: "minds/core/referencespace/v1.0.0/d5717c4a-0fa1-46e6-918c-b8003069ade8",
   },
   PARCELLATION: {
-    JBA29: "minds/core/parcellationatlas/v1.0.0/94c1125b-b87e-45e4-901c-00daee7f2579-290"
+    JBA29: "minds/core/parcellationatlas/v1.0.0/94c1125b-b87e-45e4-901c-00daee7f2579-290",
+    WAXHOLMV4: "minds/core/parcellationatlas/v1.0.0/ebb923ba-b4d5-4b82-8088-fa9215c2e1fe-v4",
   }
 }
diff --git a/src/state/atlasSelection/effects.spec.ts b/src/state/atlasSelection/effects.spec.ts
index 61110e26b..556e002c5 100644
--- a/src/state/atlasSelection/effects.spec.ts
+++ b/src/state/atlasSelection/effects.spec.ts
@@ -1,16 +1,15 @@
-import { HttpClientTestingModule, HttpTestingController } from "@angular/common/http/testing"
 import { TestBed } from "@angular/core/testing"
 import { provideMockActions } from "@ngrx/effects/testing"
 import { Action } from "@ngrx/store"
 import { MockStore, provideMockStore } from "@ngrx/store/testing"
 import { hot } from "jasmine-marbles"
 import { Observable, of, throwError } from "rxjs"
-import { SAPI, SAPIModule, SapiRegionModel, SAPIParcellation, SapiAtlasModel, SapiSpaceModel, SapiParcellationModel } from "src/atlasComponents/sapi"
+import { SAPI, SAPIModule, SapiRegionModel, SapiAtlasModel, SapiSpaceModel, SapiParcellationModel } from "src/atlasComponents/sapi"
 import { IDS } from "src/atlasComponents/sapi/constants"
 import { actions, selectors } from "."
 import { Effect } from "./effects"
 import * as mainActions from "../actions"
-import { take } from "rxjs/operators"
+import { atlasSelection } from ".."
 
 describe("> effects.ts", () => {
   describe("> Effect", () => {
@@ -40,7 +39,6 @@ describe("> effects.ts", () => {
 
         const sapisvc = TestBed.inject(SAPI)
         const regions = await sapisvc.getParcRegions(IDS.ATLAES.HUMAN, IDS.PARCELLATION.JBA29, IDS.TEMPLATES.MNI152).toPromise()
-  
         hoc1left = regions.find(r => /hoc1/i.test(r.name) && /left/i.test(r.name))
         if (!hoc1left) throw new Error(`cannot find hoc1 left`)
         hoc1leftCentroid = JSON.parse(JSON.stringify(hoc1left)) 
@@ -94,6 +92,98 @@ describe("> effects.ts", () => {
       })
 
     })
+
+    describe("> onTemplateParcSelectionPostHook", () => {
+      describe("> 0", () => {
+      })
+      describe("> 1", () => {
+        const currNavigation = {
+          orientation: [0, 0, 0, 1],
+          perspectiveOrientation: [0, 0, 0, 1],
+          perspectiveZoom: 1,
+          position: [1, 2, 3], 
+          zoom: 1
+        }
+        beforeEach(() => {
+          const store = TestBed.inject(MockStore)
+          store.overrideSelector(atlasSelection.selectors.navigation, currNavigation)
+        })
+        describe("> when atlas is different", () => {
+          describe("> if no atlas prior", () => {
+
+            it("> navigation should be reset", () => {
+              const effects = TestBed.inject(Effect)
+              const hook = effects.onTemplateParcSelectionPostHook[1]
+              const obs = hook({
+                current: {
+                  atlas: null,
+                  parcellation: null,
+                  template: null
+                },
+                previous: {
+                  atlas: {
+                    "@id": IDS.ATLAES.RAT
+                  } as any,
+                  parcellation: {
+                    "@id": IDS.PARCELLATION.WAXHOLMV4
+                  } as any,
+                  template: {
+                    "@id": IDS.TEMPLATES.WAXHOLM
+                  } as any,
+                }
+              })
+
+              expect(obs).toBeObservable(
+                hot('(a|)', {
+                  a: {
+                    navigation: null
+                  }
+                })
+              )
+            })
+          })
+          describe("> if different atlas prior", () => {
+
+            it("> navigation should be reset", () => {
+              const effects = TestBed.inject(Effect)
+              const hook = effects.onTemplateParcSelectionPostHook[1]
+              const obs = hook({
+                current: {
+                  atlas: {
+                    "@id": IDS.ATLAES.HUMAN
+                  } as any,
+                  parcellation: {
+                    "@id": IDS.PARCELLATION.JBA29
+                  } as any,
+                  template: {
+                    "@id": IDS.TEMPLATES.MNI152
+                  } as any,
+                },
+                previous: {
+                  atlas: {
+                    "@id": IDS.ATLAES.RAT
+                  } as any,
+                  parcellation: {
+                    "@id": IDS.PARCELLATION.WAXHOLMV4
+                  } as any,
+                  template: {
+                    "@id": IDS.TEMPLATES.WAXHOLM
+                  } as any,
+                }
+              })
+
+              expect(obs).toBeObservable(
+                hot('(a|)', {
+                  a: {
+                    navigation: null
+                  }
+                })
+              )
+            })
+          })
+        })
+      })
+    })
   
     describe('> if selected atlas has no matching tmpl space', () => {
 
diff --git a/src/state/atlasSelection/effects.ts b/src/state/atlasSelection/effects.ts
index 5461f7dd8..54fe35a6a 100644
--- a/src/state/atlasSelection/effects.ts
+++ b/src/state/atlasSelection/effects.ts
@@ -52,6 +52,16 @@ export class Effect {
     ({ current, previous }) => {
       const prevSpcName = InterSpaceCoordXformSvc.TmplIdToValidSpaceName(previous?.template?.["@id"])
       const currSpcName = InterSpaceCoordXformSvc.TmplIdToValidSpaceName(current?.template?.["@id"])
+
+      /**
+       * if trans-species, return default state for navigation
+       */
+       if (previous?.atlas?.["@id"] !== current?.atlas?.["@id"]) {
+        return of({
+          navigation: null
+        })
+      }
+
       /**
        * if either space name is undefined, return default state for navigation
        */
@@ -435,6 +445,5 @@ export class Effect {
     private store: Store,
     private interSpaceCoordXformSvc: InterSpaceCoordXformSvc,
   ){
-
   }
 }
\ No newline at end of file
-- 
GitLab