1+ import { beforeEach , describe , expect , it , vi } from 'vitest'
2+ import { EnhancerRegistry , TextareaRegistry } from '../datamodel/registries'
3+
4+ // Mock WXT's defineContentScript global
5+ vi . stubGlobal ( 'defineContentScript' , vi . fn ( ) )
6+
7+ describe ( 'GitHub PR Content Script' , ( ) => {
8+ let enhancers : EnhancerRegistry
9+ let enhancedTextareas : TextareaRegistry
10+ let mockTextarea : HTMLTextAreaElement
11+
12+ beforeEach ( ( ) => {
13+ // Reset DOM and registries for each test
14+ document . body . innerHTML = ''
15+ enhancers = new EnhancerRegistry ( )
16+ enhancedTextareas = new TextareaRegistry ( )
17+
18+ // Mock window.location for GitHub PR page
19+ Object . defineProperty ( window , 'location' , {
20+ writable : true ,
21+ value : {
22+ hostname : 'github.com' ,
23+ pathname : '/diffplug/selfie/pull/517' ,
24+ href : 'https://github.com/diffplug/selfie/pull/517'
25+ }
26+ } )
27+
28+ // Create a mock textarea element that mimics GitHub's PR comment box
29+ mockTextarea = document . createElement ( 'textarea' )
30+ mockTextarea . name = 'comment[body]'
31+ mockTextarea . placeholder = 'Leave a comment'
32+ mockTextarea . className = 'form-control markdown-body'
33+
34+ // Add it to a typical GitHub comment form structure
35+ const commentForm = document . createElement ( 'div' )
36+ commentForm . className = 'js-new-comment-form'
37+ commentForm . appendChild ( mockTextarea )
38+ document . body . appendChild ( commentForm )
39+ } )
40+
41+ it ( 'should identify GitHub PR textarea and register it in TextareaRegistry' , ( ) => {
42+ // Simulate the content script's enhanceMaybe function
43+ const enhancedTextarea = enhancers . tryToEnhance ( mockTextarea )
44+
45+ expect ( enhancedTextarea ) . toBeTruthy ( )
46+ expect ( enhancedTextarea ?. element ) . toBe ( mockTextarea )
47+ expect ( enhancedTextarea ?. spot . type ) . toBe ( 'GH_PR_ADD_COMMENT' )
48+
49+ // Register the enhanced textarea
50+ if ( enhancedTextarea ) {
51+ enhancedTextareas . register ( enhancedTextarea )
52+ }
53+
54+ // Verify it's in the registry
55+ const registeredTextarea = enhancedTextareas . get ( mockTextarea )
56+ expect ( registeredTextarea ) . toBeTruthy ( )
57+ expect ( registeredTextarea ?. element ) . toBe ( mockTextarea )
58+ } )
59+
60+ it ( 'should create correct GitHubContext spot for PR comment' , ( ) => {
61+ const enhancedTextarea = enhancers . tryToEnhance ( mockTextarea )
62+
63+ expect ( enhancedTextarea ) . toBeTruthy ( )
64+
65+ // Snapshot test on the spot value
66+ expect ( enhancedTextarea ?. spot ) . toMatchSnapshot ( 'github-pr-517-spot' )
67+
68+ // Also verify specific expected values
69+ expect ( enhancedTextarea ?. spot ) . toMatchObject ( {
70+ type : 'GH_PR_ADD_COMMENT' ,
71+ domain : 'github.com' ,
72+ slug : 'diffplug/selfie' ,
73+ number : 517 ,
74+ unique_key : 'github:diffplug/selfie:pull:517' ,
75+ commentId : undefined
76+ } )
77+ } )
78+
79+ it ( 'should handle multiple textareas on the same page' , ( ) => {
80+ // Create a second textarea for inline code comments
81+ const codeCommentTextarea = document . createElement ( 'textarea' )
82+ codeCommentTextarea . className = 'form-control js-suggester-field'
83+
84+ const inlineForm = document . createElement ( 'div' )
85+ inlineForm . className = 'js-inline-comment-form'
86+ inlineForm . appendChild ( codeCommentTextarea )
87+ document . body . appendChild ( inlineForm )
88+
89+ // Test both textareas
90+ const mainCommentEnhanced = enhancers . tryToEnhance ( mockTextarea )
91+ const codeCommentEnhanced = enhancers . tryToEnhance ( codeCommentTextarea )
92+
93+ expect ( mainCommentEnhanced ?. spot . type ) . toBe ( 'GH_PR_ADD_COMMENT' )
94+ expect ( codeCommentEnhanced ?. spot . type ) . toBe ( 'GH_PR_CODE_COMMENT' )
95+
96+ // Register both
97+ if ( mainCommentEnhanced ) enhancedTextareas . register ( mainCommentEnhanced )
98+ if ( codeCommentEnhanced ) enhancedTextareas . register ( codeCommentEnhanced )
99+
100+ // Verify both are registered
101+ expect ( enhancedTextareas . get ( mockTextarea ) ) . toBeTruthy ( )
102+ expect ( enhancedTextareas . get ( codeCommentTextarea ) ) . toBeTruthy ( )
103+ } )
104+
105+ it ( 'should not enhance textarea on non-GitHub pages' , ( ) => {
106+ // Change location to non-GitHub site
107+ Object . defineProperty ( window , 'location' , {
108+ writable : true ,
109+ value : {
110+ hostname : 'example.com' ,
111+ pathname : '/some/page' ,
112+ href : 'https://example.com/some/page'
113+ }
114+ } )
115+
116+ const enhancedTextarea = enhancers . tryToEnhance ( mockTextarea )
117+ expect ( enhancedTextarea ) . toBeNull ( )
118+ } )
119+ } )
0 commit comments