import {TestBed} from '@angular/core/testing'; import {ScoreStarsService} from './score-stars.service'; import {MicroLessonMetrics, ExerciseMetrics, AnswerMetric, ExerciseMetricsBonusTime} from 'ox-core'; describe('ScoreStarsService', () => { beforeEach(() => TestBed.configureTestingModule({})); it('should be created', () => { const service: ScoreStarsService = TestBed.get(ScoreStarsService); expect(service).toBeTruthy(); }); it('should failed if you ask for star count without setting the score', () => { const service: ScoreStarsService = TestBed.get(ScoreStarsService); expect(function () { service.calculateStars({} as MicroLessonMetrics); }).toThrow(new Error('You can\' t set the star count if you haven\'t set the score!')); }); it('should calculate correctly generic star count', () => { // 1 const service: ScoreStarsService = TestBed.get(ScoreStarsService); expect(service['defaultStarCalculator'](100)).toBe(1); expect(service['defaultStarCalculator'](500)).toBe(1); expect(service['defaultStarCalculator'](2000)).toBe(1); // 2 expect(service['defaultStarCalculator'](3000)).toBe(2); expect(service['defaultStarCalculator'](4000)).toBe(2); expect(service['defaultStarCalculator'](5000)).toBe(2); // 3 expect(service['defaultStarCalculator'](6000)).toBe(3); expect(service['defaultStarCalculator'](7000)).toBe(3); expect(service['defaultStarCalculator'](7400)).toBe(3); // 4 expect(service['defaultStarCalculator'](7500)).toBe(4); expect(service['defaultStarCalculator'](8500)).toBe(4); expect(service['defaultStarCalculator'](8999)).toBe(4); // 5 expect(service['defaultStarCalculator'](9000)).toBe(5); expect(service['defaultStarCalculator'](9500)).toBe(5); expect(service['defaultStarCalculator'](10000)).toBe(5); }); it('Standard metrics score with just one answer and correct is 10.000', () => { const service: ScoreStarsService = TestBed.get(ScoreStarsService); const metrics: MicroLessonMetrics = new MicroLessonMetrics(); metrics.exerciseMetrics = [ new ExerciseMetrics([], 0, 0, new Date(), new ExerciseMetricsBonusTime(20, 30), {}, [ new AnswerMetric('', new Date(), 'right'), ], [], false, [], new Date()) ]; service.calculateScore(metrics); expect(metrics.score).toBe(10000); }); it('Standard metrics score is correctly calculated! with 4 ejs', () => { const service: ScoreStarsService = TestBed.get(ScoreStarsService); const metrics: MicroLessonMetrics = new MicroLessonMetrics(); const correctExMetric = new ExerciseMetrics([], 0, 0, new Date(), new ExerciseMetricsBonusTime(20, 30), {}, [new AnswerMetric('', new Date(), 'right'), ], [], false, [], new Date()); const oneWrongMetric = new ExerciseMetrics([], 0, 0, new Date(), new ExerciseMetricsBonusTime(20, 30), {}, [new AnswerMetric('', new Date(), 'wrong'), new AnswerMetric('', new Date(), 'right')], [], false, [], new Date()); metrics.exerciseMetrics = [ correctExMetric, correctExMetric, correctExMetric, oneWrongMetric ]; service.calculateScore(metrics); expect(metrics.score).toBe(6000); }); it('Standard metrics score is correctly calculated! with 8 ejs', () => { const service: ScoreStarsService = TestBed.get(ScoreStarsService); const metrics: MicroLessonMetrics = new MicroLessonMetrics(); const correctExMetric = new ExerciseMetrics([], 0, 0, new Date(), new ExerciseMetricsBonusTime(20, 30), {}, [new AnswerMetric('', new Date(), 'right'), ], [], false, [], new Date()); const oneWrongMetric = new ExerciseMetrics([], 0, 0, new Date(), new ExerciseMetricsBonusTime(20, 30), {}, [new AnswerMetric('', new Date(), 'wrong'), new AnswerMetric('', new Date(), 'right')], [], false, [], new Date()); const doubleWrong = new ExerciseMetrics([], 0, 0, new Date(), new ExerciseMetricsBonusTime(20, 30), {}, [new AnswerMetric('', new Date(), 'wrong'), new AnswerMetric('', new Date(), 'wrong'), new AnswerMetric('', new Date(), 'right')], [], false, [], new Date()); metrics.exerciseMetrics = [ correctExMetric, correctExMetric, correctExMetric, correctExMetric, oneWrongMetric, doubleWrong, doubleWrong, doubleWrong, ]; service.calculateScore(metrics); expect(metrics.score).toBe(6000); }); it('Standard metrics score is correctly calculated! with 10 ejs', () => { const service: ScoreStarsService = TestBed.get(ScoreStarsService); const metrics: MicroLessonMetrics = new MicroLessonMetrics(); const correctExMetric = new ExerciseMetrics([], 0, 0, new Date(), new ExerciseMetricsBonusTime(20, 30), {}, [new AnswerMetric('', new Date(), 'right'), ], [], false, [], new Date()); const oneWrongMetric = new ExerciseMetrics([], 0, 0, new Date(), new ExerciseMetricsBonusTime(20, 30), {}, [new AnswerMetric('', new Date(), 'wrong'), new AnswerMetric('', new Date(), 'right')], [], false, [], new Date()); const doubleWrong = new ExerciseMetrics([], 0, 0, new Date(), new ExerciseMetricsBonusTime(20, 30), {}, [new AnswerMetric('', new Date(), 'wrong'), new AnswerMetric('', new Date(), 'wrong'), new AnswerMetric('', new Date(), 'right')], [], true, [], new Date()); const surrender = new ExerciseMetrics([], 0, 0, new Date(), new ExerciseMetricsBonusTime(20, 30), {}, [new AnswerMetric('', new Date(), 'wrong'), new AnswerMetric('', new Date(), 'wrong')], [], true, [], new Date()); metrics.exerciseMetrics = [ correctExMetric, correctExMetric, correctExMetric, correctExMetric, correctExMetric, oneWrongMetric, oneWrongMetric, doubleWrong, doubleWrong, surrender ]; service.calculateScore(metrics); expect(metrics.score).toBe(6000); }); });