Skip to content
Snippets Groups Projects
mesh.service.spec.ts 7.27 KiB
Newer Older
import { TestBed } from "@angular/core/testing"
import { MockStore, provideMockStore } from "@ngrx/store/testing"
import { hot } from "jasmine-marbles"
Xiao Gui's avatar
Xiao Gui committed
import { NehubaMeshService } from "./mesh.service"
Xiao Gui's avatar
Xiao Gui committed
import { atlasSelection } from "src/state"
Xiao Gui's avatar
Xiao Gui committed
import { SxplrRegion } from "src/atlasComponents/sapi/sxplrTypes"
Xiao Gui's avatar
Xiao Gui committed
import { LayerCtrlEffects } from "../layerCtrl.service/layerCtrl.effects"
import { NEVER, of, pipe } from "rxjs"
Xiao Gui's avatar
Xiao Gui committed
import { mapTo } from "rxjs/operators"
Xiao Gui's avatar
Xiao Gui committed
import { selectorAuxMeshes } from "../store"
import { HttpClientModule } from "@angular/common/http"
import { BaseService } from "../base.service/base.service"
Xiao Gui's avatar
Xiao Gui committed
const fits1 = {
  name: "foo-bar"
} as SxplrRegion
const fits2 = {
  name: "foobar-foobar"
} as SxplrRegion
Xiao Gui's avatar
Xiao Gui committed
const auxMesh = {
  "@id": 'bla',
  labelIndicies: [1,2,3],
  name: 'bla',
  ngId: 'bla',
  rgb: [255, 255, 255] as [number, number, number],
  visible: true,
  displayName: 'bla'
Xiao Gui's avatar
Xiao Gui committed
describe('> mesh.service.ts', () => {
Xiao Gui's avatar
Xiao Gui committed
  
Xiao Gui's avatar
Xiao Gui committed
  
Xiao Gui's avatar
Xiao Gui committed
  let getATPSpy: jasmine.Spy = jasmine.createSpy('distinctATP')
Xiao Gui's avatar
Xiao Gui committed
  const mockAtlas = {
    '@id': 'mockAtlas'
  }
  const mockTmpl = {
    '@id': 'mockTmpl'
  }
  const mockParc = {
    '@id': 'mockParc'
  }
Xiao Gui's avatar
Xiao Gui committed
  beforeEach(() => {
    getATPSpy = spyOn(atlasSelection.fromRootStore, 'distinctATP')
    getATPSpy.and.returnValue(
      pipe(
        mapTo({
          atlas: mockAtlas,
          parcellation: mockParc,
          template: mockTmpl
        })
      )
    )
  })
Xiao Gui's avatar
Xiao Gui committed
    
    getATPSpy.calls.reset()
  })
  describe('> NehubaMeshService', () => {
    beforeEach(() => {
      TestBed.configureTestingModule({
        imports: [
          HttpClientModule,
        ],
        providers: [
          provideMockStore(),
          NehubaMeshService,
Xiao Gui's avatar
Xiao Gui committed
          {
            provide: LayerCtrlEffects,
            useValue: {
              onATPDebounceNgLayers$: NEVER
            }
          },
          {
            provide: BaseService,
            useValue: {
              completeNgIdLabelRegionMap$: NEVER
            }
Xiao Gui's avatar
Xiao Gui committed
          }
        ]
      })
    })

    it('> can be init', () => {
      const service = TestBed.inject(NehubaMeshService)
      expect(service).toBeTruthy()
    })

    describe("> loadMeshes$", () => {
Xiao Gui's avatar
Xiao Gui committed

Xiao Gui's avatar
Xiao Gui committed
      const ngId = 'blabla'
      const labelIndex = 12
      const ngId2 = 'blabla-foo-foo'
      const labelIndex2 = 123
Xiao Gui's avatar
Xiao Gui committed
      beforeEach(() => {
        const baseSvc = TestBed.inject(BaseService)
        baseSvc.completeNgIdLabelRegionMap$ = of({
          [ngId]: {
            [labelIndex]: fits1
          },
          [ngId2]: {
            [labelIndex2]: fits2
          }
        })
Xiao Gui's avatar
Xiao Gui committed
        const mockStore = TestBed.inject(MockStore)
        
        mockStore.overrideSelector(atlasSelection.selectors.selectedTemplate, {} as any)
        mockStore.overrideSelector(atlasSelection.selectors.selectedParcellation, {} as any)
Xiao Gui's avatar
Xiao Gui committed
      })
Xiao Gui's avatar
Xiao Gui committed
      describe("> auxMesh defined", () => {
Xiao Gui's avatar
Xiao Gui committed
        beforeEach(() => {
          const mockStore = TestBed.inject(MockStore)
          mockStore.overrideSelector(selectorAuxMeshes, [auxMesh])
Xiao Gui's avatar
Xiao Gui committed
        describe("> no selected region", () => {
          beforeEach(() => {
            const mockStore = TestBed.inject(MockStore)
            mockStore.overrideSelector(atlasSelection.selectors.selectedRegions, [])
          })
          it('> show auxmesh only', () => {
Xiao Gui's avatar
Xiao Gui committed
            const service = TestBed.inject(NehubaMeshService)
            expect(
              service.loadMeshes$
            ).toBeObservable(
              hot('(abc)', {
                a: {
                  layer: {
                    name: ngId
                  },
                  labelIndicies:[]
Xiao Gui's avatar
Xiao Gui committed
                b: {
                  layer: {
                    name: ngId2
                  },
                  labelIndicies:[]
Xiao Gui's avatar
Xiao Gui committed
                c: {
                  layer: {
                    name: auxMesh.ngId,
                  },
                  labelIndicies: auxMesh.labelIndicies
                }
              })
            )
          })
Xiao Gui's avatar
Xiao Gui committed
        describe("> has selected region", () => {
          beforeEach(() => {
            const mockStore = TestBed.inject(MockStore)
            mockStore.overrideSelector(atlasSelection.selectors.selectedRegions, [fits1])
          })
          it("> shows both shown mesh and aux mesh", () => {
            
            const service = TestBed.inject(NehubaMeshService)
            expect(
              service.loadMeshes$
            ).toBeObservable(
              hot('(abc)', {
                a: {
                  layer: {
                    name: ngId
                  },
                  labelIndicies: [ labelIndex ]
                },
                b: {
                  layer: {
                    name: ngId2,
                  },
                  labelIndicies: []
                },
                c: {
                  layer: {
                    name: auxMesh.ngId,
                  },
                  labelIndicies: auxMesh.labelIndicies
                }
              })
            )
          })
        })
      })
Xiao Gui's avatar
Xiao Gui committed
      describe('> auxmesh not defined', () => {
        beforeEach(() => {
          const mockStore = TestBed.inject(MockStore)
          mockStore.overrideSelector(selectorAuxMeshes, [])
        })
Xiao Gui's avatar
Xiao Gui committed
        describe("> has no selected region", () => {
          beforeEach(() => {
            const mockStore = TestBed.inject(MockStore)
            mockStore.overrideSelector(atlasSelection.selectors.selectedRegions, [])
Xiao Gui's avatar
Xiao Gui committed
            mockStore.overrideSelector(atlasSelection.selectors.selectedTemplate, {} as any)
            mockStore.overrideSelector(atlasSelection.selectors.selectedParcellation, {} as any)
Xiao Gui's avatar
Xiao Gui committed
          })
Xiao Gui's avatar
Xiao Gui committed
          it("> load all meshes", () => {
            
            const service = TestBed.inject(NehubaMeshService)
            expect(
              service.loadMeshes$
            ).toBeObservable(
              hot('(ab)', {
                a: {
                  layer: {
                    name: ngId
                  },
                  labelIndicies: [ labelIndex ]
                },
                b: {
                  layer: {
                    name: ngId2,
                  },
                  labelIndicies: [ labelIndex2 ]
                }
              })
            )
Xiao Gui's avatar
Xiao Gui committed
        describe("> has selected region", () => {
          beforeEach(() => {
            const mockStore = TestBed.inject(MockStore)
            mockStore.overrideSelector(atlasSelection.selectors.selectedRegions, [fits1])
Xiao Gui's avatar
Xiao Gui committed
            mockStore.overrideSelector(atlasSelection.selectors.selectedTemplate, {} as any)
            mockStore.overrideSelector(atlasSelection.selectors.selectedParcellation, {} as any)
Xiao Gui's avatar
Xiao Gui committed
          it("> load only selected mesh", () => {
Xiao Gui's avatar
Xiao Gui committed
            const service = TestBed.inject(NehubaMeshService)
            expect(
              service.loadMeshes$
            ).toBeObservable(
              hot('(ab)', {
                a: {
                  layer: {
                    name: ngId
                  },
                  labelIndicies: [ labelIndex ]
                },
                b: {
                  layer: {
                    name: ngId2,
                  },
                  labelIndicies: [  ]
                }
              })
            )
          })