1
+ import { compress } from 'lz-string' ;
2
+ import { STORAGE } from 'helpers/const' ;
1
3
import * as StorageService from 'services/storage' ;
2
4
5
+ jest . mock ( '../helpers/const' , ( ) => {
6
+ const actual = jest . requireActual ( '../helpers/const' ) ;
7
+ return {
8
+ ...actual ,
9
+ MAX_HISTORY_SIZE : 2 ,
10
+ } ;
11
+ } ) ;
12
+
3
13
describe ( 'Storage tests' , ( ) => {
4
14
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
+ } ) ;
5
26
6
27
it ( 'Save value to locale storage' , ( ) => {
7
28
StorageService . setLocalStorage ( 'mock' , 'value' ) ;
@@ -26,4 +47,69 @@ describe('Storage tests', () => {
26
47
StorageService . clearLocalStorage ( 'value' ) ;
27
48
expect ( removeItemSpy ) . toHaveBeenCalledWith ( 'value' ) ;
28
49
} ) ;
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
+ } ) ;
29
115
} ) ;
0 commit comments