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