import { date, datatype } from 'faker' import { BioimpedanceWithWeight } from '../models' import { CompetitionCompetitor, CompetitionHistory } from '../../../models/ranking' import { GenericScoreCalculator } from './generic-score.calculator' class TestGenericScoreCalculator extends GenericScoreCalculator { protected static getValue(bioimpedance: BioimpedanceWithWeight): number { return bioimpedance?.bmi || 0 } } describe('GenericScoreCalculator', () => { describe('#calcule', () => { const bioimpedance = BioimpedanceWithWeight.toInstance({}) const competitor = CompetitionCompetitor.toInstance({}) let bmi = 0 let history: CompetitionHistory[] beforeEach(() => { bioimpedance.bmi = bmi competitor.history = history }) describe('when the value is null', () => { beforeAll(() => { bmi = null }) it('returns the values with 0', () => { expect(TestGenericScoreCalculator.calcule({ bioimpedance, competitor })).toEqual({ value: 0, score: 0 }) }) }) describe('when the value is undefined', () => { beforeAll(() => { bmi = undefined }) it('returns the values with 0', () => { expect(TestGenericScoreCalculator.calcule({ bioimpedance, competitor })).toEqual({ value: 0, score: 0 }) }) }) describe('when is the first score', () => { beforeAll(() => { bmi = 30 history = [] }) it('returns with the value and score 0', () => { expect(TestGenericScoreCalculator.calcule({ bioimpedance, competitor })).toEqual({ value: bmi, score: 0 }) }) }) describe('when is not the first score', () => { let previousValue: number beforeAll(() => { bmi = 30 previousValue = datatype.number({ min: bmi, max: 100 }) history = [CompetitionHistory.toInstance({ done_at: new Date(), value: previousValue })] }) it('returns with the value and score calculed', () => { const score = ((previousValue - bmi) * 100) / previousValue expect(TestGenericScoreCalculator.calcule({ bioimpedance, competitor })).toEqual({ value: bmi, score }) }) describe('when has more than one history', () => { beforeAll(() => { bmi = 30 history = [ CompetitionHistory.toInstance({ done_at: date.past(), value: previousValue }), CompetitionHistory.toInstance({ done_at: date.recent(), value: previousValue * 10 }), ] }) it('returns with the value and score calculed with the first value from history', () => { const score = ((previousValue - bmi) * 100) / previousValue expect(TestGenericScoreCalculator.calcule({ bioimpedance, competitor })).toEqual({ value: bmi, score }) }) }) }) }) })