Skip to content

Commit

Permalink
Fixed not reloaded dive results
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkapok committed Apr 15, 2024
1 parent fa70de9 commit 50c838e
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
32 changes: 18 additions & 14 deletions projects/planner/src/app/shared/diff/results-comparison.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 50c838e

Please sign in to comment.