@@ -5,17 +5,19 @@ import { SocketService } from '@global-service/socket/socket.service';
55import { LocalStorageService } from '@global-service/localstorage/local-storage.service' ;
66import { BehaviorSubject , Observable } from 'rxjs' ;
77import { TranslateModule } from '@ngx-translate/core' ;
8- import { By } from '@angular/platform-browser' ;
8+ import { By , DomSanitizer } from '@angular/platform-browser' ;
99import { PlaceholderForDivDirective } from 'src/app/main/component/comments/directives/placeholder-for-div.directive' ;
1010import { MatSelectModule } from '@angular/material/select' ;
1111import { UserProfileImageComponent } from '@global-user/components/shared/components/user-profile-image/user-profile-image.component' ;
1212import { MatMenuModule } from '@angular/material/menu' ;
1313import { MatIconModule } from '@angular/material/icon' ;
1414import { BrowserAnimationsModule } from '@angular/platform-browser/animations' ;
15+ import { ElementRef } from '@angular/core' ;
1516
1617describe ( 'CommentTextareaComponent' , ( ) => {
1718 let component : CommentTextareaComponent ;
1819 let fixture : ComponentFixture < CommentTextareaComponent > ;
20+ let mockSanitizer : jasmine . SpyObj < DomSanitizer > ;
1921
2022 const socketServiceMock : SocketService = jasmine . createSpyObj ( 'SocketService' , [ 'onMessage' , 'send' , 'initiateConnection' ] ) ;
2123 socketServiceMock . onMessage = ( ) => new Observable ( ) ;
@@ -45,12 +47,14 @@ describe('CommentTextareaComponent', () => {
4547 ] ;
4648
4749 beforeEach ( waitForAsync ( ( ) => {
50+ mockSanitizer = jasmine . createSpyObj ( 'DomSanitizer' , [ 'sanitize' ] ) ;
4851 TestBed . configureTestingModule ( {
4952 declarations : [ CommentTextareaComponent , PlaceholderForDivDirective , UserProfileImageComponent ] ,
5053 providers : [
5154 { provide : Router , useValue : { } } ,
5255 { provide : SocketService , useValue : socketServiceMock } ,
53- { provide : LocalStorageService , useValue : localStorageServiceMock }
56+ { provide : LocalStorageService , useValue : localStorageServiceMock } ,
57+ { provide : DomSanitizer , useValue : mockSanitizer }
5458 ] ,
5559 imports : [ MatSelectModule , TranslateModule . forRoot ( ) , BrowserAnimationsModule , MatMenuModule , MatIconModule ]
5660 } ) . compileComponents ( ) ;
@@ -64,6 +68,7 @@ describe('CommentTextareaComponent', () => {
6468 beforeEach ( ( ) => {
6569 fixture = TestBed . createComponent ( CommentTextareaComponent ) ;
6670 component = fixture . componentInstance ;
71+ component . commentTextarea = new ElementRef ( document . createElement ( 'div' ) ) ;
6772 fixture . detectChanges ( ) ;
6873 } ) ;
6974
@@ -253,4 +258,82 @@ describe('CommentTextareaComponent', () => {
253258 imageFiles : null
254259 } ) ;
255260 } ) ;
261+
262+ describe ( 'handleInputChange' , ( ) => {
263+ it ( 'should update the content value if the text content changes' , ( ) => {
264+ component . commentTextarea . nativeElement . textContent = 'New content' ;
265+ component . content . setValue ( 'Old content' ) ;
266+
267+ component [ 'handleInputChange' ] ( ) ;
268+
269+ expect ( component . content . value ) . toBe ( 'New content' ) ;
270+ } ) ;
271+
272+ it ( 'should call updateLinksInTextarea with the new text content' , ( ) => {
273+ const updateLinksSpy = spyOn < any > ( component , 'updateLinksInTextarea' ) ;
274+ component . commentTextarea . nativeElement . textContent = 'New content' ;
275+
276+ component [ 'handleInputChange' ] ( ) ;
277+
278+ expect ( updateLinksSpy ) . toHaveBeenCalledWith ( 'New content' ) ;
279+ } ) ;
280+ } ) ;
281+
282+ describe ( 'onCommentTextareaFocus' , ( ) => {
283+ it ( 'should call updateLinksInTextarea with trimmed current text' , ( ) => {
284+ const updateLinksSpy = spyOn < any > ( component , 'updateLinksInTextarea' ) ;
285+ component . commentTextarea . nativeElement . textContent = ' Current text ' ;
286+ component . onCommentTextareaFocus ( ) ;
287+
288+ expect ( updateLinksSpy ) . toHaveBeenCalledWith ( 'Current text' ) ;
289+ } ) ;
290+ } ) ;
291+
292+ describe ( 'onCommentTextareaBlur' , ( ) => {
293+ it ( 'should set the stripped text to the content value' , ( ) => {
294+ component . commentTextarea . nativeElement . textContent = 'Blur text' ;
295+ component . onCommentTextareaBlur ( ) ;
296+
297+ expect ( component . content . value ) . toBe ( 'Blur text' ) ;
298+ } ) ;
299+
300+ it ( 'should call emitComment' , ( ) => {
301+ const emitCommentSpy = spyOn < any > ( component , 'emitComment' ) ;
302+ component . onCommentTextareaBlur ( ) ;
303+
304+ expect ( emitCommentSpy ) . toHaveBeenCalled ( ) ;
305+ } ) ;
306+ } ) ;
307+
308+ describe ( 'updateLinksInTextarea' , ( ) => {
309+ it ( 'should sanitize and set innerHTML if a URL pattern is found' , ( ) => {
310+ const renderLinksSpy = spyOn ( component , 'renderLinks' ) . and . returnValue ( '<a href="http://example.com">http://example.com</a>' ) ;
311+ mockSanitizer . sanitize . and . returnValue ( '<a href="http://example.com">http://example.com</a>' ) ;
312+ const initializeLinksSpy = spyOn < any > ( component , 'initializeLinkClickListeners' ) ;
313+ const testText = 'Check this link http://example.com' ;
314+
315+ component [ 'updateLinksInTextarea' ] ( testText ) ;
316+
317+ expect ( renderLinksSpy ) . toHaveBeenCalledWith ( testText ) ;
318+ expect ( component . commentTextarea . nativeElement . innerHTML ) . toBe ( '<a href="http://example.com">http://example.com</a>' ) ;
319+ expect ( initializeLinksSpy ) . toHaveBeenCalledWith ( component . commentTextarea . nativeElement ) ;
320+ } ) ;
321+
322+ describe ( 'initializeLinkClickListeners' , ( ) => {
323+ it ( 'should open links in a new tab on click' , ( ) => {
324+ const element = document . createElement ( 'div' ) ;
325+ const link = document . createElement ( 'a' ) ;
326+ link . href = 'http://example.com' ;
327+ link . textContent = 'example' ;
328+ element . appendChild ( link ) ;
329+
330+ const openSpy = spyOn ( window , 'open' ) ;
331+ component [ 'initializeLinkClickListeners' ] ( element ) ;
332+
333+ link . click ( ) ;
334+
335+ expect ( openSpy ) . toHaveBeenCalledWith ( 'http://example.com' , '_blank' , 'noopener,noreferrer' ) ;
336+ } ) ;
337+ } ) ;
338+ } ) ;
256339} ) ;
0 commit comments