All files / src/test-utils mocks.ts

65.21% Statements 15/23
30% Branches 3/10
100% Functions 8/8
70% Lines 14/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73    6x       6x       7x       9x       9x       2x       6x 26x                   6x       12x 12x                         14x 14x                       6x        
import { AchievementConfiguration, AchievementMetricValue, AchievementState } from '../types';
 
export class LocalStorageMock {
  private store: { [key: string]: string };
 
  constructor() {
    this.store = {};
  }
 
  clear() {
    this.store = {};
  }
 
  getItem(key: string) {
    return this.store[key] || null;
  }
 
  setItem(key: string, value: string) {
    this.store[key] = String(value);
  }
 
  removeItem(key: string) {
    delete this.store[key];
  }
}
 
const convertToNumber = (value: AchievementMetricValue): number => {
  if (typeof value === 'number') return value;
  Iif (typeof value === 'boolean') return value ? 1 : 0;
  Iif (value instanceof Date) return value.getTime();
  Iif (typeof value === 'string') {
    const parsed = parseFloat(value);
    return isNaN(parsed) ? 0 : parsed;
  }
  return 0;
};
 
export const mockAchievementConfig: AchievementConfiguration = {
  level: [
    {
      isConditionMet: (value: AchievementMetricValue, state?: AchievementState) => {
        const numValue = convertToNumber(value);
        return numValue >= 1;
      },
      achievementDetails: {
        achievementId: 'level_1',
        achievementTitle: 'Novice',
        achievementDescription: 'Reached level 1',
        achievementIconKey: 'star',
      },
    },
  ],
  score: [
    {
      isConditionMet: (value: AchievementMetricValue, state?: AchievementState) => {
        const numValue = convertToNumber(value);
        return numValue >= 100;
      },
      achievementDetails: {
        achievementId: 'high_score',
        achievementTitle: 'High Score',
        achievementDescription: 'Scored 100 points',
        achievementIconKey: 'trophy',
      },
    },
  ],
};
 
export const mockInitialState = {
  level: [1],
  score: [0],
  previouslyAwardedAchievements: [],
};