|
| 1 | +import { |
| 2 | + ComponentFixture, |
| 3 | + TestBed, |
| 4 | +} from '@angular/core/testing'; |
| 5 | +import { By } from '@angular/platform-browser'; |
| 6 | + |
| 7 | +import { Item } from '../../../../core/shared/item.model'; |
| 8 | +import { ClarinDescriptionItemFieldComponent } from './clarin-description-item-field.component'; |
| 9 | + |
| 10 | +describe('ClarinDescriptionItemFieldComponent', () => { |
| 11 | + |
| 12 | + let component: ClarinDescriptionItemFieldComponent; |
| 13 | + let fixture: ComponentFixture<ClarinDescriptionItemFieldComponent>; |
| 14 | + |
| 15 | + function itemWithDescriptions(values: { value: string, language: string }[]): Item { |
| 16 | + return Object.assign(new Item(), { |
| 17 | + metadata: { |
| 18 | + 'dc.description': values, |
| 19 | + }, |
| 20 | + }); |
| 21 | + } |
| 22 | + |
| 23 | + function render(item: Item): void { |
| 24 | + fixture = TestBed.createComponent(ClarinDescriptionItemFieldComponent); |
| 25 | + component = fixture.componentInstance; |
| 26 | + component.fields = ['dc.description']; |
| 27 | + component.item = item; |
| 28 | + fixture.detectChanges(); |
| 29 | + } |
| 30 | + |
| 31 | + beforeEach(async () => { |
| 32 | + await TestBed.configureTestingModule({ |
| 33 | + imports: [ClarinDescriptionItemFieldComponent], |
| 34 | + }).compileComponents(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should render one element per description value', () => { |
| 38 | + render(itemWithDescriptions([ |
| 39 | + { value: 'first description', language: 'en_US' }, |
| 40 | + { value: 'second description', language: 'cs_CZ' }, |
| 41 | + ])); |
| 42 | + |
| 43 | + const divs = fixture.debugElement.queryAll(By.css('div')); |
| 44 | + expect(divs.length).toBe(2); |
| 45 | + expect(divs[0].nativeElement.textContent).toContain('first description'); |
| 46 | + expect(divs[1].nativeElement.textContent).toContain('second description'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should give each value its own normalized BCP 47 lang attribute', () => { |
| 50 | + render(itemWithDescriptions([ |
| 51 | + { value: 'anglicky', language: 'en_US' }, |
| 52 | + { value: 'cesky', language: 'cs_CZ' }, |
| 53 | + ])); |
| 54 | + |
| 55 | + const divs = fixture.debugElement.queryAll(By.css('div')); |
| 56 | + expect(divs[0].nativeElement.getAttribute('lang')).toEqual('en-US'); |
| 57 | + expect(divs[1].nativeElement.getAttribute('lang')).toEqual('cs-CZ'); |
| 58 | + expect(fixture.nativeElement.innerHTML).not.toContain('lang="en_US"'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should omit the lang attribute for the wildcard language', () => { |
| 62 | + render(itemWithDescriptions([{ value: 'language independent', language: '*' }])); |
| 63 | + |
| 64 | + const div = fixture.debugElement.query(By.css('div')); |
| 65 | + expect(div.nativeElement.hasAttribute('lang')).toBeFalse(); |
| 66 | + expect(fixture.nativeElement.innerHTML).not.toContain('lang="*"'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should still turn URLs in the description into links', () => { |
| 70 | + render(itemWithDescriptions([ |
| 71 | + { value: 'see https://lindat.cz for more', language: 'en_US' }, |
| 72 | + ])); |
| 73 | + |
| 74 | + const anchor = fixture.debugElement.query(By.css('div a')); |
| 75 | + expect(anchor).toBeTruthy(); |
| 76 | + expect(anchor.nativeElement.getAttribute('href')).toEqual('https://lindat.cz'); |
| 77 | + expect(anchor.nativeElement.getAttribute('target')).toEqual('_blank'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should render nothing when the item has no description', () => { |
| 81 | + render(Object.assign(new Item(), { metadata: {} })); |
| 82 | + |
| 83 | + expect(component.descriptionEntries).toEqual([]); |
| 84 | + expect(fixture.debugElement.queryAll(By.css('div')).length).toBe(0); |
| 85 | + }); |
| 86 | + |
| 87 | +}); |
0 commit comments