Skip to content

Commit

Permalink
SHIBUI-1385 Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmathis committed Aug 30, 2019
1 parent f778a74 commit 51bfa3c
Show file tree
Hide file tree
Showing 10 changed files with 522 additions and 26 deletions.
4 changes: 2 additions & 2 deletions ui/src/app/metadata/configuration/action/restore.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ export class RestoreVersionError implements Action {

export class UpdateRestorationChangesRequest implements Action {
readonly type = RestoreActionTypes.UPDATE_RESTORATION_REQUEST;
constructor(public payload: any) { }
constructor(public payload: Partial<Metadata>) { }
}

export class UpdateRestorationChangesSuccess implements Action {
readonly type = RestoreActionTypes.UPDATE_RESTORATION_SUCCESS;
constructor(public payload: any) { }
constructor(public payload: Partial<Metadata>) { }
}

export class UpdateRestoreFormStatus implements Action {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Component, ViewChild, Input } from '@angular/core';
import { Component, ViewChild } from '@angular/core';
import { TestBed, async, ComponentFixture } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { StoreModule, combineReducers } from '@ngrx/store';
import { NgbDropdownModule } from '@ng-bootstrap/ng-bootstrap';

import { MetadataConfiguration } from '../model/metadata-configuration';
import { ConfigurationComponent } from './configuration.component';
import * as fromConfiguration from '../reducer';
import * as fromProviders from '../../provider/reducer';
Expand All @@ -19,11 +18,6 @@ import { MockI18nModule } from '../../../../testing/i18n.stub';
class TestHostComponent {
@ViewChild(ConfigurationComponent)
public componentUnderTest: ConfigurationComponent;

configuration: MetadataConfiguration = {
dates: [],
sections: []
};
}

describe('Metadata Configuration Page Component', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { Component, ViewChild, NO_ERRORS_SCHEMA } from '@angular/core';
import { TestBed, async, ComponentFixture } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { StoreModule, combineReducers, Store } from '@ngrx/store';
import { NgbDropdownModule } from '@ng-bootstrap/ng-bootstrap';

import { RestoreEditStepComponent } from './restore-edit-step.component';
import * as fromConfiguration from '../reducer';
import * as fromProviders from '../../provider/reducer';
import * as fromResolvers from '../../resolver/reducer';
import * as fromWizard from '../../../wizard/reducer';
import { MockI18nModule } from '../../../../testing/i18n.stub';

import {
RestoreActionTypes
} from '../action/restore.action';
import { WizardActionTypes } from '../../../wizard/action/wizard.action';

@Component({
template: `
<restore-edit-step></restore-edit-step>
`
})
class TestHostComponent {
@ViewChild(RestoreEditStepComponent)
public componentUnderTest: RestoreEditStepComponent;
}

describe('Restore Version Edit Step Component', () => {

let fixture: ComponentFixture<TestHostComponent>;
let instance: TestHostComponent;
let app: RestoreEditStepComponent;
let store: Store<fromConfiguration.State>;
let dispatchSpy;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
NgbDropdownModule,
StoreModule.forRoot({
'metadata-configuration': combineReducers(fromConfiguration.reducers),
'provider': combineReducers(fromProviders.reducers),
'resolver': combineReducers(fromResolvers.reducers),
'wizard': combineReducers(fromWizard.reducers)
}),
MockI18nModule,
RouterTestingModule
],
declarations: [
RestoreEditStepComponent,
TestHostComponent
],
schemas: [
NO_ERRORS_SCHEMA
]
}).compileComponents();

store = TestBed.get(Store);
dispatchSpy = spyOn(store, 'dispatch');

fixture = TestBed.createComponent(TestHostComponent);
instance = fixture.componentInstance;
app = instance.componentUnderTest;
fixture.detectChanges();
}));

it('should compile', () => {
expect(app).toBeTruthy();
});

describe('onChange', () => {
it('should dispatch an update changes event', () => {
app.onChange({ name: 'test' });
expect(store.dispatch).toHaveBeenCalled();
expect(dispatchSpy.calls.mostRecent().args[0].type).toBe(RestoreActionTypes.UPDATE_RESTORATION_REQUEST);
});
});

describe('updateStatus', () => {
it('should dispatch an update form status event', () => {
app.updateStatus([{ value: 'foo' }, 'common']);
expect(store.dispatch).toHaveBeenCalled();
expect(dispatchSpy.calls.mostRecent().args[0].type).toBe(RestoreActionTypes.UPDATE_STATUS);
});

it('should dispatch an update form status event', () => {
app.updateStatus([{}, 'common']);
expect(store.dispatch).toHaveBeenCalled();
expect(dispatchSpy.calls.mostRecent().args[0].type).toBe(RestoreActionTypes.UPDATE_STATUS);
});
});

describe('updateLock', () => {
it('should dispatch a LockEditor event when passed a locked status', () => {
app.updateLock(true);
expect(store.dispatch).toHaveBeenCalled();
expect(dispatchSpy.calls.mostRecent().args[0].type).toBe(WizardActionTypes.LOCK);
});

it('should dispatch a UnlockEditor event when passed a locked status', () => {
app.updateLock(false);
expect(store.dispatch).toHaveBeenCalled();
expect(dispatchSpy.calls.mostRecent().args[0].type).toBe(WizardActionTypes.UNLOCK);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { getWizardDefinition, getSchema, getValidators, getCurrent, getWizardIndex } from '../../../wizard/reducer';
import { Wizard, WizardStep } from '../../../wizard/model';
import { Metadata } from '../../domain/domain.type';
import { map, switchMap, withLatestFrom } from 'rxjs/operators';
import { map, switchMap, withLatestFrom, filter } from 'rxjs/operators';
import { NAV_FORMATS } from '../../domain/component/editor-nav.component';
import { UpdateRestorationChangesRequest, UpdateRestoreFormStatus } from '../action/restore.action';
import { LockEditor, UnlockEditor } from '../../../wizard/action/wizard.action';
Expand All @@ -32,45 +32,51 @@ export class RestoreEditStepComponent implements OnDestroy {
model$: Observable<Metadata> = this.store.select(getFormattedModel);
step$: Observable<WizardStep> = this.store.select(getCurrent);

lockable$: Observable<boolean> = this.step$.pipe(map(step => step.locked));
lockable$: Observable<boolean> = this.step$.pipe(filter(s => !!s), map(step => step.locked));

validators$: Observable<any>;

formats = NAV_FORMATS;

constructor(
private store: Store<ConfigurationState>,
private route: ActivatedRoute
private store: Store<ConfigurationState>
) {
this.validators$ = this.definition$.pipe(
filter(def => !!def),
map(def => def.validatorParams),
switchMap(params => combineLatest(params.map(p => this.store.select(p)))),
switchMap(selections => this.store.select(getValidators(selections)))
);

this.step$
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe(s => this.lockChange$.next(s && s.locked ? true : false));
.pipe(takeUntil(this.ngUnsubscribe), filter(step => !!step))
.subscribe(s => this.lockChange$.next(s.locked ? true : false));

this.lockChange$
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe(locked => this.store.dispatch(locked ? new LockEditor() : new UnlockEditor()));
.subscribe(this.updateLock);

this.statusChange$
.pipe(
takeUntil(this.ngUnsubscribe),
withLatestFrom(this.store.select(getWizardIndex))
)
.subscribe(([errors, currentPage]) => {
const status = { [currentPage]: !(errors.value) ? 'VALID' : 'INVALID' };
this.store.dispatch(new UpdateRestoreFormStatus(status));
});
.subscribe(this.updateStatus);
}

onChange(changes: any): void {
this.store.dispatch(new UpdateRestorationChangesRequest(changes));
}

updateStatus([errors, currentPage]) {
const status = { [currentPage]: !(errors.value) ? 'VALID' : 'INVALID' };
this.store.dispatch(new UpdateRestoreFormStatus(status));
}

updateLock(locked: boolean) {
this.store.dispatch(locked ? new LockEditor() : new UnlockEditor());
}

ngOnDestroy() {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Component, ViewChild, NO_ERRORS_SCHEMA } from '@angular/core';
import { TestBed, async, ComponentFixture } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { StoreModule, combineReducers, Store } from '@ngrx/store';
import { NgbDropdownModule } from '@ng-bootstrap/ng-bootstrap';

import { RestoreEditComponent } from './restore-edit.component';
import * as fromConfiguration from '../reducer';
import * as fromProviders from '../../provider/reducer';
import * as fromResolvers from '../../resolver/reducer';
import { MockI18nModule } from '../../../../testing/i18n.stub';
import { RestoreActionTypes } from '../action/restore.action';

@Component({
template: `
<restore-edit></restore-edit>
`
})
class TestHostComponent {
@ViewChild(RestoreEditComponent)
public componentUnderTest: RestoreEditComponent;
}

describe('Restore Version Edit Component', () => {

let fixture: ComponentFixture<TestHostComponent>;
let instance: TestHostComponent;
let app: RestoreEditComponent;
let store: Store<fromConfiguration.State>;
let dispatchSpy;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
NgbDropdownModule,
StoreModule.forRoot({
'metadata-configuration': combineReducers(fromConfiguration.reducers),
'provider': combineReducers(fromProviders.reducers),
'resolver': combineReducers(fromResolvers.reducers)
}),
MockI18nModule,
RouterTestingModule
],
declarations: [
RestoreEditComponent,
TestHostComponent
],
schemas: [ NO_ERRORS_SCHEMA ]
}).compileComponents();

store = TestBed.get(Store);
dispatchSpy = spyOn(store, 'dispatch');

fixture = TestBed.createComponent(TestHostComponent);
instance = fixture.componentInstance;
app = instance.componentUnderTest;
fixture.detectChanges();
}));

it('should compile', () => {
expect(app).toBeTruthy();
});

describe('save', () => {
it('should dispatch a save request event', () => {
app.save();
expect(store.dispatch).toHaveBeenCalled();
expect(dispatchSpy.calls.mostRecent().args[0].type).toBe(RestoreActionTypes.RESTORE_VERSION_REQUEST);
});
});

describe('cancel', () => {
it('should dispatch a cancel request event', () => {
app.cancel();
expect(store.dispatch).toHaveBeenCalled();
expect(dispatchSpy.calls.mostRecent().args[0].type).toBe(RestoreActionTypes.CANCEL_RESTORE);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Component, ViewChild, NO_ERRORS_SCHEMA } from '@angular/core';
import { TestBed, async, ComponentFixture } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { StoreModule, combineReducers } from '@ngrx/store';
import { NgbDropdownModule } from '@ng-bootstrap/ng-bootstrap';

import { VersionOptionsComponent } from './version-options.component';
import * as fromConfiguration from '../reducer';
import * as fromProviders from '../../provider/reducer';
import * as fromResolvers from '../../resolver/reducer';
import { MockI18nModule } from '../../../../testing/i18n.stub';
import { Metadata } from '../../domain/domain.type';
import { ViewportScroller } from '@angular/common';

@Component({
template: `
<version-options-page></version-options-page>
`
})
class TestHostComponent {
@ViewChild(VersionOptionsComponent)
public componentUnderTest: VersionOptionsComponent;
}

describe('Metadata Version Options Page Component', () => {

let fixture: ComponentFixture<TestHostComponent>;
let instance: TestHostComponent;
let app: VersionOptionsComponent;
let scroller: ViewportScroller;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
NgbDropdownModule,
StoreModule.forRoot({
'metadata-configuration': combineReducers(fromConfiguration.reducers),
'provider': combineReducers(fromProviders.reducers),
'resolver': combineReducers(fromResolvers.reducers)
}),
MockI18nModule,
RouterTestingModule
],
declarations: [
VersionOptionsComponent,
TestHostComponent
],
schemas: [ NO_ERRORS_SCHEMA ]
}).compileComponents();

scroller = TestBed.get(ViewportScroller);

fixture = TestBed.createComponent(TestHostComponent);
instance = fixture.componentInstance;
app = instance.componentUnderTest;
fixture.detectChanges();
}));

it('should compile', () => {
expect(app).toBeTruthy();
});

describe('setModel method', () => {
it('should set attributes based on the passed data', () => {
app.setModel({ id: 'foo', '@type': 'bar' } as Metadata);
expect(app.id).toBe('foo');
expect(app.kind).toBe('provider');

app.setModel({ resourceId: 'baz' } as Metadata);
expect(app.id).toBe('baz');
expect(app.kind).toBe('resolver');
});
});

describe('onScrollTo method', () => {
it('should set attributes based on the passed data', () => {
spyOn(scroller, 'scrollToAnchor');
app.onScrollTo('foo');
expect(scroller.scrollToAnchor).toHaveBeenCalledWith('foo');
});
});
});
Loading

0 comments on commit 51bfa3c

Please sign in to comment.