@@ -2,58 +2,57 @@ import { beforeEach, describe, expect, it } from 'vitest'
2
2
import { handleCommentEvent , states } from '../src/entrypoints/background'
3
3
import type { CommentEvent , CommentSpot } from '../src/lib/enhancer'
4
4
5
+ const mockSender = {
6
+ tab : {
7
+ id : 123 ,
8
+ windowId : 456 ,
9
+ } ,
10
+ }
11
+ const mockSpot = {
12
+ type : 'TEST_SPOT' ,
13
+ unique_key : 'test-key' ,
14
+ }
5
15
describe ( 'Background Event Handler' , ( ) => {
6
- let mockSender : any
7
- let mockSpot : CommentSpot
8
-
9
16
beforeEach ( ( ) => {
10
- // Clear the shared states map before each test
11
17
states . clear ( )
12
-
13
- mockSender = {
14
- tab : {
15
- id : 123 ,
16
- windowId : 456 ,
17
- } ,
18
- }
19
-
20
- mockSpot = {
21
- type : 'TEST_SPOT' ,
22
- unique_key : 'test-key' ,
23
- }
24
18
} )
25
-
26
19
describe ( 'ENHANCED Event' , ( ) => {
27
20
it ( 'should create new comment state when textarea is enhanced' , ( ) => {
28
- const message : CommentEvent = {
29
- spot : mockSpot ,
30
- type : 'ENHANCED' ,
31
- }
32
-
33
- handleCommentEvent ( message , mockSender )
34
-
35
- const expectedKey = {
36
- spot : mockSpot ,
37
- tab : { tabId : 123 , windowId : 456 } ,
38
- }
39
-
40
- const state = states . get ( expectedKey )
41
- expect ( state ) . toBeDefined ( )
42
- expect ( state ?. tab ) . toEqual ( { tabId : 123 , windowId : 456 } )
43
- expect ( state ?. spot ) . toEqual ( mockSpot )
44
- expect ( state ?. drafts ) . toEqual ( [ ] )
21
+ handleCommentEvent (
22
+ {
23
+ spot : mockSpot ,
24
+ type : 'ENHANCED' ,
25
+ } ,
26
+ mockSender ,
27
+ )
28
+ expect ( Array . from ( states ) ) . toMatchInlineSnapshot ( `
29
+ [
30
+ [
31
+ "{"spot":{"type":"TEST_SPOT","unique_key":"test-key"},"tab":{"tabId":123,"windowId":456}}",
32
+ {
33
+ "drafts": [],
34
+ "spot": {
35
+ "type": "TEST_SPOT",
36
+ "unique_key": "test-key",
37
+ },
38
+ "tab": {
39
+ "tabId": 123,
40
+ "windowId": 456,
41
+ },
42
+ },
43
+ ],
44
+ ]
45
+ ` )
45
46
} )
46
-
47
47
it ( 'should not handle ENHANCED event without tab info' , ( ) => {
48
- const message : CommentEvent = {
49
- spot : mockSpot ,
50
- type : 'ENHANCED' ,
51
- }
52
-
53
48
const senderWithoutTab = { tab : null }
54
-
55
- handleCommentEvent ( message , senderWithoutTab )
56
-
49
+ handleCommentEvent (
50
+ {
51
+ spot : mockSpot ,
52
+ type : 'ENHANCED' ,
53
+ } ,
54
+ senderWithoutTab ,
55
+ )
57
56
expect ( states . size ) . toBe ( 0 )
58
57
} )
59
58
} )
@@ -65,7 +64,6 @@ describe('Background Event Handler', () => {
65
64
spot : mockSpot ,
66
65
type : 'ENHANCED' ,
67
66
}
68
-
69
67
handleCommentEvent ( enhanceMessage , mockSender )
70
68
expect ( states . size ) . toBe ( 1 )
71
69
@@ -74,9 +72,7 @@ describe('Background Event Handler', () => {
74
72
spot : mockSpot ,
75
73
type : 'DESTROYED' ,
76
74
}
77
-
78
75
handleCommentEvent ( destroyMessage , mockSender )
79
-
80
76
expect ( states . size ) . toBe ( 0 )
81
77
} )
82
78
@@ -85,10 +81,8 @@ describe('Background Event Handler', () => {
85
81
spot : mockSpot ,
86
82
type : 'DESTROYED' ,
87
83
}
88
-
89
84
// Should not throw error
90
85
handleCommentEvent ( message , mockSender )
91
-
92
86
expect ( states . size ) . toBe ( 0 )
93
87
} )
94
88
} )
@@ -99,9 +93,7 @@ describe('Background Event Handler', () => {
99
93
spot : mockSpot ,
100
94
type : 'LOST_FOCUS' ,
101
95
}
102
-
103
96
handleCommentEvent ( message , mockSender )
104
-
105
97
expect ( states . size ) . toBe ( 0 )
106
98
} )
107
99
} )
@@ -110,13 +102,10 @@ describe('Background Event Handler', () => {
110
102
it ( 'should handle multiple enhanced textareas from different tabs' , ( ) => {
111
103
const spot1 : CommentSpot = { type : 'SPOT1' , unique_key : 'key1' }
112
104
const spot2 : CommentSpot = { type : 'SPOT2' , unique_key : 'key2' }
113
-
114
105
const sender1 = { tab : { id : 123 , windowId : 456 } }
115
106
const sender2 = { tab : { id : 789 , windowId : 456 } }
116
-
117
107
handleCommentEvent ( { spot : spot1 , type : 'ENHANCED' } , sender1 )
118
108
handleCommentEvent ( { spot : spot2 , type : 'ENHANCED' } , sender2 )
119
-
120
109
expect ( states . size ) . toBe ( 2 )
121
110
} )
122
111
0 commit comments