11import { describe , test , expect , beforeEach , afterEach , vi } from "vitest" ;
2+ import { fakeBrowser } from "wxt/testing/fake-browser" ;
23import { getHistory , addToHistory } from "./history" ;
34
4- // Mock storage for testing
5- const mockStorage : Record < string , any > = { } ;
6-
75describe ( "history" , ( ) => {
86 beforeEach ( ( ) => {
9- // Clear mock storage before each test
10- Object . keys ( mockStorage ) . forEach ( key => delete mockStorage [ key ] ) ;
11-
12- // Mock browser.storage.local methods
13- vi . spyOn ( browser . storage . local , 'get' ) . mockImplementation ( ( key : string ) => {
14- return Promise . resolve ( { [ key ] : mockStorage [ key ] } ) ;
15- } ) ;
16-
17- vi . spyOn ( browser . storage . local , 'set' ) . mockImplementation ( ( data : Record < string , any > ) => {
18- Object . assign ( mockStorage , data ) ;
19- return Promise . resolve ( ) ;
20- } ) ;
7+ // Reset fake browser state (clears in-memory storage)
8+ fakeBrowser . reset ( ) ;
219
2210 // Mock crypto.randomUUID
2311 let counter = 0 ;
@@ -41,7 +29,7 @@ describe("history", () => {
4129 { id : "1" , text : "test1" , timestamp : 1000 } ,
4230 { id : "2" , text : "test2" , timestamp : 2000 } ,
4331 ] ;
44- mockStorage . clipboardHistory = mockHistory ;
32+ await browser . storage . local . set ( { clipboardHistory : mockHistory } ) ;
4533
4634 const history = await getHistory ( ) ;
4735 expect ( history ) . toEqual ( mockHistory ) ;
@@ -52,7 +40,6 @@ describe("history", () => {
5240 test ( "adds a new item to empty history" , async ( ) => {
5341 await addToHistory ( "test text" ) ;
5442
55- expect ( browser . storage . local . set ) . toHaveBeenCalled ( ) ;
5643 const history = await getHistory ( ) ;
5744 expect ( history ) . toHaveLength ( 1 ) ;
5845 expect ( history [ 0 ] . text ) . toBe ( "test text" ) ;
@@ -61,9 +48,11 @@ describe("history", () => {
6148 } ) ;
6249
6350 test ( "adds new item to the beginning of existing history" , async ( ) => {
64- mockStorage . clipboardHistory = [
65- { id : "1" , text : "old text" , timestamp : 1000 } ,
66- ] ;
51+ await browser . storage . local . set ( {
52+ clipboardHistory : [
53+ { id : "1" , text : "old text" , timestamp : 1000 } ,
54+ ] ,
55+ } ) ;
6756
6857 await addToHistory ( "new text" ) ;
6958
@@ -80,7 +69,7 @@ describe("history", () => {
8069 text : `text-${ i } ` ,
8170 timestamp : i ,
8271 } ) ) ;
83- mockStorage . clipboardHistory = existingItems ;
72+ await browser . storage . local . set ( { clipboardHistory : existingItems } ) ;
8473
8574 await addToHistory ( "new text" ) ;
8675
0 commit comments