Skip to content

Commit

Permalink
SHIBUI-580 Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmathis committed Jul 6, 2018
1 parent 23e6343 commit 8759c88
Show file tree
Hide file tree
Showing 17 changed files with 497 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { Component, ViewChild } from '@angular/core';
import { TestBed, async, ComponentFixture } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { StoreModule, Store, combineReducers } from '@ngrx/store';

import { NgbDropdownModule } from '@ng-bootstrap/ng-bootstrap';

import { ProviderWizardSummaryComponent } from './provider-wizard-summary.component';
import * as fromRoot from '../reducer';
import { SchemaFormModule, WidgetRegistry, DefaultWidgetRegistry } from 'ngx-schema-form';
import * as fromWizard from '../../../wizard/reducer';
import { Wizard } from '../../../wizard/model';
import { MetadataProvider } from '../../domain/model';
import { SummaryPropertyComponent } from './summary-property.component';
import { SCHEMA } from '../../../../testing/form-schema.stub';
import { MetadataProviderWizard } from '../model';

@Component({
template: `
<provider-wizard-summary [summary]="summary"></provider-wizard-summary>
`
})
class TestHostComponent {
@ViewChild(ProviderWizardSummaryComponent)
public componentUnderTest: ProviderWizardSummaryComponent;

private _summary;

get summary(): { definition: Wizard<MetadataProvider>, schema: { [id: string]: any }, model: any } {
return this._summary;
}

set summary(summary: { definition: Wizard<MetadataProvider>, schema: { [id: string]: any }, model: any }) {
this._summary = summary;
}
}

describe('Provider Wizard Summary Component', () => {

let fixture: ComponentFixture<TestHostComponent>;
let instance: TestHostComponent;
let app: ProviderWizardSummaryComponent;
let store: Store<fromRoot.State>;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
NgbDropdownModule.forRoot(),
RouterTestingModule,
SchemaFormModule.forRoot(),
StoreModule.forRoot({
provider: combineReducers(fromRoot.reducers),
wizard: combineReducers(fromWizard.reducers)
})
],
declarations: [
ProviderWizardSummaryComponent,
SummaryPropertyComponent,
TestHostComponent
],
providers: [
{ provide: WidgetRegistry, useClass: DefaultWidgetRegistry }
]
}).compileComponents();

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

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

it('should instantiate the component', async(() => {
expect(app).toBeTruthy();
}));

describe('ngOnChanges', () => {
it('should set columns and sections if summary is provided', () => {
instance.summary = {
model: {
name: 'foo',
'@type': 'MetadataProvider'
},
schema: SCHEMA,
definition: MetadataProviderWizard
};
fixture.detectChanges();
expect(app.sections).toBeDefined();
expect(app.columns).toBeDefined();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class ProviderWizardSummaryComponent implements OnChanges {
id: step.id,
index: step.index,
label: step.label,
properties: getStepProperties(schemas[step.id], def.translate ? def.translate.formatter(model) : model)
properties: getStepProperties(schemas[step.id], def.translate.formatter(model))
})
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Component, ViewChild } from '@angular/core';
import { TestBed, async, ComponentFixture } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { StoreModule, Store, combineReducers } from '@ngrx/store';

import { NgbDropdownModule } from '@ng-bootstrap/ng-bootstrap';

import { SummaryPropertyComponent } from './summary-property.component';
import * as fromRoot from '../reducer';
import { SchemaFormModule, WidgetRegistry, DefaultWidgetRegistry } from 'ngx-schema-form';
import * as fromWizard from '../../../wizard/reducer';
import { Wizard } from '../../../wizard/model';
import { MetadataProvider } from '../../domain/model';
import { Property } from '../model/property';

@Component({
template: `
<summary-property [property]="property"></summary-property>
`
})
class TestHostComponent {
@ViewChild(SummaryPropertyComponent)
public componentUnderTest: SummaryPropertyComponent;

private _property;

get property(): Property {
return this._property;
}

set property(prop: Property) {
this._property = prop;
}
}

describe('Summary Property Component', () => {

let fixture: ComponentFixture<TestHostComponent>;
let instance: TestHostComponent;
let app: SummaryPropertyComponent;
let store: Store<fromRoot.State>;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
NgbDropdownModule.forRoot(),
RouterTestingModule,
SchemaFormModule.forRoot(),
StoreModule.forRoot({
provider: combineReducers(fromRoot.reducers),
wizard: combineReducers(fromWizard.reducers)
})
],
declarations: [
SummaryPropertyComponent,
TestHostComponent
],
providers: [
{ provide: WidgetRegistry, useClass: DefaultWidgetRegistry }
]
}).compileComponents();

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

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

it('should instantiate the component', async(() => {
expect(app).toBeTruthy();
}));
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { ProviderWizardStepComponent } from './provider-wizard-step.component';
import * as fromRoot from '../reducer';
import { SchemaFormModule, WidgetRegistry, DefaultWidgetRegistry } from 'ngx-schema-form';
import * as fromWizard from '../../../wizard/reducer';
import { SCHEMA } from '../../../../testing/form-schema.stub';
import { MetadataProviderWizard } from '../model';

@Component({
template: `
Expand Down Expand Up @@ -59,4 +61,38 @@ describe('Provider Wizard Step Component', () => {
it('should instantiate the component', async(() => {
expect(app).toBeTruthy();
}));

describe('resetSelectedType method', () => {
it('should dispatch a SetDefinition action if the type has changed', () => {
app.resetSelectedType({ value: { name: 'foo', '@type': 'FileBackedHttpMetadataResolver' } }, SCHEMA, MetadataProviderWizard);
expect(store.dispatch).toHaveBeenCalled();
});

it('should NOT dispatch a SetDefinition action if the type hasn\'t changed', () => {
app.resetSelectedType({ value: { name: 'foo', '@type': 'MetadataProvider' } }, SCHEMA, MetadataProviderWizard);
expect(store.dispatch).not.toHaveBeenCalled();
});

it('should NOT dispatch a SetDefinition action if the type isn\'t found', () => {
app.resetSelectedType({ value: { name: 'foo', '@type': 'FooProvider' } }, SCHEMA, MetadataProviderWizard);
expect(store.dispatch).not.toHaveBeenCalled();
});

it('should return changes and definition if no type supplied', () => {
app.resetSelectedType({ value: { name: 'foo' } }, SCHEMA, MetadataProviderWizard);
expect(store.dispatch).not.toHaveBeenCalled();
});
});

describe('updateStatus method', () => {
it('should dispatch an UpdateStatus action', () => {
app.updateStatus({value: { name: 'notfound'} });
expect(store.dispatch).toHaveBeenCalled();
});

it('should NOT dispatch a SetDefinition action if the type hasn\'t changed', () => {
app.updateStatus({ value: null });
expect(store.dispatch).toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, OnDestroy } from '@angular/core';
import { Observable, Subject } from 'rxjs';
import { withLatestFrom, map, distinctUntilChanged } from 'rxjs/operators';
import { withLatestFrom, map, distinctUntilChanged, skipWhile } from 'rxjs/operators';
import { Store } from '@ngrx/store';

import * as fromProvider from '../reducer';
Expand Down Expand Up @@ -86,44 +86,47 @@ export class ProviderWizardStepComponent implements OnDestroy {
},
definition
})),
map(({ model, definition }) => definition && definition.translate ? definition.translate.formatter(model) : model)
skipWhile(({ model, definition }) => !definition || !model),
map(({ model, definition }) => definition.translate.formatter(model))
);

this.valueChangeEmitted$.pipe(
withLatestFrom(this.schema$, this.definition$),
map(([changes, schema, definition]) => {
const type = changes.value['@type'];
if (type && type !== definition.type) {
const newDefinition = MetadataProviderTypes.find(def => def.type === type);
if (newDefinition) {
this.store.dispatch(new SetDefinition({
...MetadataProviderWizard,
...newDefinition,
steps: [
...MetadataProviderWizard.steps,
...newDefinition.steps
]
}));
changes = { value: pick(Object.keys(schema.properties))(changes.value) };
}
}
return { changes: changes.value, definition };
}),
map(({ changes, definition }) => definition.translate ? definition.translate.parser(changes) : changes)
map(([changes, schema, definition]) => this.resetSelectedType(changes, schema, definition)),
skipWhile(({ changes, definition }) => !definition || !changes),
map(({ changes, definition }) => definition.translate.parser(changes))
)
.subscribe(changes => this.store.dispatch(new UpdateProvider(changes)));

this.statusChangeEmitted$.pipe(
distinctUntilChanged()
).subscribe(errors => {
console.log(!(errors.value));
const status = { [this.currentPage]: !(errors.value) ? 'VALID' : 'INVALID' };
this.store.dispatch(new UpdateStatus(status));
});
this.statusChangeEmitted$.pipe(distinctUntilChanged()).subscribe(errors => this.updateStatus(errors));

this.store.select(fromWizard.getWizardIndex).subscribe(i => this.currentPage = i);
}

resetSelectedType(changes: any, schema: any, definition: any): { changes: any, definition: any } {
const type = changes.value['@type'];
if (type && type !== definition.type) {
const newDefinition = MetadataProviderTypes.find(def => def.type === type);
if (newDefinition) {
this.store.dispatch(new SetDefinition({
...MetadataProviderWizard,
...newDefinition,
steps: [
...MetadataProviderWizard.steps,
...newDefinition.steps
]
}));
changes = { value: pick(Object.keys(schema.properties))(changes.value) };
}
}
return { changes: changes.value, definition };
}

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

ngOnDestroy() {
this.valueChangeSubject.complete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ export class ProviderWizardComponent implements OnDestroy {
}

next(): void {
if (this.nextStep) {
this.store.dispatch(new SetIndex(this.nextStep.id));
}
this.store.dispatch(new SetIndex(this.nextStep.id));
}

previous(): void {
Expand Down
6 changes: 5 additions & 1 deletion ui/src/app/metadata/provider/model/provider.form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { Metadata } from '../../domain/domain.type';

export const MetadataProviderWizard: Wizard<MetadataProvider> = {
label: 'MetadataProvider',
type: '@MetadataProvider',
type: 'MetadataProvider',
translate: {
parser: changes => changes,
formatter: model => model
},
steps: [
{
id: 'new',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const snapshot: fromProvider.CollectionState = {
loaded: false
};

describe('Provider Reducer', () => {
describe('Provider Collection Reducer', () => {
describe('undefined action', () => {
it('should return the default state', () => {
const result = reducer(snapshot, {} as any);
Expand Down
18 changes: 18 additions & 0 deletions ui/src/app/metadata/provider/reducer/editor.reducer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { reducer, initialState as snapshot } from './editor.reducer';
import { EditorActionTypes, ClearEditor } from '../action/editor.action';

describe('Provider Editor Reducer', () => {
describe('undefined action', () => {
it('should return the default state', () => {
const result = reducer(snapshot, {} as any);

expect(result).toEqual(snapshot);
});
});

describe(`${EditorActionTypes.CLEAR}`, () => {
it('should reset to initial state', () => {
expect(reducer(snapshot, new ClearEditor())).toEqual(snapshot);
});
});
});
18 changes: 18 additions & 0 deletions ui/src/app/metadata/provider/reducer/entity.reducer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { reducer, initialState as snapshot } from './entity.reducer';
import { EntityActionTypes, ClearProvider } from '../action/entity.action';

describe('Provider Editor Reducer', () => {
describe('undefined action', () => {
it('should return the default state', () => {
const result = reducer(snapshot, {} as any);

expect(result).toEqual(snapshot);
});
});

describe(`${EntityActionTypes.CLEAR_PROVIDER}`, () => {
it('should reset to initial state', () => {
expect(reducer(snapshot, new ClearProvider())).toEqual(snapshot);
});
});
});
Loading

0 comments on commit 8759c88

Please sign in to comment.