1+ import { compress } from 'lz-string' ;
2+ import { STORAGE } from 'helpers/const' ;
13import * as StorageService from 'services/storage' ;
24
5+ jest . mock ( '../helpers/const' , ( ) => {
6+ const actual = jest . requireActual ( '../helpers/const' ) ;
7+ return {
8+ ...actual ,
9+ MAX_HISTORY_SIZE : 2 ,
10+ } ;
11+ } ) ;
12+
313describe ( 'Storage tests' , ( ) => {
414 const setItemSpy = jest . spyOn ( Storage . prototype , 'setItem' ) ;
15+ const today = '2025-05-26' ;
16+
17+ beforeEach ( ( ) => {
18+ jest . clearAllMocks ( ) ;
19+ jest . useFakeTimers ( ) ;
20+ jest . setSystemTime ( new Date ( today ) ) ;
21+ } ) ;
22+
23+ afterEach ( ( ) => {
24+ jest . useRealTimers ( ) ;
25+ } ) ;
526
627 it ( 'Save value to locale storage' , ( ) => {
728 StorageService . setLocalStorage ( 'mock' , 'value' ) ;
@@ -26,4 +47,69 @@ describe('Storage tests', () => {
2647 StorageService . clearLocalStorage ( 'value' ) ;
2748 expect ( removeItemSpy ) . toHaveBeenCalledWith ( 'value' ) ;
2849 } ) ;
50+
51+ describe ( 'getHistory' , ( ) => {
52+ const mockCode = 'console.log(1)' ;
53+ beforeEach ( jest . clearAllMocks ) ;
54+
55+ it ( 'return empty array when no history found' , ( ) => {
56+ jest . spyOn ( Storage . prototype , 'getItem' ) . mockReturnValueOnce ( null ) ;
57+ const result = StorageService . getHistory ( ) ;
58+ expect ( result ) . toEqual ( [ ] ) ;
59+ } ) ;
60+
61+ it ( 'return history result' , ( ) => {
62+ const mockHistory = [
63+ { code : 'test' , date : today } ,
64+ { code : 'test2' , date : today } ,
65+ ] ;
66+ jest
67+ . spyOn ( Storage . prototype , 'getItem' )
68+ . mockReturnValueOnce ( JSON . stringify ( mockHistory ) ) ;
69+ const result = StorageService . getHistory ( ) ;
70+ expect ( result . length ) . toEqual ( mockHistory . length ) ;
71+ } ) ;
72+
73+ it ( 'save code to history' , ( ) => {
74+ jest . spyOn ( Storage . prototype , 'getItem' ) . mockReturnValueOnce ( null ) ;
75+
76+ StorageService . saveToHistory ( mockCode ) ;
77+ expect ( setItemSpy ) . toHaveBeenCalledWith (
78+ STORAGE . HISTORY ,
79+ JSON . stringify ( [ { code : compress ( mockCode ) , date : today } ] ) ,
80+ ) ;
81+ } ) ;
82+
83+ it ( 'remove oldest item in history if max reached' , ( ) => {
84+ jest . spyOn ( Storage . prototype , 'getItem' ) . mockReturnValueOnce (
85+ JSON . stringify ( [
86+ { code : compress ( 'console.log(2)' ) , date : today } ,
87+ { code : compress ( 'console.log(3)' ) , date : today } ,
88+ { code : compress ( 'console.log(4)' ) , date : today } ,
89+ ] ) ,
90+ ) ;
91+
92+ StorageService . saveToHistory ( mockCode ) ;
93+ expect ( setItemSpy ) . toHaveBeenCalledWith (
94+ STORAGE . HISTORY ,
95+ JSON . stringify ( [
96+ { code : compress ( 'console.log(3)' ) , date : today } ,
97+ { code : compress ( 'console.log(4)' ) , date : today } ,
98+ { code : compress ( mockCode ) , date : today } ,
99+ ] ) ,
100+ ) ;
101+ } ) ;
102+
103+ it ( 'does not save code to history if it already exist' , ( ) => {
104+ const mockCode = 'console.log(1)' ;
105+ jest
106+ . spyOn ( Storage . prototype , 'getItem' )
107+ . mockReturnValueOnce (
108+ JSON . stringify ( [ { code : compress ( mockCode ) , date : today } ] ) ,
109+ ) ;
110+
111+ StorageService . saveToHistory ( mockCode ) ;
112+ expect ( setItemSpy ) . not . toHaveBeenCalled ( ) ;
113+ } ) ;
114+ } ) ;
29115} ) ;
0 commit comments