From 50c838e0f7db12768ea17f9b6f6040a1a4f05669 Mon Sep 17 00:00:00 2001 From: Jiri Pokorny Date: Mon, 15 Apr 2024 18:41:27 +0200 Subject: [PATCH] Fixed not reloaded dive results --- .../diff/results-comparison.service.spec.ts | 97 +++++++++++++++++++ .../shared/diff/results-comparison.service.ts | 32 +++--- 2 files changed, 115 insertions(+), 14 deletions(-) create mode 100644 projects/planner/src/app/shared/diff/results-comparison.service.spec.ts diff --git a/projects/planner/src/app/shared/diff/results-comparison.service.spec.ts b/projects/planner/src/app/shared/diff/results-comparison.service.spec.ts new file mode 100644 index 00000000..5f6ba58c --- /dev/null +++ b/projects/planner/src/app/shared/diff/results-comparison.service.spec.ts @@ -0,0 +1,97 @@ +import { TestBed } from '@angular/core/testing'; +import { DiveSchedules } from '../dive.schedules'; +import { ProfileComparatorService } from './profileComparatorService'; +import { ReloadDispatcher } from '../reloadDispatcher'; +import { UnitConversion } from '../UnitConversion'; +import { ResultDiff, ResultsComparison } from './results-comparison.service'; +import { DiveResults } from '../diveresults'; + +describe('ResultsComparison service current values', () => { + let sut: ResultsComparison; + let schedules: DiveSchedules; + let profileB: DiveResults; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [], + imports: [], + providers: [ + ProfileComparatorService, UnitConversion, + ReloadDispatcher, DiveSchedules, + ResultsComparison + ] + }).compileComponents(); + }); + + beforeEach(() => { + sut = TestBed.inject(ResultsComparison); + schedules = TestBed.inject(DiveSchedules); + + schedules.add(); + profileB = schedules.add().diveResult; + const selection = TestBed.inject(ProfileComparatorService); + selection.selectProfile(2); + }); + + const assertResultsDiff = (resultsDiff: ResultDiff, expectedB: number) => { + expect(resultsDiff.valueA).toBeCloseTo(0, 6); + expect(resultsDiff.valueB).toBeCloseTo(expectedB, 6); + }; + + it('cns', () => { + profileB.cns = 80; + assertResultsDiff(sut.cns, 80); + }); + + it('otu', () => { + profileB.otu = 81; + assertResultsDiff(sut.otu, 81); + }); + + it('Time to surface', () => { + profileB.timeToSurface = 82; + assertResultsDiff(sut.timeToSurface, 82); + }); + + it('Emergency ascent starts', () => { + profileB.emergencyAscentStart = 83; + assertResultsDiff(sut.emergencyAscentStart, 83); + }); + + it('No deco limit', () => { + profileB.noDecoTime = 84; + assertResultsDiff(sut.noDeco, 84); + }); + + it('Max time', () => { + profileB.maxTime = 85; + assertResultsDiff(sut.maxTime, 85); + }); + + it('Average depth', () => { + profileB.averageDepth = 86; + assertResultsDiff(sut.averageDepth, 86); + }); + + it('Max density', () => { + profileB.highestDensity.density = 6; + assertResultsDiff(sut.highestDensity, 6); + }); + + describe('Imperial units', () => { + beforeEach(() => { + const units = TestBed.inject(UnitConversion); + units.imperialUnits = true; + }); + + it('Average depth ft', () => { + profileB.averageDepth = 30; + assertResultsDiff(sut.averageDepth, 98.4251969); + }); + + it('Max density lb/cuft', () => { + profileB.highestDensity.density = 6; + assertResultsDiff(sut.highestDensity, 0.374568); + }); + }); +}); diff --git a/projects/planner/src/app/shared/diff/results-comparison.service.ts b/projects/planner/src/app/shared/diff/results-comparison.service.ts index 9cd52c56..fb322bc9 100644 --- a/projects/planner/src/app/shared/diff/results-comparison.service.ts +++ b/projects/planner/src/app/shared/diff/results-comparison.service.ts @@ -8,24 +8,28 @@ import { faArrowDown, faArrowUp, faMinus, IconDefinition } from '@fortawesome/free-solid-svg-icons'; -class ResultDiff { +export class ResultDiff { private arrowUp: IconDefinition = faArrowUp; private arrowDown: IconDefinition = faArrowDown; private dash: IconDefinition = faMinus; + /** + * Provide lambdas to resolve current values when selection changes. + * We dont need to capture the results since recalculation is cheap. + * */ constructor( - private profileA: DiveResults, - private profileB: DiveResults, + private profileA: () => DiveResults, + private profileB: () => DiveResults, private betterDirection: number, private valueAccessor: (result: DiveResults) => number, ) { } public get valueA(): number { - return this.valueAccessor(this.profileA); + return this.valueAccessor(this.profileA()); } public get valueB(): number { - return this.valueAccessor(this.profileB); + return this.valueAccessor(this.profileB()); } public get difference(): number { @@ -61,23 +65,23 @@ class ResultDiff { @Injectable() export class ResultsComparison { - public totalDuration = new ResultDiff(this.profileA, this.profileB, 1, + public totalDuration = new ResultDiff(() => this.profileA, () => this.profileB, 1, d => d.totalDuration); - public timeToSurface = new ResultDiff(this.profileA, this.profileB, -1, + public timeToSurface = new ResultDiff(() => this.profileA, () => this.profileB, -1, d => d.timeToSurface); - public averageDepth = new ResultDiff(this.profileA, this.profileB, -1, + public averageDepth = new ResultDiff(() => this.profileA, () => this.profileB, -1, d => this.units.fromMeters(d.averageDepth)); - public emergencyAscentStart = new ResultDiff(this.profileA, this.profileB, -1, + public emergencyAscentStart = new ResultDiff(() => this.profileA, () => this.profileB, -1, d => d.emergencyAscentStart); - public noDeco = new ResultDiff(this.profileA, this.profileB, 1, + public noDeco = new ResultDiff(() => this.profileA, () => this.profileB, 1, d => d.noDecoTime); - public maxTime = new ResultDiff(this.profileA, this.profileB, 1, + public maxTime = new ResultDiff(() => this.profileA, () => this.profileB, 1, d => d.maxTime); - public highestDensity = new ResultDiff(this.profileA, this.profileB, -1, + public highestDensity = new ResultDiff(() => this.profileA, () => this.profileB, -1, d => this.density(d)); - public otu = new ResultDiff(this.profileA, this.profileB, -1, + public otu = new ResultDiff(() => this.profileA, () => this.profileB, -1, d => d.otu); - public cns = new ResultDiff(this.profileA, this.profileB, -1, + public cns = new ResultDiff(() => this.profileA, () => this.profileB, -1, d => d.cns); private readonly maxCns = 1000;