1+ import {
2+ getStoredCalibrationValue ,
3+ storeCalibrationValue ,
4+ getDefaultCalibrationValue ,
5+ getAllCalibrationData ,
6+ clearAllCalibrationData ,
7+ } from '@/utils/calibrationHelper' ;
8+ import { MeasurementUnit } from '@/types/shapes' ;
9+
10+ // Mock localStorage
11+ const localStorageMock = {
12+ getItem : jest . fn ( ) ,
13+ setItem : jest . fn ( ) ,
14+ removeItem : jest . fn ( ) ,
15+ clear : jest . fn ( ) ,
16+ } ;
17+
18+ Object . defineProperty ( window , 'localStorage' , {
19+ value : localStorageMock ,
20+ } ) ;
21+
22+ describe ( 'calibrationHelper' , ( ) => {
23+ beforeEach ( ( ) => {
24+ jest . clearAllMocks ( ) ;
25+ } ) ;
26+
27+ describe ( 'getDefaultCalibrationValue' , ( ) => {
28+ it ( 'should return correct defaults for each unit' , ( ) => {
29+ expect ( getDefaultCalibrationValue ( 'cm' ) ) . toBe ( 60 ) ;
30+ expect ( getDefaultCalibrationValue ( 'in' ) ) . toBe ( 152.4 ) ;
31+ } ) ;
32+
33+ it ( 'should return cm default for unknown units' , ( ) => {
34+ expect ( getDefaultCalibrationValue ( 'unknown' as MeasurementUnit ) ) . toBe ( 60 ) ;
35+ } ) ;
36+ } ) ;
37+
38+ describe ( 'getStoredCalibrationValue' , ( ) => {
39+ it ( 'should return stored value when available' , ( ) => {
40+ localStorageMock . getItem . mockReturnValue ( '80' ) ;
41+
42+ const result = getStoredCalibrationValue ( 'cm' ) ;
43+
44+ expect ( localStorageMock . getItem ) . toHaveBeenCalledWith ( 'geometry-canvas-cm' ) ;
45+ expect ( result ) . toBe ( 80 ) ;
46+ } ) ;
47+
48+ it ( 'should return default value when no stored value' , ( ) => {
49+ localStorageMock . getItem . mockReturnValue ( null ) ;
50+
51+ const result = getStoredCalibrationValue ( 'cm' ) ;
52+
53+ expect ( result ) . toBe ( 60 ) ; // default for cm
54+ } ) ;
55+
56+ it ( 'should return default value for invalid stored value' , ( ) => {
57+ localStorageMock . getItem . mockReturnValue ( 'invalid' ) ;
58+
59+ const result = getStoredCalibrationValue ( 'cm' ) ;
60+
61+ expect ( result ) . toBe ( 60 ) ; // default for cm
62+ } ) ;
63+ } ) ;
64+
65+ describe ( 'storeCalibrationValue' , ( ) => {
66+ beforeEach ( ( ) => {
67+ jest . clearAllMocks ( ) ;
68+ } ) ;
69+
70+ it ( 'should store valid calibration value' , ( ) => {
71+ storeCalibrationValue ( 'cm' , 80 ) ;
72+
73+ expect ( localStorageMock . setItem ) . toHaveBeenCalledWith ( 'geometry-canvas-cm' , '80' ) ;
74+ } ) ;
75+
76+ it ( 'should not store invalid calibration value' , ( ) => {
77+ storeCalibrationValue ( 'cm' , - 10 ) ;
78+
79+ // Should not call setItem for the calibration value, but isLocalStorageAvailable check still calls it
80+ expect ( localStorageMock . setItem ) . toHaveBeenCalledWith ( 'test' , 'test' ) ;
81+ expect ( localStorageMock . setItem ) . not . toHaveBeenCalledWith ( 'geometry-canvas-cm' , '-10' ) ;
82+ } ) ;
83+
84+ it ( 'should not store zero calibration value' , ( ) => {
85+ storeCalibrationValue ( 'cm' , 0 ) ;
86+
87+ // Should not call setItem for the calibration value, but isLocalStorageAvailable check still calls it
88+ expect ( localStorageMock . setItem ) . toHaveBeenCalledWith ( 'test' , 'test' ) ;
89+ expect ( localStorageMock . setItem ) . not . toHaveBeenCalledWith ( 'geometry-canvas-cm' , '0' ) ;
90+ } ) ;
91+ } ) ;
92+
93+ describe ( 'getAllCalibrationData' , ( ) => {
94+ it ( 'should return calibration data for all units' , ( ) => {
95+ localStorageMock . getItem . mockImplementation ( ( key ) => {
96+ if ( key === 'geometry-canvas-cm' ) return '80' ;
97+ if ( key === 'geometry-canvas-in' ) return '160' ;
98+ return null ;
99+ } ) ;
100+
101+ const result = getAllCalibrationData ( ) ;
102+
103+ expect ( result ) . toEqual ( {
104+ pixelsPerCm : 80 ,
105+ pixelsPerInch : 160 ,
106+ } ) ;
107+ } ) ;
108+ } ) ;
109+
110+ describe ( 'clearAllCalibrationData' , ( ) => {
111+ it ( 'should clear all stored calibration data' , ( ) => {
112+ clearAllCalibrationData ( ) ;
113+
114+ expect ( localStorageMock . removeItem ) . toHaveBeenCalledWith ( 'geometry-canvas-cm' ) ;
115+ expect ( localStorageMock . removeItem ) . toHaveBeenCalledWith ( 'geometry-canvas-in' ) ;
116+ } ) ;
117+ } ) ;
118+ } ) ;
0 commit comments