From 5a02478f848580837fdc21002e59a0b641278458 Mon Sep 17 00:00:00 2001
From: paf <paf@titelfrei.de>
Date: Wed, 7 Apr 2021 18:10:53 +0200
Subject: [PATCH] optimizes some unit tests

---
 .../group-monitor.service.spec.ts             | 48 +++++++++----------
 .../group-monitor/group-monitor.service.ts    |  5 +-
 2 files changed, 24 insertions(+), 29 deletions(-)

diff --git a/src/app/group-monitor/group-monitor.service.spec.ts b/src/app/group-monitor/group-monitor.service.spec.ts
index 5408c9cf..1e899c66 100644
--- a/src/app/group-monitor/group-monitor.service.spec.ts
+++ b/src/app/group-monitor/group-monitor.service.spec.ts
@@ -78,90 +78,86 @@ describe('GroupMonitorService', () => {
 
   describe('sortSessions', () => {
     it('should sort by bookletName alphabetically', () => {
-      const sorted = service.sortSessions({ active: 'bookletName', direction: 'asc' }, unitTestExampleSessions);
+      const sorted = service.sortSessions({ active: 'bookletName', direction: 'asc' }, [...unitTestExampleSessions]);
       expect(sorted.map(s => s.data.bookletName))
         .toEqual(['example_booklet_1', 'example_booklet_2', 'this_does_not_exist']);
     });
 
     it('should sort by bookletName alphabetically in reverse', () => {
-      const sorted = service.sortSessions({ active: 'bookletName', direction: 'desc' }, unitTestExampleSessions);
+      const sorted = service.sortSessions({ active: 'bookletName', direction: 'desc' }, [...unitTestExampleSessions]);
       expect(sorted.map(s => s.data.bookletName))
         .toEqual(['this_does_not_exist', 'example_booklet_2', 'example_booklet_1']);
     });
 
     it('should sort by personLabel alphabetically', () => {
-      const sorted = service.sortSessions({ active: 'personLabel', direction: 'asc' }, unitTestExampleSessions);
+      const sorted = service.sortSessions({ active: 'personLabel', direction: 'asc' }, [...unitTestExampleSessions]);
       expect(sorted.map(s => s.data.personLabel)).toEqual(['Person 1', 'Person 1', 'Person 2']);
     });
 
     it('should sort by personLabel alphabetically in reverse', () => {
-      const sorted = service.sortSessions({ active: 'personLabel', direction: 'desc' }, unitTestExampleSessions);
+      const sorted = service.sortSessions({ active: 'personLabel', direction: 'desc' }, [...unitTestExampleSessions]);
       expect(sorted.map(s => s.data.personLabel)).toEqual(['Person 2', 'Person 1', 'Person 1']);
     });
 
     it('should sort by timestamp', () => {
-      const sorted = service.sortSessions({ active: 'timestamp', direction: 'asc' }, unitTestExampleSessions);
+      const sorted = service.sortSessions({ active: 'timestamp', direction: 'asc' }, [...unitTestExampleSessions]);
       expect(sorted.map(s => s.data.timestamp)).toEqual([10000000, 10000300, 10000500]);
     });
 
     it('should sort by timestamp reverse', () => {
-      const sorted = service.sortSessions({ active: 'timestamp', direction: 'desc' }, unitTestExampleSessions);
+      const sorted = service.sortSessions({ active: 'timestamp', direction: 'desc' }, [...unitTestExampleSessions]);
       expect(sorted.map(s => s.data.timestamp)).toEqual([10000500, 10000300, 10000000]);
     });
 
     it('should sort by checked', () => {
-      const exampleSession1 = unitTestExampleSessions[1];
-      // sortSession does not only return sorted array, but sorts original array in-play as js function sort does
-      service.checkingOptions.autoCheckAll = false; // TODO if replaceCheckedSessions was not private, this
-      service.checkNone(); // could be written more straigtforward
-      service.checkSession(exampleSession1);
-      const sorted = service.sortSessions({ active: '_checked', direction: 'asc' }, unitTestExampleSessions);
-      expect(sorted[0].id).toEqual(exampleSession1.id);
+      // eslint-disable-next-line @typescript-eslint/dot-notation
+      service['replaceCheckedSessions']([unitTestExampleSessions[1]]);
+      const sorted = service.sortSessions({ active: '_checked', direction: 'asc' }, [...unitTestExampleSessions]);
+      expect(sorted[0].id).toEqual(unitTestExampleSessions[1].id);
     });
 
     it('should sort by checked reverse', () => {
-      const exampleSession1 = unitTestExampleSessions[1];
-      service.checkingOptions.autoCheckAll = false;
-      service.checkNone();
-      service.checkSession(exampleSession1);
-      const sorted = service.sortSessions({ active: '_checked', direction: 'desc' }, unitTestExampleSessions);
-      expect(sorted[2].id).toEqual(exampleSession1.id);
+      // eslint-disable-next-line @typescript-eslint/dot-notation
+      service['replaceCheckedSessions']([unitTestExampleSessions[1]]);
+      const sorted = service.sortSessions({ active: '_checked', direction: 'desc' }, [...unitTestExampleSessions]);
+      expect(sorted[2].id).toEqual(unitTestExampleSessions[1].id);
     });
 
     it('should sort by superstate', () => {
-      const sorted = service.sortSessions({ active: '_superState', direction: 'asc' }, unitTestExampleSessions);
+      const sorted = service.sortSessions({ active: '_superState', direction: 'asc' }, [...unitTestExampleSessions]);
       expect(sorted.map(s => s.state)).toEqual(['pending', 'paused', 'idle']);
     });
 
     it('should sort by superstate reverse', () => {
-      const sorted = service.sortSessions({ active: '_superState', direction: 'desc' }, unitTestExampleSessions);
+      const sorted = service.sortSessions({ active: '_superState', direction: 'desc' }, [...unitTestExampleSessions]);
       expect(sorted.map(s => s.state)).toEqual(['idle', 'paused', 'pending']);
     });
 
     it('should sort by currentBlock', () => {
-      const sorted = service.sortSessions({ active: '_currentBlock', direction: 'asc' }, unitTestExampleSessions);
+      const sorted = service.sortSessions({ active: '_currentBlock', direction: 'asc' }, [...unitTestExampleSessions]);
       expect(sorted.map(s => (s.current ? s.current.ancestor.blockId : '--'))).toEqual(['block-1', 'block-3', '--']);
     });
 
     it('should sort by currentBlock reverse', () => {
-      const sorted = service.sortSessions({ active: '_currentBlock', direction: 'desc' }, unitTestExampleSessions);
+      const sorted = service.sortSessions({ active: '_currentBlock', direction: 'desc' }, [...unitTestExampleSessions]);
       expect(sorted.map(s => (s.current ? s.current.ancestor.blockId : '--'))).toEqual(['--', 'block-3', 'block-1']);
     });
 
     it('should sort by currentUnit label alphabetically', () => {
-      const sorted = service.sortSessions({ active: '_currentUnit', direction: 'asc' }, unitTestExampleSessions);
+      const sorted = service.sortSessions({ active: '_currentUnit', direction: 'asc' }, [...unitTestExampleSessions]);
       expect(sorted.map(s => (s.current ? s.current.unit.id : '--'))).toEqual(['unit-1', 'unit-10', '--']);
     });
 
     it('should sort by currentUnit label alphabetically reverse', () => {
-      const sorted = service.sortSessions({ active: '_currentUnit', direction: 'desc' }, unitTestExampleSessions);
+      const sorted = service.sortSessions({ active: '_currentUnit', direction: 'desc' }, [...unitTestExampleSessions]);
       expect(sorted.map(s => (s.current ? s.current.unit.id : '--'))).toEqual(['--', 'unit-10', 'unit-1']);
     });
   });
 
   describe('getSessionSetStats', () => {
     it('should fetch correct stats from sessions', () => {
-      const result = GroupMonitorService.getSessionSetStats(unitTestExampleSessions, 2);
+      // eslint-disable-next-line @typescript-eslint/dot-notation
+      const result = GroupMonitorService['getSessionSetStats'](unitTestExampleSessions, 2);
       const expectation: TestSessionSetStats = {
         number: 3,
         differentBooklets: 3,
diff --git a/src/app/group-monitor/group-monitor.service.ts b/src/app/group-monitor/group-monitor.service.ts
index fe465c5e..8d4c42c8 100644
--- a/src/app/group-monitor/group-monitor.service.ts
+++ b/src/app/group-monitor/group-monitor.service.ts
@@ -416,8 +416,7 @@ export class GroupMonitorService {
     this._checkedStats$.next(GroupMonitorService.getSessionSetStats(this.checked, this.sessions.length));
   }
 
-  // TODO only public for test
-  static getSessionSetStats(sessionSet: TestSession[], allCount: number = sessionSet.length): TestSessionSetStats {
+  private static getSessionSetStats(sessionSet: TestSession[], all: number = sessionSet.length): TestSessionSetStats {
     const booklets = new Set();
     const bookletSpecies = new Set();
     let paused = 0;
@@ -435,7 +434,7 @@ export class GroupMonitorService {
       number: sessionSet.length,
       differentBooklets: booklets.size,
       differentBookletSpecies: bookletSpecies.size,
-      all: (allCount === sessionSet.length),
+      all: (all === sessionSet.length),
       paused,
       locked
     };
-- 
GitLab