Skip to content

Commit 97da132

Browse files
committed
tighten up the background test.
1 parent b083a7b commit 97da132

File tree

1 file changed

+42
-53
lines changed

1 file changed

+42
-53
lines changed

browser-extension/tests/background-events.test.ts renamed to browser-extension/tests/background.test.ts

Lines changed: 42 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,57 @@ import { beforeEach, describe, expect, it } from 'vitest'
22
import { handleCommentEvent, states } from '../src/entrypoints/background'
33
import type { CommentEvent, CommentSpot } from '../src/lib/enhancer'
44

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+
}
515
describe('Background Event Handler', () => {
6-
let mockSender: any
7-
let mockSpot: CommentSpot
8-
916
beforeEach(() => {
10-
// Clear the shared states map before each test
1117
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-
}
2418
})
25-
2619
describe('ENHANCED Event', () => {
2720
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+
`)
4546
})
46-
4747
it('should not handle ENHANCED event without tab info', () => {
48-
const message: CommentEvent = {
49-
spot: mockSpot,
50-
type: 'ENHANCED',
51-
}
52-
5348
const senderWithoutTab = { tab: null }
54-
55-
handleCommentEvent(message, senderWithoutTab)
56-
49+
handleCommentEvent(
50+
{
51+
spot: mockSpot,
52+
type: 'ENHANCED',
53+
},
54+
senderWithoutTab,
55+
)
5756
expect(states.size).toBe(0)
5857
})
5958
})
@@ -65,7 +64,6 @@ describe('Background Event Handler', () => {
6564
spot: mockSpot,
6665
type: 'ENHANCED',
6766
}
68-
6967
handleCommentEvent(enhanceMessage, mockSender)
7068
expect(states.size).toBe(1)
7169

@@ -74,9 +72,7 @@ describe('Background Event Handler', () => {
7472
spot: mockSpot,
7573
type: 'DESTROYED',
7674
}
77-
7875
handleCommentEvent(destroyMessage, mockSender)
79-
8076
expect(states.size).toBe(0)
8177
})
8278

@@ -85,10 +81,8 @@ describe('Background Event Handler', () => {
8581
spot: mockSpot,
8682
type: 'DESTROYED',
8783
}
88-
8984
// Should not throw error
9085
handleCommentEvent(message, mockSender)
91-
9286
expect(states.size).toBe(0)
9387
})
9488
})
@@ -99,9 +93,7 @@ describe('Background Event Handler', () => {
9993
spot: mockSpot,
10094
type: 'LOST_FOCUS',
10195
}
102-
10396
handleCommentEvent(message, mockSender)
104-
10597
expect(states.size).toBe(0)
10698
})
10799
})
@@ -110,13 +102,10 @@ describe('Background Event Handler', () => {
110102
it('should handle multiple enhanced textareas from different tabs', () => {
111103
const spot1: CommentSpot = { type: 'SPOT1', unique_key: 'key1' }
112104
const spot2: CommentSpot = { type: 'SPOT2', unique_key: 'key2' }
113-
114105
const sender1 = { tab: { id: 123, windowId: 456 } }
115106
const sender2 = { tab: { id: 789, windowId: 456 } }
116-
117107
handleCommentEvent({ spot: spot1, type: 'ENHANCED' }, sender1)
118108
handleCommentEvent({ spot: spot2, type: 'ENHANCED' }, sender2)
119-
120109
expect(states.size).toBe(2)
121110
})
122111

0 commit comments

Comments
 (0)