From 2cddf26db20d4157bdd2a5f249d422e5ad6b8530 Mon Sep 17 00:00:00 2001
From: Jack Reed <phillipjreed@gmail.com>
Date: Sun, 11 Oct 2020 08:50:05 -0600
Subject: [PATCH] Add tests around sagas updates for window

---
 __tests__/src/sagas/windows.test.js | 44 +++++++++++++++++++++++++++++
 src/state/sagas/windows.js          |  3 +-
 2 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/__tests__/src/sagas/windows.test.js b/__tests__/src/sagas/windows.test.js
index e900713f1..81223e470 100644
--- a/__tests__/src/sagas/windows.test.js
+++ b/__tests__/src/sagas/windows.test.js
@@ -17,6 +17,7 @@ import {
 } from '../../../src/state/selectors';
 import { fetchManifests } from '../../../src/state/sagas/iiif';
 import {
+  determineAndShowCollectionDialog,
   fetchWindowManifest,
   setWindowDefaultSearchQuery,
   setWindowStartingCanvas,
@@ -30,6 +31,7 @@ import {
   setCollectionPath,
 } from '../../../src/state/sagas/windows';
 import fixture from '../../fixtures/version-2/019.json';
+import collectionFixture from '../../fixtures/version-2/collection.json';
 
 describe('window-level sagas', () => {
   describe('fetchWindowManifest', () => {
@@ -40,10 +42,12 @@ describe('window-level sagas', () => {
           manifestId: 'manifest.json',
         },
       };
+      const manifest = Utils.parseManifest(fixture);
 
       return expectSaga(fetchWindowManifest, action)
         .provide([
           [select(getManifests), {}],
+          [select(getManifestoInstance, { manifestId: 'manifest.json' }), manifest],
           [call(fetchManifests, 'manifest.json'), {}],
           [call(setWindowStartingCanvas, action)],
           [call(setWindowDefaultSearchQuery, action)],
@@ -66,9 +70,11 @@ describe('window-level sagas', () => {
           [call(setWindowStartingCanvas, action)],
           [call(setWindowDefaultSearchQuery, action)],
           [call(setCollectionPath, { manifestId: 'manifest.json', windowId: 'x' })],
+          [call(determineAndShowCollectionDialog, 'manifest.json', 'x')],
         ])
         .call(setWindowStartingCanvas, action)
         .call(setWindowDefaultSearchQuery, action)
+        .call(determineAndShowCollectionDialog, 'manifest.json', 'x')
         .run();
     });
   });
@@ -434,4 +440,42 @@ describe('window-level sagas', () => {
         .run();
     });
   });
+
+  describe('determineAndShowCollectionDialog', () => {
+    it('shows the collection dialog if it is a collection', () => {
+      const manifest = Utils.parseManifest(collectionFixture);
+
+      return expectSaga(determineAndShowCollectionDialog, 'manifest.json', 'x')
+        .provide([
+          [select(getManifestoInstance, { manifestId: 'manifest.json' }), manifest],
+        ])
+        .put.like({
+          action: {
+            collectionPath: [],
+            manifestId: 'manifest.json',
+            type: 'mirador/SHOW_WINDOW_COLLECTION_DIALOG',
+            windowId: 'x',
+          },
+        })
+        .run();
+    });
+
+    it('does nothing if not a collection', () => {
+      const manifest = Utils.parseManifest(fixture);
+
+      return expectSaga(determineAndShowCollectionDialog, 'manifest.json', 'x')
+        .provide([
+          [select(getManifestoInstance, { manifestId: 'manifest.json' }), manifest],
+        ])
+        .not.put.like({
+          action: {
+            collectionPath: [],
+            manifestId: 'manifest.json',
+            type: 'mirador/SHOW_WINDOW_COLLECTION_DIALOG',
+            windowId: 'x',
+          },
+        })
+        .run();
+    });
+  });
 });
diff --git a/src/state/sagas/windows.js b/src/state/sagas/windows.js
index 20a76ed73..57bb042ba 100644
--- a/src/state/sagas/windows.js
+++ b/src/state/sagas/windows.js
@@ -235,8 +235,7 @@ export function* fetchInfoResponses({ visibleCanvases: visibleCanvasIds, windowI
 /** */
 export function* determineAndShowCollectionDialog(manifestId, windowId) {
   const manifestoInstance = yield select(getManifestoInstance, { manifestId });
-  const isCollection = manifestoInstance.isCollection();
-  if (isCollection) {
+  if (manifestoInstance && manifestoInstance.isCollection()) {
     yield put(showWindowCollectionDialog(manifestId, [], windowId));
   }
 }
-- 
GitLab