From e4dabf49963e7df7150c6ced84a303ab6ae01dbf Mon Sep 17 00:00:00 2001
From: paf <paf@titelfrei.de>
Date: Mon, 12 Apr 2021 12:00:55 +0200
Subject: [PATCH] changes the selection/check behaviour so, that after a block
 is selected (the first time) the checked sessions are kept and the clicked
 one is added if not there

---
 .../group-monitor/group-monitor.component.ts  | 10 +--
 .../group-monitor.service.spec.ts             | 85 ++++++++++++++++++-
 .../group-monitor/group-monitor.service.ts    |  3 +-
 3 files changed, 88 insertions(+), 10 deletions(-)

diff --git a/src/app/group-monitor/group-monitor.component.ts b/src/app/group-monitor/group-monitor.component.ts
index ccd98042..1c101622 100644
--- a/src/app/group-monitor/group-monitor.component.ts
+++ b/src/app/group-monitor/group-monitor.component.ts
@@ -155,6 +155,7 @@ export class GroupMonitorComponent implements OnInit, OnDestroy {
       session.booklet.species.length *
       session.booklet.species.charCodeAt(0) *
       session.booklet.species.charCodeAt(session.booklet.species.length / 4) *
+      session.booklet.species.charCodeAt(session.booklet.species.length / 4) *
       session.booklet.species.charCodeAt(session.booklet.species.length / 2) *
       session.booklet.species.charCodeAt(3 * (session.booklet.species.length / 4)) *
       session.booklet.species.charCodeAt(session.booklet.species.length - 1)
@@ -222,19 +223,16 @@ export class GroupMonitorComponent implements OnInit, OnDestroy {
     if (!isBooklet(this.selectedElement.originSession.booklet)) {
       return;
     }
-    // if (!this.selectedElement.element.nextBlockId) {
-    //   return;
-    // }
-    this.selectElement({
+    this.selectedElement = {
       element: this.selectedElement.element.nextBlockId ?
         BookletService.getBlockById(
           this.selectedElement.element.nextBlockId,
           this.selectedElement.originSession.booklet
         ) : null,
-      inversion: this.selectedElement.inversion,
+      inversion: false,
       originSession: this.selectedElement.originSession,
       spreading: this.selectedElement.spreading
-    });
+    };
   }
 
   unlockCommand(): void {
diff --git a/src/app/group-monitor/group-monitor.service.spec.ts b/src/app/group-monitor/group-monitor.service.spec.ts
index eb2ae757..610d30a7 100644
--- a/src/app/group-monitor/group-monitor.service.spec.ts
+++ b/src/app/group-monitor/group-monitor.service.spec.ts
@@ -32,7 +32,7 @@ class MockBookletService {
 
 class MockBackendService {
   observeSessionsMonitor(): Observable<TestSessionData[]> {
-    return of(unitTestExampleSessions.map(s => s.data));
+    return of([...unitTestExampleSessions, ...additionalUnitTestExampleSessions].map(s => s.data));
   }
 
   getGroupData(groupName: string): Observable<GroupData> {
@@ -238,7 +238,7 @@ describe('GroupMonitorService', () => {
   describe('groupForGoto', () => {
     it('return a group for each booklet in set an the the first unit in the selected block', () => {
       const selection: Selected = {
-        element: <Testlet>unitTestExampleBooklets.example_booklet_1.units.children[3], // alt = block-2
+        element: <Testlet>unitTestExampleBooklets.example_booklet_1.units.children[3], // alf = block-2
         inversion: false,
         originSession: unitTestExampleSessions[0],
         spreading: false
@@ -255,4 +255,85 @@ describe('GroupMonitorService', () => {
       // but another frist unit
     });
   });
+
+  describe('checkSessionsBySelection', () => {
+    it('should select all possible test-sessions after selecting a block when spreading is true', () => {
+      service.checkSessionsBySelection({
+        element: <Testlet>unitTestExampleBooklets.example_booklet_1.units.children[3], // alf = block-2
+        inversion: false,
+        originSession: unitTestExampleSessions[0],
+        spreading: true
+      });
+      expect(service.checked.map(s => s.data.testId)).toEqual([1, 33, 34]);
+
+      service.checkSessionsBySelection({
+        element: <Testlet>unitTestExampleBooklets.example_booklet_2.units.children[0], // zoe = block-1
+        inversion: false,
+        originSession: unitTestExampleSessions[1],
+        spreading: true
+      });
+      expect(service.checked.map(s => s.data.testId)).toEqual([2]);
+    });
+
+    it('should check the current test-session after selecting a block when spreading is false', () => {
+      service.checkSessionsBySelection({
+        element: <Testlet>unitTestExampleBooklets.example_booklet_1.units.children[3], // alf = block-2
+        inversion: false,
+        originSession: unitTestExampleSessions[0],
+        spreading: false
+      });
+      expect(service.checked.map(s => s.data.testId)).toEqual([1]);
+      service.checkSessionsBySelection({
+        element: <Testlet>unitTestExampleBooklets.example_booklet_2.units.children[0], // zoe = block-1
+        inversion: false,
+        originSession: additionalUnitTestExampleSessions[0],
+        spreading: false
+      });
+      expect(service.checked.map(s => s.data.testId)).toEqual([1, 33]);
+    });
+
+    it('should check possible test-sessions which where not checked before when inversion is true', () => {
+      service.checkSessionsBySelection({
+        element: <Testlet>unitTestExampleBooklets.example_booklet_1.units.children[3], // alf = block-2
+        inversion: true,
+        originSession: unitTestExampleSessions[0],
+        spreading: true
+      });
+      // nothing is checked, inversion checks all possible
+      expect(service.checked.map(s => s.data.testId)).toEqual([1, 33, 34]);
+
+      service.checkSessionsBySelection({
+        element: <Testlet>unitTestExampleBooklets.example_booklet_1.units.children[3], // alf = block-2
+        inversion: true,
+        originSession: unitTestExampleSessions[0],
+        spreading: true
+      });
+      // all possible where checked, so nothing remains
+      expect(service.checked.map(s => s.data.testId)).toEqual([]);
+
+      // eslint-disable-next-line @typescript-eslint/dot-notation
+      service['replaceCheckedSessions']([unitTestExampleSessions[0], additionalUnitTestExampleSessions[0]]);
+
+      service.checkSessionsBySelection({
+        element: <Testlet>unitTestExampleBooklets.example_booklet_1.units.children[3], // alf = block-2
+        inversion: true,
+        originSession: unitTestExampleSessions[0],
+        spreading: true
+      });
+      // test 1 and test 33 where checked, so 4 will be the inversion
+      expect(service.checked.map(s => s.data.testId)).toEqual([34]);
+    });
+
+    it('should ignore inversion, when spreading is set to false', () => {
+      // eslint-disable-next-line @typescript-eslint/dot-notation
+      service['replaceCheckedSessions']([unitTestExampleSessions[0], additionalUnitTestExampleSessions[0]]);
+      service.checkSessionsBySelection({
+        element: <Testlet>unitTestExampleBooklets.example_booklet_1.units.children[3], // alf = block-2
+        inversion: true,
+        originSession: unitTestExampleSessions[0],
+        spreading: false
+      });
+      expect(service.checked.map(s => s.data.testId)).toEqual([1, 33]);
+    });
+  });
 });
diff --git a/src/app/group-monitor/group-monitor.service.ts b/src/app/group-monitor/group-monitor.service.ts
index 4db7e54f..ecad79e1 100644
--- a/src/app/group-monitor/group-monitor.service.ts
+++ b/src/app/group-monitor/group-monitor.service.ts
@@ -346,7 +346,6 @@ export class GroupMonitorService {
     return (typeof this._checked[session.data.testId] !== 'undefined');
   }
 
-  // todo unit test
   checkSessionsBySelection(selected: Selected): void {
     if (this.checkingOptions.autoCheckAll) {
       return;
@@ -354,7 +353,7 @@ export class GroupMonitorService {
     let toCheck: TestSession[] = [];
     if (selected.element) {
       if (!selected.spreading) {
-        toCheck = [selected.originSession];
+        toCheck = [...this.checked, selected.originSession];
       } else {
         toCheck = this._sessions$.getValue()
           .filter(session => (session.booklet.species === selected.originSession.booklet.species))
-- 
GitLab