import { ItemGridElementComponent } from './item-grid-element.component'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { ChangeDetectionStrategy, NO_ERRORS_SCHEMA } from '@angular/core'; import { By } from '@angular/platform-browser'; import { TruncatePipe } from '../../utils/truncate.pipe'; import { Item } from '../../../core/shared/item.model'; import { Observable } from 'rxjs/Observable'; let itemGridElementComponent: ItemGridElementComponent; let fixture: ComponentFixture; const mockItemWithAuthorAndDate: Item = Object.assign(new Item(), { bitstreams: Observable.of({}), metadata: [ { key: 'dc.contributor.author', language: 'en_US', value: 'Smith, Donald' }, { key: 'dc.date.issued', language: null, value: '2015-06-26' }] }); const mockItemWithoutAuthorAndDate: Item = Object.assign(new Item(), { bitstreams: Observable.of({}), metadata: [ { key: 'dc.title', language: 'en_US', value: 'This is just another title' }, { key: 'dc.type', language: null, value: 'Article' }] }); describe('ItemGridElementComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ ItemGridElementComponent , TruncatePipe], providers: [ { provide: 'objectElementProvider', useValue: {mockItemWithAuthorAndDate}} ], schemas: [ NO_ERRORS_SCHEMA ] }).overrideComponent(ItemGridElementComponent, { set: { changeDetection: ChangeDetectionStrategy.Default } }).compileComponents(); })); beforeEach(async(() => { fixture = TestBed.createComponent(ItemGridElementComponent); itemGridElementComponent = fixture.componentInstance; })); describe('When the item has an author', () => { beforeEach(() => { itemGridElementComponent.object = mockItemWithAuthorAndDate; fixture.detectChanges(); }); it('should show the author paragraph', () => { const itemAuthorField = fixture.debugElement.query(By.css('p.item-authors')); expect(itemAuthorField).not.toBeNull(); }); }); describe('When the item has no author', () => { beforeEach(() => { itemGridElementComponent.object = mockItemWithoutAuthorAndDate; fixture.detectChanges(); }); it('should not show the author paragraph', () => { const itemAuthorField = fixture.debugElement.query(By.css('p.item-authors')); expect(itemAuthorField).toBeNull(); }); }); describe('When the item has an issuedate', () => { beforeEach(() => { itemGridElementComponent.object = mockItemWithAuthorAndDate; fixture.detectChanges(); }); it('should show the issuedate span', () => { const itemAuthorField = fixture.debugElement.query(By.css('span.item-date')); expect(itemAuthorField).not.toBeNull(); }); }); describe('When the item has no issuedate', () => { beforeEach(() => { itemGridElementComponent.object = mockItemWithoutAuthorAndDate; fixture.detectChanges(); }); it('should not show the issuedate span', () => { const dateField = fixture.debugElement.query(By.css('span.item-date')); expect(dateField).toBeNull(); }); }); });