From a46c7537389ff7a26356680cd086625fd66da03c Mon Sep 17 00:00:00 2001 From: Ryan Mathis Date: Fri, 21 Jun 2019 07:49:54 -0700 Subject: [PATCH] SHIBUI-1268 Implemented tests for metadata header --- .../component/history-list.component.html | 4 +- .../component/history-list.component.spec.ts | 25 +++---- .../metadata-header.component.spec.ts | 58 +++++++++++++++ .../component/metadata-header.component.ts | 6 +- .../metadata-history.component.spec.ts | 7 +- .../container/metadata-options.component.html | 2 +- .../metadata-options.component.spec.ts | 20 +++++- .../container/metadata-options.component.ts | 9 +-- .../reducer/history.reducer.spec.ts | 70 +++++++++++++++++++ .../configuration/reducer/history.reducer.ts | 4 +- .../service/configuration.service.spec.ts | 3 +- 11 files changed, 177 insertions(+), 31 deletions(-) diff --git a/ui/src/app/metadata/configuration/component/history-list.component.html b/ui/src/app/metadata/configuration/component/history-list.component.html index b22da0fc3..2983c151c 100644 --- a/ui/src/app/metadata/configuration/component/history-list.component.html +++ b/ui/src/app/metadata/configuration/component/history-list.component.html @@ -26,7 +26,7 @@ {{ version.date | date }} {{ version.creator }} - @@ -34,6 +34,6 @@ - \ No newline at end of file diff --git a/ui/src/app/metadata/configuration/component/history-list.component.spec.ts b/ui/src/app/metadata/configuration/component/history-list.component.spec.ts index 4cb6770eb..43d49a29f 100644 --- a/ui/src/app/metadata/configuration/component/history-list.component.spec.ts +++ b/ui/src/app/metadata/configuration/component/history-list.component.spec.ts @@ -8,16 +8,15 @@ import { MetadataVersion } from '../model/version'; export const TestData = { versions: [ { - versionNumber: 1, - saveDate: new Date(), - changedBy: 'admin', - actions: [] + id: 'foo', + date: new Date().toDateString(), + creator: 'admin' } ] }; @Component({ - template: `` + template: `` }) class TestHostComponent { @ViewChild(HistoryListComponent) @@ -32,7 +31,7 @@ class TestHostComponent { describe('Metadata History List Component', () => { let fixture: ComponentFixture; let instance: TestHostComponent; - let table: HistoryListComponent; + let list: HistoryListComponent; beforeEach(() => { TestBed.configureTestingModule({ @@ -48,32 +47,34 @@ describe('Metadata History List Component', () => { fixture = TestBed.createComponent(TestHostComponent); instance = fixture.componentInstance; - table = instance.componentUnderTest; + list = instance.componentUnderTest; fixture.detectChanges(); }); it('should compile', () => { - expect(table).toBeDefined(); + expect(list).toBeDefined(); }); describe('compare selected', () => { it('should allow the user to toggle selected versions for comparison', () => { - table.toggleVersionSelected(TestData.versions[0]); - expect(table.selected.length).toBe(1); + list.toggleVersionSelected(TestData.versions[0]); + expect(list.selected.length).toBe(1); }); it('should emit an event with the selected values when the Compare Selected button is clicked', () => { spyOn(instance, 'compare'); const selected = TestData.versions; - table.compareSelected(selected); + list.compareSelected(selected); fixture.detectChanges(); expect(instance.compare).toHaveBeenCalledWith(selected); }); + }); + describe('restore', () => { it('should emit an event with the selected version when the Restore button is clicked', () => { spyOn(instance, 'restore'); const selected = TestData.versions[0]; - table.restoreVersion(selected); + list.restoreVersion(selected); fixture.detectChanges(); expect(instance.restore).toHaveBeenCalledWith(selected); }); diff --git a/ui/src/app/metadata/configuration/component/metadata-header.component.spec.ts b/ui/src/app/metadata/configuration/component/metadata-header.component.spec.ts index e69de29bb..e8db42c36 100644 --- a/ui/src/app/metadata/configuration/component/metadata-header.component.spec.ts +++ b/ui/src/app/metadata/configuration/component/metadata-header.component.spec.ts @@ -0,0 +1,58 @@ +import { Component, ViewChild } from '@angular/core'; +import { TestBed, async, ComponentFixture } from '@angular/core/testing'; +import { MockI18nModule } from '../../../../testing/i18n.stub'; +import { MetadataVersion } from '../model/version'; +import { MetadataHeaderComponent } from './metadata-header.component'; + +@Component({ + template: ` + + ` +}) +class TestHostComponent { + @ViewChild(MetadataHeaderComponent) + public componentUnderTest: MetadataHeaderComponent; + + isEnabled = true; + + version: MetadataVersion = { + id: 'foo', + creator: 'foobar', + date: new Date().toDateString() + }; + versionNumber = 1; + isCurrent = false; +} + +describe('Metadata Header Component', () => { + + let fixture: ComponentFixture; + let instance: TestHostComponent; + let app: MetadataHeaderComponent; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + imports: [ + MockI18nModule + ], + declarations: [ + MetadataHeaderComponent, + TestHostComponent + ] + }).compileComponents(); + + fixture = TestBed.createComponent(TestHostComponent); + instance = fixture.componentInstance; + app = instance.componentUnderTest; + fixture.detectChanges(); + })); + + it('should accept a property input', async(() => { + expect(app).toBeTruthy(); + })); +}); diff --git a/ui/src/app/metadata/configuration/component/metadata-header.component.ts b/ui/src/app/metadata/configuration/component/metadata-header.component.ts index da4bd643a..8d0dcc3db 100644 --- a/ui/src/app/metadata/configuration/component/metadata-header.component.ts +++ b/ui/src/app/metadata/configuration/component/metadata-header.component.ts @@ -9,15 +9,11 @@ import { MetadataVersion } from '../model/version'; }) export class MetadataHeaderComponent { - @Input() metadata: Metadata; + @Input() isEnabled: boolean; @Input() version: MetadataVersion; @Input() versionNumber: number; @Input() isCurrent: boolean; constructor() {} - - get isEnabled(): boolean { - return this.metadata ? ('serviceEnabled' in this.metadata) ? this.metadata.serviceEnabled : this.metadata.enabled : false; - } } diff --git a/ui/src/app/metadata/configuration/container/metadata-history.component.spec.ts b/ui/src/app/metadata/configuration/container/metadata-history.component.spec.ts index ec6ccdc7a..084d79ef0 100644 --- a/ui/src/app/metadata/configuration/container/metadata-history.component.spec.ts +++ b/ui/src/app/metadata/configuration/container/metadata-history.component.spec.ts @@ -6,6 +6,8 @@ import { MetadataHistory } from '../model/history'; import { MetadataVersion } from '../model/version'; import { MetadataHistoryService } from '../service/history.service'; import { of } from 'rxjs'; +import { StoreModule, combineReducers } from '@ngrx/store'; +import * as fromConfiguration from '../reducer'; export const TestData = { versions: [ @@ -44,7 +46,10 @@ describe('Metadata Version History Component', () => { } ], imports: [ - MockI18nModule + MockI18nModule, + StoreModule.forRoot({ + 'metadata-configuration': combineReducers(fromConfiguration.reducers), + }), ], declarations: [ MockHistoryListComponent, diff --git a/ui/src/app/metadata/configuration/container/metadata-options.component.html b/ui/src/app/metadata/configuration/container/metadata-options.component.html index e5e9475b0..358d16921 100644 --- a/ui/src/app/metadata/configuration/container/metadata-options.component.html +++ b/ui/src/app/metadata/configuration/container/metadata-options.component.html @@ -1,6 +1,6 @@
diff --git a/ui/src/app/metadata/configuration/container/metadata-options.component.spec.ts b/ui/src/app/metadata/configuration/container/metadata-options.component.spec.ts index 2e35d8c75..0cb1bd5b0 100644 --- a/ui/src/app/metadata/configuration/container/metadata-options.component.spec.ts +++ b/ui/src/app/metadata/configuration/container/metadata-options.component.spec.ts @@ -8,6 +8,9 @@ import { MetadataConfiguration } from '../model/metadata-configuration'; import * as fromConfiguration from '../reducer'; import { MockI18nModule } from '../../../../testing/i18n.stub'; import { MetadataOptionsComponent } from './metadata-options.component'; +import { Metadata } from '../../domain/domain.type'; +import { MetadataVersion } from '../model/version'; +import { CommonModule } from '@angular/common'; @Component({ selector: 'metadata-configuration', @@ -17,6 +20,17 @@ class MetadataConfigurationComponent { @Input() configuration: MetadataConfiguration; } +@Component({ + selector: 'metadata-header', + template: `` +}) +class MetadataHeaderComponent { + @Input() isEnabled: boolean; + @Input() version: MetadataVersion; + @Input() versionNumber: number; + @Input() isCurrent: boolean; +} + @Component({ template: ` @@ -44,18 +58,20 @@ describe('Metadata Options Page Component', () => { 'metadata-configuration': combineReducers(fromConfiguration.reducers), }), MockI18nModule, - RouterTestingModule + RouterTestingModule, + CommonModule ], declarations: [ MetadataOptionsComponent, MetadataConfigurationComponent, + MetadataHeaderComponent, TestHostComponent ], }).compileComponents(); store = TestBed.get(Store); spyOn(store, 'dispatch'); - spyOn(store, 'select'); + spyOn(store, 'select').and.callThrough(); fixture = TestBed.createComponent(TestHostComponent); instance = fixture.componentInstance; diff --git a/ui/src/app/metadata/configuration/container/metadata-options.component.ts b/ui/src/app/metadata/configuration/container/metadata-options.component.ts index 83d176a6f..461252f1d 100644 --- a/ui/src/app/metadata/configuration/container/metadata-options.component.ts +++ b/ui/src/app/metadata/configuration/container/metadata-options.component.ts @@ -11,8 +11,8 @@ import { getSelectedIsCurrent } from '../reducer'; import { MetadataConfiguration } from '../model/metadata-configuration'; -import { Metadata } from '../../domain/domain.type'; import { MetadataVersion } from '../model/version'; +import { map } from 'rxjs/operators'; @Component({ selector: 'metadata-options-page', @@ -23,7 +23,7 @@ import { MetadataVersion } from '../model/version'; export class MetadataOptionsComponent { configuration$: Observable; - model$: Observable; + isEnabled$: Observable; version$: Observable; versionNumber$: Observable; isCurrent$: Observable; @@ -32,8 +32,9 @@ export class MetadataOptionsComponent { private store: Store ) { this.configuration$ = this.store.select(getConfigurationSections); - this.model$ = this.store.select(getConfigurationModel); - + this.isEnabled$ = this.store.select(getConfigurationModel).pipe( + map(config => config ? ('serviceEnabled' in config) ? config.serviceEnabled : config.enabled : false) + ); this.version$ = this.store.select(getSelectedVersion); this.versionNumber$ = this.store.select(getSelectedVersionNumber); this.isCurrent$ = this.store.select(getSelectedIsCurrent); diff --git a/ui/src/app/metadata/configuration/reducer/history.reducer.spec.ts b/ui/src/app/metadata/configuration/reducer/history.reducer.spec.ts index e69de29bb..a3bc830e5 100644 --- a/ui/src/app/metadata/configuration/reducer/history.reducer.spec.ts +++ b/ui/src/app/metadata/configuration/reducer/history.reducer.spec.ts @@ -0,0 +1,70 @@ +import { reducer } from './history.reducer'; +import * as fromHistory from './history.reducer'; +import * as actions from '../action/history.action'; +import { MetadataHistory } from '../model/history'; + +describe('History Reducer', () => { + + const baseState = fromHistory.initialState; + + const history: MetadataHistory = { + versions: [ + { + id: '1', + date: new Date().toLocaleDateString(), + creator: 'foo' + }, + { + id: '2', + date: new Date().toDateString(), + creator: 'foo' + } + ] + }; + + describe('undefined action', () => { + it('should return the default state', () => { + const result = reducer(undefined, {} as any); + + expect(result).toEqual(fromHistory.initialState); + }); + }); + + describe('SET_HISTORY action', () => { + it('should set the state metadata model', () => { + const action = new actions.SetHistory(history); + const result = reducer(fromHistory.initialState, action); + + expect(Object.keys(result.entities)).toEqual(['1', '2']); + }); + }); + + describe('SELECT_VERSION action', () => { + it('should set the state metadata model', () => { + const action = new actions.SelectVersion('1'); + const result = reducer(baseState, action); + + expect(result).toEqual({ ...baseState, selectedVersionId: '1' }); + }); + }); + + describe('CLEAR action', () => { + it('should clear the state and reset to initial state', () => { + const action = new actions.ClearHistory(); + const result = reducer({ + ...baseState, + ...history + }, action); + + expect(result).toEqual(baseState); + }); + }); + + describe('selector functions', () => { + describe('getSelectedId', () => { + it('should return the selected version id', () => { + expect(fromHistory.getSelectedVersionId({ ...baseState, selectedVersionId: '1' })).toBe('1'); + }); + }); + }); +}); diff --git a/ui/src/app/metadata/configuration/reducer/history.reducer.ts b/ui/src/app/metadata/configuration/reducer/history.reducer.ts index 244a55f1c..8a2566f59 100644 --- a/ui/src/app/metadata/configuration/reducer/history.reducer.ts +++ b/ui/src/app/metadata/configuration/reducer/history.reducer.ts @@ -4,7 +4,6 @@ import { MetadataVersion } from '../model/version'; export interface HistoryState extends EntityState { selectedVersionId: string; - versions: MetadataVersion[]; } export function sortByDate(a: MetadataVersion, b: MetadataVersion): number { @@ -17,8 +16,7 @@ export const adapter: EntityAdapter = createEntityAdapter { beforeEach(() => {