11import { test , expect } from '@playwright/test' ;
22import { getStorageWithExpiry } from '@/utils/get-storage-with-expiry' ;
3+ import { formatDateString } from '@/utils/set-storage-with-expiry' ;
4+
5+ /**
6+ * Calculates an expiry date by adding days to today's date.
7+ *
8+ * @function calculateExpiryDate
9+ * @param {number } days - Number of days to add to today's date
10+ *
11+ * @returns {string } Expiry date string in YYYY-MM-DD format
12+ */
13+ function calculateExpiryDate ( days : number ) : string {
14+ const today = new Date ( ) ;
15+ const expiryDate = new Date ( today ) ;
16+ expiryDate . setDate ( today . getDate ( ) + days ) ;
17+
18+ return formatDateString ( expiryDate ) ;
19+ }
320
421// localStorage mock
522function createLocalStorageMock ( ) {
@@ -64,7 +81,7 @@ test.describe('getStorageWithExpiry', () => {
6481 test ( 'should return stored boolean value when item has not expired' , ( ) => {
6582 const item = {
6683 value : true ,
67- expiry : Date . now ( ) + 86400000 , // 24 hours from now
84+ expiry : calculateExpiryDate ( 1 ) , // 1 day from now
6885 } ;
6986
7087 localStorage . setItem ( 'test-key' , JSON . stringify ( item ) ) ;
@@ -77,7 +94,7 @@ test.describe('getStorageWithExpiry', () => {
7794 test ( 'should return false when stored value is false' , ( ) => {
7895 const item = {
7996 value : false ,
80- expiry : Date . now ( ) + 86400000 ,
97+ expiry : calculateExpiryDate ( 1 ) ,
8198 } ;
8299
83100 localStorage . setItem ( 'test-key' , JSON . stringify ( item ) ) ;
@@ -88,9 +105,13 @@ test.describe('getStorageWithExpiry', () => {
88105 } ) ;
89106
90107 test ( 'should return null when expired' , ( ) => {
108+ const today = new Date ( ) ;
109+ const yesterday = new Date ( today ) ;
110+ yesterday . setDate ( today . getDate ( ) - 1 ) ;
111+
91112 const item = {
92113 value : true ,
93- expiry : Date . now ( ) - 1000 , // Expired 1 second ago
114+ expiry : formatDateString ( yesterday ) , // Expired yesterday
94115 } ;
95116
96117 localStorage . setItem ( 'expired-key' , JSON . stringify ( item ) ) ;
@@ -123,7 +144,7 @@ test.describe('getStorageWithExpiry', () => {
123144
124145 test ( 'should return null when structure is invalid - missing value' , ( ) => {
125146 const item = {
126- expiry : Date . now ( ) + 86400000 ,
147+ expiry : calculateExpiryDate ( 1 ) ,
127148 // value is missing
128149 } ;
129150
@@ -134,10 +155,10 @@ test.describe('getStorageWithExpiry', () => {
134155 expect ( result ) . toBeNull ( ) ;
135156 } ) ;
136157
137- test ( 'should return null when expiry is not a number ' , ( ) => {
158+ test ( 'should return null when expiry is not a string ' , ( ) => {
138159 const item = {
139160 value : true ,
140- expiry : 'not-a-number' ,
161+ expiry : 1234567890 , // Number instead of date string
141162 } ;
142163
143164 localStorage . setItem ( 'invalid-expiry-type-key' , JSON . stringify ( item ) ) ;
@@ -150,7 +171,7 @@ test.describe('getStorageWithExpiry', () => {
150171 test ( 'should return null when value is not a boolean' , ( ) => {
151172 const item = {
152173 value : 'not-a-boolean' ,
153- expiry : Date . now ( ) + 86400000 ,
174+ expiry : calculateExpiryDate ( 1 ) ,
154175 } ;
155176
156177 localStorage . setItem ( 'invalid-value-type-key' , JSON . stringify ( item ) ) ;
@@ -175,4 +196,37 @@ test.describe('getStorageWithExpiry', () => {
175196
176197 expect ( result ) . toBeNull ( ) ;
177198 } ) ;
199+
200+ test ( 'should return value when expiry date is today' , ( ) => {
201+ const today = formatDateString ( new Date ( ) ) ;
202+
203+ const item = {
204+ value : true ,
205+ expiry : today ,
206+ } ;
207+
208+ localStorage . setItem ( 'today-expiry-key' , JSON . stringify ( item ) ) ;
209+
210+ const result = getStorageWithExpiry ( 'today-expiry-key' ) ;
211+
212+ // Should not be expired since expiry is at start of day (midnight)
213+ expect ( result ) . toBe ( true ) ;
214+ } ) ;
215+
216+ test ( 'should return null when expiry date is before today' , ( ) => {
217+ const today = new Date ( ) ;
218+ const twoDaysAgo = new Date ( today ) ;
219+ twoDaysAgo . setDate ( today . getDate ( ) - 2 ) ;
220+
221+ const item = {
222+ value : true ,
223+ expiry : formatDateString ( twoDaysAgo ) ,
224+ } ;
225+
226+ localStorage . setItem ( 'past-expiry-key' , JSON . stringify ( item ) ) ;
227+
228+ const result = getStorageWithExpiry ( 'past-expiry-key' ) ;
229+
230+ expect ( result ) . toBeNull ( ) ;
231+ } ) ;
178232} ) ;
0 commit comments