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