Skip to content

Commit

Permalink
SHIBUI-1332 Implemented unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmathis committed Jul 25, 2019
1 parent d7d6fda commit 61ca58f
Show file tree
Hide file tree
Showing 21 changed files with 351 additions and 54 deletions.
35 changes: 35 additions & 0 deletions ui/src/app/core/reducer/configuration.reducer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { reducer } from './configuration.reducer';
import * as fromConfiguration from './configuration.reducer';
import * as actions from '../action/configuration.action';

describe('Configuration Reducer', () => {
const initialState: fromConfiguration.ConfigState = {
roles: []
};

describe('undefined action', () => {
it('should return the default state', () => {
const result = reducer(undefined, {} as any);
expect(result).toEqual(initialState);
});
});

describe('Role Load Request', () => {
it('should set fetching to true', () => {
const action = new actions.LoadRoleSuccess(['ADMIN']);
const result = reducer(initialState, action);
expect(result).toEqual(
{
...initialState,
roles: ['ADMIN']
}
);
});
});

describe('selector functions', () => {
it('should return the roles from state', () => {
expect(fromConfiguration.getRoles(initialState)).toEqual([]);
});
});
});
18 changes: 17 additions & 1 deletion ui/src/app/core/reducer/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ describe('Core index reducers', () => {
version: fromVersion.initialState as fromVersion.VersionState,
config: fromConfig.initialState as fromConfig.ConfigState
};

describe('getUserStateFn function', () => {
it('should return the user state', () => {
expect(fromIndex.getUserStateFn(state)).toEqual(state.user);
Expand All @@ -21,4 +20,21 @@ describe('Core index reducers', () => {
expect(fromIndex.getVersionStateFn(state)).toEqual(state.version);
});
});
describe('getConfigStateFn function', () => {
it('should return the config state', () => {
expect(fromIndex.getConfigStateFn(state)).toEqual(state.config);
});
});
describe('filterRolesFn', () => {
it('should return the roles that are not `non roles`', () => {
expect(fromIndex.filterRolesFn(['ROLE_ADMIN', 'ROLE_NONE'])).toEqual(['ROLE_ADMIN']);
});
});
describe('isUserAdminFn', () => {
it('should check if the provided user has the ROLE_ADMIN role', () => {
expect(fromIndex.isUserAdminFn({role: 'ROLE_ADMIN'})).toBe(true);
expect(fromIndex.isUserAdminFn({role: 'ROLE_USER'})).toBe(false);
expect(fromIndex.isUserAdminFn(null)).toBe(false);
});
});
});
2 changes: 1 addition & 1 deletion ui/src/app/core/reducer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const getVersionLoading = createSelector(getVersionState, fromVersion.get
export const getVersionError = createSelector(getVersionState, fromVersion.getVersionError);

export const filterRolesFn = (roles: string[]) => roles.filter(r => r !== 'ROLE_NONE');
export const isUserAdminFn = (user) => user ? user.role === 'ROLE_ADMIN' : null;
export const isUserAdminFn = (user) => user ? user.role === 'ROLE_ADMIN' : false;

export const getConfigState = createSelector(getCoreFeature, getConfigStateFn);
export const getRoles = createSelector(getConfigState, fromConfig.getRoles);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
container="body"
[ngStyle]="{'width': width}"
class="text-truncate"
popoverClass="popover-lg">
popoverClass="popover-lg popover-info">
{{ version[i][prop] }}
</div>
<div *ngIf="!version[i]" [ngStyle]="{'width': width}">
Expand Down Expand Up @@ -59,7 +59,7 @@
class="list-unstyled py-2 m-0"
[ngbPopover]="popContent"
triggers="mouseenter:mouseleave"
popoverClass="popover-lg"
popoverClass="popover-lg popover-info"
*ngIf="v && v.length > 0">
<li *ngFor="let item of v; odd as isOdd" class="text-truncate" [ngClass]="{'bg-light': isOdd, 'py-2': v.length > 1}">
<ng-container *ngIf="preview.observers.length > 0 && isUrl(item)">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
[editable]="false"
[configuration]="configuration"
[entity]="filter"
[definition]="definition"></metadata-configuration>
[definition]="definition"
(preview)="onPreview($event)"></metadata-configuration>
<button class="btn btn-link btn-sm" (click)="open = !open" [disabled]="isLast">
<i class="fa fa-chevron-up sr-hidden"></i>&nbsp;
<span translate="action.close">Close</span>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Component, Input, Output, EventEmitter, OnChanges, SimpleChanges } from '@angular/core';
import { Store } from '@ngrx/store';
import { MetadataFilter } from '../../domain/model';
import { MetadataConfigurationService } from '../service/configuration.service';
import { Wizard } from '../../../wizard/model';
import { MetadataConfiguration } from '../model/metadata-configuration';
import { PreviewEntity } from '../../domain/action/entity.action';
import { Metadata } from '../../domain/domain.type';
import * as fromRoot from '../../../app.reducer';

@Component({
selector: 'filter-configuration-list-item',
Expand All @@ -23,9 +26,17 @@ export class FilterConfigurationListItemComponent implements OnChanges {
definition: any;

constructor(
private configService: MetadataConfigurationService
private configService: MetadataConfigurationService,
private store: Store<fromRoot.State>
) {}

onPreview($event: { data: any, parent: Metadata }): void {
this.store.dispatch(new PreviewEntity({
id: $event.data,
entity: this.definition.getEntity($event.parent)
}));
}

ngOnChanges(changes: SimpleChanges): void {
if (changes.filter) {
this.definition = this.configService.getDefinition(this.filter['@type']);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Component, ViewChild } from '@angular/core';
import { TestBed, async, ComponentFixture } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

import { NgbPopoverModule } from '@ng-bootstrap/ng-bootstrap';
import { Property } from '../../domain/model/property';
import { MockI18nModule } from '../../../../testing/i18n.stub';
import { SCHEMA } from '../../../../testing/form-schema.stub';
import { getStepProperty } from '../../domain/utility/configuration';
import { FilterTargetPropertyComponent } from './filter-target-property.component';
import { PrimitivePropertyComponent } from './primitive-property.component';
import { ArrayPropertyComponentStub, PrimitivePropertyComponentStub } from '../../../../testing/property-component.stub';
import { AttributesService } from '../../domain/service/attributes.service';
import { of } from 'rxjs';

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

property: Property = getStepProperty(SCHEMA.properties.formatFilterTarget, {
formatFilterTargetType: 'ENTITY_ID',
value: [
'foo',
'bar',
'baz'
]
}, SCHEMA.definitions);
}

describe('Filter Target Property Component', () => {

let fixture: ComponentFixture<TestHostComponent>;
let instance: TestHostComponent;
let app: FilterTargetPropertyComponent;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
NgbPopoverModule,
MockI18nModule,
RouterTestingModule
],
declarations: [
FilterTargetPropertyComponent,
PrimitivePropertyComponentStub,
ArrayPropertyComponentStub,
TestHostComponent
],
providers: []
}).compileComponents();

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

it('should accept a property input', async(() => {
expect(app).toBeTruthy();
}));
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
</label>
</div>
</td>
<td *ngIf="i === 0"><translate-i18n key="label.current">Current</translate-i18n> (v{{ i + 1 }})</td>
<td *ngIf="i > 0">v{{ i + 1 }}</td>
<td>{{ version.date | date }}</td>
<td *ngIf="i === 0"><translate-i18n key="label.current">Current</translate-i18n> (v{{ history.length - i }})</td>
<td *ngIf="i > 0">v{{ history.length - (i) }}</td>
<td>{{ version.date | date:'medium' }}</td>
<td>{{ version.creator }}</td>
<td>
<button class="btn btn-link" (click)="restore.emit(version)">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ <h2 class="title h4 m-0 flex-grow-1">
*ngIf="section"
[property]="section"
[columns]="configuration.dates.length"
(preview)="preview.emit($event)">
(preview)="onPreview($event)">
</object-property>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { Component, ChangeDetectionStrategy, Input, Output, EventEmitter } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { MetadataConfiguration } from '../model/metadata-configuration';
import { Property } from '../../domain/model/property';
import { Observable, of } from 'rxjs';
import { Metadata } from '../../domain/domain.type';
import { PreviewEntity } from '../../domain/action/entity.action';
import { ConfigurationState } from '../reducer';
import { Store } from '@ngrx/store';

@Component({
selector: 'metadata-configuration',
Expand All @@ -32,6 +27,10 @@ export class MetadataConfigurationComponent {
this.router.navigate(['../', 'edit', id], { relativeTo: this.activatedRoute.parent });
}

onPreview($event): void {
this.preview.emit($event);
}

get width(): string {
const columns = this.configuration.dates.length;
return `${Math.floor(100 / (columns + 1)) }%`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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 { NgbDropdownModule, NgbPopoverModule } from '@ng-bootstrap/ng-bootstrap';
import { NgbPopoverModule } from '@ng-bootstrap/ng-bootstrap';
import { Property } from '../../domain/model/property';
import { MockI18nModule } from '../../../../testing/i18n.stub';
import { ObjectPropertyComponent } from './object-property.component';
import { SCHEMA } from '../../../../testing/form-schema.stub';
import { getStepProperties, getStepProperty } from '../../domain/utility/configuration';
import { getStepProperty } from '../../domain/utility/configuration';
import { PrimitivePropertyComponent } from './primitive-property.component';
import { ArrayPropertyComponent } from './array-property.component';
import { FilterTargetPropertyComponent } from './filter-target-property.component';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { ConfigurationPropertyComponent } from './configuration-property.compone
templateUrl: './primitive-property.component.html',
styleUrls: []
})

export class PrimitivePropertyComponent extends ConfigurationPropertyComponent {
constructor() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ describe('Metadata Version History Component', () => {
];

const sorted = instance.sortVersionsByDate(versions);
expect(sorted[0].id).toEqual('bar');
expect(sorted[1].id).toEqual('baz');
expect(sorted[2].id).toEqual('foo');
expect(sorted[3].id).toEqual('baz2');
expect(sorted[0].id).toEqual('baz2');
expect(sorted[1].id).toEqual('foo');
expect(sorted[2].id).toEqual('baz');
expect(sorted[3].id).toEqual('bar');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ConfigurationState, getVersionCollection } from '../reducer';
import { MetadataVersion } from '../model/version';
import { CompareVersionRequest } from '../action/compare.action';
import { Router, ActivatedRoute } from '@angular/router';
import { map } from 'rxjs/operators';

@Component({
selector: 'metadata-history',
Expand All @@ -21,15 +22,16 @@ export class MetadataHistoryComponent {
private router: Router,
private route: ActivatedRoute
) {
this.history$ = this.store.select(getVersionCollection);
this.history$ = this.store.select(getVersionCollection)
.pipe(map(versions => this.sortVersionsByDate(versions)));
}

sortVersionsByDate(versions: MetadataVersion[]): MetadataVersion[] {
return versions.sort((a, b) => {
const aDate = new Date(a.date).getTime();
const bDate = new Date(b.date).getTime();
return aDate === bDate ? 0 : aDate < bDate ? -1 : 1;
});
}).reverse();
}

compareVersions(versions: MetadataVersion[]): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
</div>
<metadata-configuration
[configuration]="configuration$ | async"
(preview)="onPreview($event)"
id="configuration">
</metadata-configuration>
<div *ngIf="kind === 'provider'" id="filters">
Expand All @@ -49,6 +48,7 @@ <h2 class="title h4 m-0 ml-2 flex-grow-1">
(onUpdateOrderDown)="updateOrderDown($event)"
(onUpdateOrderUp)="updateOrderUp($event)"
(onRemove)="removeFilter($event)"
(preview)="onPreview($event)"
[filters]="filters$ | async"></filter-configuration-list>
</div>
<button class="btn btn-link" (click)="onScrollTo('header')">
Expand Down
Loading

0 comments on commit 61ca58f

Please sign in to comment.