From 1a2c74d6f197e4df87a4f873c22c0577284340d1 Mon Sep 17 00:00:00 2001
From: Jonas Van Goolen <jonas@atmire.com>
Date: Mon, 6 Nov 2017 13:40:00 +0100
Subject: [PATCH] #150 Grid element testing

---
 .../collection-grid-element.component.spec.ts | 26 +++++-
 .../community-grid-element.component.spec.ts  | 24 +++++-
 .../item-grid-element.component.spec.ts       | 30 ++++++-
 ...arch-result-grid-element.component.spec.ts | 67 +++++++++++++++
 ...arch-result-grid-element.component.spec.ts | 67 +++++++++++++++
 ...arch-result-grid-element.component.spec.ts | 84 +++++++++++++++++++
 6 files changed, 290 insertions(+), 8 deletions(-)
 create mode 100644 src/app/shared/object-grid/search-result-grid-element/collection-search-result/collection-search-result-grid-element.component.spec.ts
 create mode 100644 src/app/shared/object-grid/search-result-grid-element/community-search-result/community-search-result-grid-element.component.spec.ts
 create mode 100644 src/app/shared/object-grid/search-result-grid-element/item-search-result/item-search-result-grid-element.component.spec.ts

diff --git a/src/app/shared/object-grid/collection-grid-element/collection-grid-element.component.spec.ts b/src/app/shared/object-grid/collection-grid-element/collection-grid-element.component.spec.ts
index a1af8b5a3c..fd935af992 100644
--- a/src/app/shared/object-grid/collection-grid-element/collection-grid-element.component.spec.ts
+++ b/src/app/shared/object-grid/collection-grid-element/collection-grid-element.component.spec.ts
@@ -5,6 +5,7 @@ import { ActivatedRoute, Router } from '@angular/router';
 import { RouterStub } from '../../testing/router-stub';
 import { NO_ERRORS_SCHEMA } from '@angular/core';
 import { By } from '@angular/platform-browser';
+import { Collection } from '../../../core/shared/collection.model';
 
 let collectionGridElementComponent: CollectionGridElementComponent;
 let fixture: ComponentFixture<CollectionGridElementComponent>;
@@ -16,6 +17,15 @@ const activatedRouteStub = {
     scope: scopeParam
   })
 };
+let mockCollection: Collection = Object.assign(new Collection(), {
+  metadata: [
+    {
+      key: 'dc.description.abstract',
+      language: 'en_US',
+      value: 'Short description'
+    }]
+});
+let createdGridElementComponent:CollectionGridElementComponent= new CollectionGridElementComponent(mockCollection);
 
 describe('CollectionGridElementComponent', () => {
   beforeEach(async(() => {
@@ -24,7 +34,7 @@ describe('CollectionGridElementComponent', () => {
       providers: [
         { provide: ActivatedRoute, useValue: activatedRouteStub },
         { provide: Router, useClass: RouterStub },
-        { provide: 'objectElementProvider', useFactory: (collectionGridElementComponent)}
+        { provide: 'objectElementProvider', useValue: (createdGridElementComponent)}
       ],
 
       schemas: [ NO_ERRORS_SCHEMA ]
@@ -33,11 +43,19 @@ describe('CollectionGridElementComponent', () => {
 
   beforeEach(async(() => {
     fixture = TestBed.createComponent(CollectionGridElementComponent);
-    collectionGridElementComponent = fixture.componentInstance;
-
   }));
 
   it('should show the collection cards in the grid element',()=>{
     expect(fixture.debugElement.query(By.css('ds-collection-grid-element'))).toBeDefined();
-  })
+  });
+
+  it('should only show the description if "short description" metadata is present',()=>{
+    let descriptionText = expect(fixture.debugElement.query(By.css('p.card-text')));
+
+    if(mockCollection.shortDescription.length>0){
+      expect(descriptionText).toBeDefined();
+    }else{
+      expect(descriptionText).not.toBeDefined();
+    }
+  });
 })
diff --git a/src/app/shared/object-grid/community-grid-element/community-grid-element.component.spec.ts b/src/app/shared/object-grid/community-grid-element/community-grid-element.component.spec.ts
index f6ab7bc6cd..dc837e3c0c 100644
--- a/src/app/shared/object-grid/community-grid-element/community-grid-element.component.spec.ts
+++ b/src/app/shared/object-grid/community-grid-element/community-grid-element.component.spec.ts
@@ -18,6 +18,18 @@ const activatedRouteStub = {
     scope: scopeParam
   })
 };
+
+let mockCommunity: Community = Object.assign(new Community(), {
+  metadata: [
+    {
+      key: 'dc.description.abstract',
+      language: 'en_US',
+      value: 'Short description'
+    }]
+});
+
+let createdGridElementComponent:CommunityGridElementComponent= new CommunityGridElementComponent(mockCommunity);
+
 describe('CommunityGridElementComponent', () => {
   beforeEach(async(() => {
     TestBed.configureTestingModule({
@@ -25,7 +37,7 @@ describe('CommunityGridElementComponent', () => {
       providers: [
         { provide: ActivatedRoute, useValue: activatedRouteStub },
         { provide: Router, useClass: RouterStub },
-        { provide: 'objectElementProvider', useFactory: (communityGridElementComponent)}
+        { provide: 'objectElementProvider', useValue: (createdGridElementComponent)}
       ],
 
     schemas: [ NO_ERRORS_SCHEMA ]
@@ -41,4 +53,14 @@ describe('CommunityGridElementComponent', () => {
   it('should show the community cards in the grid element',()=>{
     expect(fixture.debugElement.query(By.css('ds-community-grid-element'))).toBeDefined();
   })
+
+  it('should only show the description if "short description" metadata is present',()=>{
+    let descriptionText = expect(fixture.debugElement.query(By.css('p.card-text')));
+
+    if(mockCommunity.shortDescription.length>0){
+      expect(descriptionText).toBeDefined();
+    }else{
+      expect(descriptionText).not.toBeDefined();
+    }
+  });
 });
diff --git a/src/app/shared/object-grid/item-grid-element/item-grid-element.component.spec.ts b/src/app/shared/object-grid/item-grid-element/item-grid-element.component.spec.ts
index b4fb9c8029..8913dca3ee 100644
--- a/src/app/shared/object-grid/item-grid-element/item-grid-element.component.spec.ts
+++ b/src/app/shared/object-grid/item-grid-element/item-grid-element.component.spec.ts
@@ -6,6 +6,7 @@ import { RouterStub } from '../../testing/router-stub';
 import { 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';
 
 let itemGridElementComponent: ItemGridElementComponent;
 let fixture: ComponentFixture<ItemGridElementComponent>;
@@ -17,6 +18,17 @@ const activatedRouteStub = {
     scope: scopeParam
   })
 };
+/* tslint:disable:no-shadowed-variable */
+let mockItem: Item = Object.assign(new Item(), {
+  metadata: [
+    {
+      key: 'dc.contributor.author',
+      language: 'en_US',
+      value: 'Smith, Donald'
+    }]
+});
+
+let createdGridElementComponent:ItemGridElementComponent= new ItemGridElementComponent(mockItem);
 
 describe('ItemGridElementComponent', () => {
   beforeEach(async(() => {
@@ -25,7 +37,7 @@ describe('ItemGridElementComponent', () => {
       providers: [
         { provide: ActivatedRoute, useValue: activatedRouteStub },
         { provide: Router, useClass: RouterStub },
-        { provide: 'objectElementProvider', useFactory: (itemGridElementComponent)}
+        { provide: 'objectElementProvider', useValue: {createdGridElementComponent}}
       ],
 
       schemas: [ NO_ERRORS_SCHEMA ]
@@ -39,6 +51,18 @@ describe('ItemGridElementComponent', () => {
   }));
 
   it('should show the item cards in the grid element',()=>{
-    expect(fixture.debugElement.query(By.css('ds-item-grid-element'))).toBeDefined();
-  })
+    expect(fixture.debugElement.query(By.css('ds-item-grid-element'))).toBeDefined()
+  });
+
+  it('should only show the author span if the author metadata is present',()=>{
+    let itemAuthorField = expect(fixture.debugElement.query(By.css('p.item-authors')));
+
+    if(mockItem.filterMetadata(['dc.contributor.author', 'dc.creator', 'dc.contributor.*']).length>0){
+      expect(itemAuthorField).toBeDefined();
+    }else{
+      expect(itemAuthorField).toBeDefined();
+    }
+  });
+
+
 })
diff --git a/src/app/shared/object-grid/search-result-grid-element/collection-search-result/collection-search-result-grid-element.component.spec.ts b/src/app/shared/object-grid/search-result-grid-element/collection-search-result/collection-search-result-grid-element.component.spec.ts
new file mode 100644
index 0000000000..8d6fe6fb38
--- /dev/null
+++ b/src/app/shared/object-grid/search-result-grid-element/collection-search-result/collection-search-result-grid-element.component.spec.ts
@@ -0,0 +1,67 @@
+import {CollectionSearchResultGridElementComponent } from './collection-search-result-grid-element.component';
+import { async, ComponentFixture, TestBed } from '@angular/core/testing';
+import { Observable } from 'rxjs/Observable';
+import { ActivatedRoute, Router } from '@angular/router';
+import { RouterStub } from '../../../testing/router-stub';
+import { NO_ERRORS_SCHEMA } from '@angular/core';
+import { By } from '@angular/platform-browser';
+import { TruncatePipe } from '../../../utils/truncate.pipe';
+import { Community } from '../../../../core/shared/community.model';
+import { Collection } from '../../../../core/shared/collection.model';
+
+
+let fixture: ComponentFixture<CollectionSearchResultGridElementComponent>;
+const queryParam = 'test query';
+const scopeParam = '7669c72a-3f2a-451f-a3b9-9210e7a4c02f';
+const activatedRouteStub = {
+  queryParams: Observable.of({
+    query: queryParam,
+    scope: scopeParam
+  })
+};
+let mockCollection: Collection = Object.assign(new Collection(), {
+  metadata: [
+    {
+      key: 'dc.description.abstract',
+      language: 'en_US',
+      value: 'Short description'
+    } ]
+
+});
+
+let createdGridElementComponent: CollectionSearchResultGridElementComponent = new CollectionSearchResultGridElementComponent(mockCollection);
+
+
+describe('CollectionSearchResultGridElementComponent', () => {
+  beforeEach(async(() => {
+    TestBed.configureTestingModule({
+      declarations: [ CollectionSearchResultGridElementComponent, TruncatePipe ],
+      providers: [
+        { provide: ActivatedRoute, useValue: activatedRouteStub },
+        { provide: Router, useClass: RouterStub },
+        { provide: 'objectElementProvider', useValue: (createdGridElementComponent) }
+      ],
+
+      schemas: [ NO_ERRORS_SCHEMA ]
+    }).compileComponents();  // compile template and css
+  }));
+
+  beforeEach(async(() => {
+    fixture = TestBed.createComponent(CollectionSearchResultGridElementComponent);
+  }));
+
+  it('should show the item result cards in the grid element', () => {
+    expect(fixture.debugElement.query(By.css('ds-collection-search-result-grid-element'))).toBeDefined();
+  });
+
+
+  it('should only show the description if "short description" metadata is present',()=>{
+    let descriptionText = expect(fixture.debugElement.query(By.css('p.card-text')));
+
+    if(mockCollection.shortDescription.length>0){
+      expect(descriptionText).toBeDefined();
+    }else{
+      expect(descriptionText).not.toBeDefined();
+    }
+  });
+});
diff --git a/src/app/shared/object-grid/search-result-grid-element/community-search-result/community-search-result-grid-element.component.spec.ts b/src/app/shared/object-grid/search-result-grid-element/community-search-result/community-search-result-grid-element.component.spec.ts
new file mode 100644
index 0000000000..0924700da7
--- /dev/null
+++ b/src/app/shared/object-grid/search-result-grid-element/community-search-result/community-search-result-grid-element.component.spec.ts
@@ -0,0 +1,67 @@
+import { CommunitySearchResultGridElementComponent } from './community-search-result-grid-element.component';
+import { async, ComponentFixture, TestBed } from '@angular/core/testing';
+import { Observable } from 'rxjs/Observable';
+import { ActivatedRoute, Router } from '@angular/router';
+import { RouterStub } from '../../../testing/router-stub';
+import { NO_ERRORS_SCHEMA } from '@angular/core';
+import { By } from '@angular/platform-browser';
+import { TruncatePipe } from '../../../utils/truncate.pipe';
+import { Community } from '../../../../core/shared/community.model';
+
+
+let communitySearchResultGridElementComponent: CommunitySearchResultGridElementComponent;
+let fixture: ComponentFixture<CommunitySearchResultGridElementComponent>;
+const queryParam = 'test query';
+const scopeParam = '7669c72a-3f2a-451f-a3b9-9210e7a4c02f';
+const activatedRouteStub = {
+  queryParams: Observable.of({
+    query: queryParam,
+    scope: scopeParam
+  })
+};
+let mockCommunity: Community = Object.assign(new Community(), {
+  metadata: [
+    {
+      key: 'dc.description.abstract',
+      language: 'en_US',
+      value: 'Short description'
+    } ]
+
+});
+
+let createdGridElementComponent: CommunitySearchResultGridElementComponent = new CommunitySearchResultGridElementComponent(mockCommunity);
+
+
+describe('CommunitySearchResultGridElementComponent', () => {
+  beforeEach(async(() => {
+    TestBed.configureTestingModule({
+      declarations: [ CommunitySearchResultGridElementComponent, TruncatePipe ],
+      providers: [
+        { provide: ActivatedRoute, useValue: activatedRouteStub },
+        { provide: Router, useClass: RouterStub },
+        { provide: 'objectElementProvider', useValue: (createdGridElementComponent) }
+      ],
+
+      schemas: [ NO_ERRORS_SCHEMA ]
+    }).compileComponents();  // compile template and css
+  }));
+
+  beforeEach(async(() => {
+    fixture = TestBed.createComponent(CommunitySearchResultGridElementComponent);
+  }));
+
+  it('should show the item result cards in the grid element', () => {
+    expect(fixture.debugElement.query(By.css('ds-community-search-result-grid-element'))).toBeDefined();
+  });
+
+
+  it('should only show the description if "short description" metadata is present',()=>{
+    let descriptionText = expect(fixture.debugElement.query(By.css('p.card-text')));
+
+    if(mockCommunity.shortDescription.length>0){
+      expect(descriptionText).toBeDefined();
+    }else{
+      expect(descriptionText).not.toBeDefined();
+    }
+  });
+});
diff --git a/src/app/shared/object-grid/search-result-grid-element/item-search-result/item-search-result-grid-element.component.spec.ts b/src/app/shared/object-grid/search-result-grid-element/item-search-result/item-search-result-grid-element.component.spec.ts
new file mode 100644
index 0000000000..318910304f
--- /dev/null
+++ b/src/app/shared/object-grid/search-result-grid-element/item-search-result/item-search-result-grid-element.component.spec.ts
@@ -0,0 +1,84 @@
+import { ItemSearchResultGridElementComponent } from './item-search-result-grid-element.component';
+import { async, ComponentFixture, TestBed } from '@angular/core/testing';
+import { Observable } from 'rxjs/Observable';
+import { ActivatedRoute, Router } from '@angular/router';
+import { RouterStub } from '../../../testing/router-stub';
+import { 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';
+
+
+let itemSearchResultGridElementComponent: ItemSearchResultGridElementComponent;
+let fixture: ComponentFixture<ItemSearchResultGridElementComponent>;
+const queryParam = 'test query';
+const scopeParam = '7669c72a-3f2a-451f-a3b9-9210e7a4c02f';
+const activatedRouteStub = {
+  queryParams: Observable.of({
+    query: queryParam,
+    scope: scopeParam
+  })
+};
+let mockItem: Item = Object.assign(new Item(), {
+  metadata: [
+    {
+      key: 'dc.contributor.author',
+      language: 'en_US',
+      value: 'Smith, Donald'
+    },
+    {
+      key: 'dc.date.issued',
+      language: null,
+      value: '1650-06-26'
+    }]
+});
+let createdGridElementComponent:ItemSearchResultGridElementComponent= new ItemSearchResultGridElementComponent(mockItem);
+
+describe('ItemSearchResultGridElementComponent', () => {
+  beforeEach(async(() => {
+    TestBed.configureTestingModule({
+      declarations: [ ItemSearchResultGridElementComponent, TruncatePipe ],
+      providers: [
+        { provide: ActivatedRoute, useValue: activatedRouteStub },
+        { provide: Router, useClass: RouterStub },
+        { provide: 'objectElementProvider', useValue: (createdGridElementComponent) }
+      ],
+
+      schemas: [ NO_ERRORS_SCHEMA ]
+    }).compileComponents();  // compile template and css
+  }));
+
+  beforeEach(async(() => {
+    fixture = TestBed.createComponent(ItemSearchResultGridElementComponent);
+    itemSearchResultGridElementComponent = fixture.componentInstance;
+
+  }));
+
+  it('should show the item result cards in the grid element',()=>{
+    expect(fixture.debugElement.query(By.css('ds-item-search-result-grid-element'))).toBeDefined();
+  });
+
+  it('should only show the author span if the author metadata is present',()=>{
+    let itemAuthorField = expect(fixture.debugElement.query(By.css('p.item-authors')));
+
+    if(mockItem.filterMetadata(['dc.contributor.author', 'dc.creator', 'dc.contributor.*']).length>0){
+      expect(itemAuthorField).toBeDefined();
+    }else{
+      expect(itemAuthorField).not.toBeDefined();
+    }
+  });
+
+  it('should only show the date span if the issuedate is present',()=>{
+    let dateField = expect(fixture.debugElement.query(By.css('span.item-list-date')));
+
+    if(mockItem.findMetadata('dc.date.issued').length>0){
+      expect(dateField).toBeDefined();
+    }else{
+      expect(dateField).not.toBeDefined();
+    }
+  });
+
+
+
+
+});
-- 
GitLab