|
| 1 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 2 | +import {TableCellLinkComponent} from './table-cell-link.component'; |
| 3 | +import {By} from '@angular/platform-browser'; |
| 4 | +import {ComponentRef} from '@angular/core'; |
| 5 | + |
| 6 | +describe('TableCellLinkComponent', () => { |
| 7 | + let component: TableCellLinkComponent; |
| 8 | + let fixture: ComponentFixture<TableCellLinkComponent>; |
| 9 | + let componentRef: ComponentRef<TableCellLinkComponent>; |
| 10 | + let windowOpenSpy: jest.SpyInstance; |
| 11 | + |
| 12 | + const DISABLED_VALUE = '-'; |
| 13 | + const VALID_URL = 'https://example.com'; |
| 14 | + const ALTERNATIVE_URL = 'https://newsite.com'; |
| 15 | + const TOOLTIP_MESSAGE = 'Open link'; |
| 16 | + const NO_LINK_MESSAGE = 'No link'; |
| 17 | + |
| 18 | + beforeEach(async () => { |
| 19 | + await TestBed.configureTestingModule({ |
| 20 | + imports: [TableCellLinkComponent], |
| 21 | + }).compileComponents(); |
| 22 | + |
| 23 | + fixture = TestBed.createComponent(TableCellLinkComponent); |
| 24 | + component = fixture.componentInstance; |
| 25 | + componentRef = fixture.componentRef; |
| 26 | + windowOpenSpy = jest.spyOn(window, 'open').mockImplementation(); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + windowOpenSpy.mockRestore(); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('isDisabled computed signal', () => { |
| 34 | + it('should return true only when value is "-"', () => { |
| 35 | + componentRef.setInput('value', DISABLED_VALUE); |
| 36 | + componentRef.setInput('tooltipMessage', NO_LINK_MESSAGE); |
| 37 | + fixture.detectChanges(); |
| 38 | + |
| 39 | + expect(component.isDisabled()).toBe(true); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should return false for any other value', () => { |
| 43 | + const testValues = [VALID_URL, '', 'some-text']; |
| 44 | + |
| 45 | + testValues.forEach(value => { |
| 46 | + componentRef.setInput('value', value); |
| 47 | + componentRef.setInput('tooltipMessage', TOOLTIP_MESSAGE); |
| 48 | + fixture.detectChanges(); |
| 49 | + |
| 50 | + expect(component.isDisabled()).toBe(false); |
| 51 | + }); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('openExternalLink', () => { |
| 56 | + it('should open link in new tab when enabled', () => { |
| 57 | + componentRef.setInput('value', VALID_URL); |
| 58 | + componentRef.setInput('tooltipMessage', TOOLTIP_MESSAGE); |
| 59 | + fixture.detectChanges(); |
| 60 | + |
| 61 | + component.openExternalLink(); |
| 62 | + |
| 63 | + expect(windowOpenSpy).toHaveBeenCalledWith(VALID_URL, '_blank'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should not open link when disabled', () => { |
| 67 | + componentRef.setInput('value', DISABLED_VALUE); |
| 68 | + componentRef.setInput('tooltipMessage', NO_LINK_MESSAGE); |
| 69 | + fixture.detectChanges(); |
| 70 | + |
| 71 | + component.openExternalLink(); |
| 72 | + |
| 73 | + expect(windowOpenSpy).not.toHaveBeenCalled(); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('UI rendering', () => { |
| 78 | + beforeEach(() => { |
| 79 | + componentRef.setInput('value', VALID_URL); |
| 80 | + componentRef.setInput('tooltipMessage', TOOLTIP_MESSAGE); |
| 81 | + fixture.detectChanges(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should render button with icon', () => { |
| 85 | + const button = fixture.debugElement.query(By.css('button[mat-icon-button]')); |
| 86 | + const icon = fixture.debugElement.query(By.css('mat-icon')); |
| 87 | + |
| 88 | + expect(button).toBeTruthy(); |
| 89 | + expect(button.nativeElement.getAttribute('aria-label')).toBe('link row'); |
| 90 | + expect(icon.nativeElement.textContent.trim()).toBe('open_in_new'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should apply disabled class based on isDisabled state', () => { |
| 94 | + const button = fixture.debugElement.query(By.css('button')); |
| 95 | + expect(button.nativeElement.classList.contains('button-disabled')).toBe(false); |
| 96 | + |
| 97 | + componentRef.setInput('value', DISABLED_VALUE); |
| 98 | + fixture.detectChanges(); |
| 99 | + |
| 100 | + expect(button.nativeElement.classList.contains('button-disabled')).toBe(true); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('UI interactions', () => { |
| 105 | + it('should open window when button is clicked and enabled', () => { |
| 106 | + componentRef.setInput('value', VALID_URL); |
| 107 | + componentRef.setInput('tooltipMessage', TOOLTIP_MESSAGE); |
| 108 | + fixture.detectChanges(); |
| 109 | + |
| 110 | + const button = fixture.debugElement.query(By.css('button')); |
| 111 | + button.nativeElement.click(); |
| 112 | + |
| 113 | + expect(windowOpenSpy).toHaveBeenCalledWith(VALID_URL, '_blank'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should not open window when button is clicked and disabled', () => { |
| 117 | + componentRef.setInput('value', DISABLED_VALUE); |
| 118 | + componentRef.setInput('tooltipMessage', NO_LINK_MESSAGE); |
| 119 | + fixture.detectChanges(); |
| 120 | + |
| 121 | + const button = fixture.debugElement.query(By.css('button')); |
| 122 | + button.nativeElement.click(); |
| 123 | + |
| 124 | + expect(windowOpenSpy).not.toHaveBeenCalled(); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + describe('Dynamic input changes', () => { |
| 129 | + it('should update disabled state when value changes', () => { |
| 130 | + componentRef.setInput('value', VALID_URL); |
| 131 | + componentRef.setInput('tooltipMessage', TOOLTIP_MESSAGE); |
| 132 | + fixture.detectChanges(); |
| 133 | + |
| 134 | + expect(component.isDisabled()).toBe(false); |
| 135 | + |
| 136 | + componentRef.setInput('value', DISABLED_VALUE); |
| 137 | + fixture.detectChanges(); |
| 138 | + |
| 139 | + expect(component.isDisabled()).toBe(true); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should open new URL when value changes', () => { |
| 143 | + componentRef.setInput('value', VALID_URL); |
| 144 | + componentRef.setInput('tooltipMessage', TOOLTIP_MESSAGE); |
| 145 | + fixture.detectChanges(); |
| 146 | + |
| 147 | + const button = fixture.debugElement.query(By.css('button')); |
| 148 | + button.nativeElement.click(); |
| 149 | + |
| 150 | + expect(windowOpenSpy).toHaveBeenCalledWith(VALID_URL, '_blank'); |
| 151 | + |
| 152 | + componentRef.setInput('value', ALTERNATIVE_URL); |
| 153 | + fixture.detectChanges(); |
| 154 | + |
| 155 | + button.nativeElement.click(); |
| 156 | + |
| 157 | + expect(windowOpenSpy).toHaveBeenCalledWith(ALTERNATIVE_URL, '_blank'); |
| 158 | + }); |
| 159 | + }); |
| 160 | +}); |
0 commit comments