import { date, datatype } from 'faker' import { BioimpedanceWithWeight } from '../models' import { CompetitionCompetitor, CompetitionHistory } from '../../../models/ranking' import { IncreaseLeanMassCalculator } from './increase-lean-mass.calculator' describe('IncreaseLeanMassCalculator', () => { describe('#calcule', () => { const bioimpedance = BioimpedanceWithWeight.toInstance({}) const competitor = CompetitionCompetitor.toInstance({}) let smm = 0 let history: CompetitionHistory[] beforeEach(() => { bioimpedance.smm = smm competitor.history = history }) describe('when the value is null', () => { beforeAll(() => { smm = null }) it('returns the values with 0', () => { expect(IncreaseLeanMassCalculator.calcule({ bioimpedance, competitor })).toEqual({ value: 0, score: 0 }) }) }) describe('when the value is undefined', () => { beforeAll(() => { smm = undefined }) it('returns the values with 0', () => { expect(IncreaseLeanMassCalculator.calcule({ bioimpedance, competitor })).toEqual({ value: 0, score: 0 }) }) }) describe('when is the first score', () => { beforeAll(() => { smm = 30 history = [] }) it('returns with the value and score 0', () => { expect(IncreaseLeanMassCalculator.calcule({ bioimpedance, competitor })).toEqual({ value: smm, score: 0 }) }) }) describe('when is not the first score', () => { let previousValue: number beforeAll(() => { smm = 30 previousValue = datatype.number({ min: 1, max: smm }) history = [CompetitionHistory.toInstance({ done_at: new Date(), value: previousValue })] }) it('returns with the value and score calculed', () => { const score = ((previousValue - smm) * 100) / previousValue expect(IncreaseLeanMassCalculator.calcule({ bioimpedance, competitor })).toEqual({ value: smm, score: score && -score, }) }) describe('when has more than one history', () => { beforeAll(() => { smm = 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 - smm) * 100) / previousValue expect(IncreaseLeanMassCalculator.calcule({ bioimpedance, competitor })).toEqual({ value: smm, score: score && -score, }) }) }) }) }) })