Skip to content

Commit

Permalink
SHIBUI-1270 Implemented new unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmathis committed Jul 16, 2019
1 parent 226e377 commit dd0ad86
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,14 @@ describe('Metadata History List Component', () => {
expect(instance.restore).toHaveBeenCalledWith(selected);
});
});

describe('toggleVersionSelected method', () => {
it('should add or remove the selected version', () => {
list.toggleVersionSelected(TestData.versions[0]);
fixture.detectChanges();
list.toggleVersionSelected(TestData.versions[0]);
fixture.detectChanges();
expect(list.selected.length).toBe(0);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { NgbDropdownModule } from '@ng-bootstrap/ng-bootstrap';
import { MetadataConfiguration } from '../model/metadata-configuration';
import { Property } from '../../domain/model/property';
import { MockI18nModule } from '../../../../testing/i18n.stub';
import { Router } from '@angular/router';

@Component({
selector: 'object-property',
Expand Down Expand Up @@ -37,6 +38,7 @@ describe('Metadata Configuration Component', () => {
let fixture: ComponentFixture<TestHostComponent>;
let instance: TestHostComponent;
let app: MetadataConfigurationComponent;
let router: Router;

beforeEach(async(() => {
TestBed.configureTestingModule({
Expand All @@ -55,10 +57,36 @@ describe('Metadata Configuration Component', () => {
fixture = TestBed.createComponent(TestHostComponent);
instance = fixture.componentInstance;
app = instance.componentUnderTest;
router = TestBed.get(Router);
fixture.detectChanges();
}));

it('should accept a configuration input', async(() => {
expect(app).toBeTruthy();
}));

describe('edit method', () => {
it('should call router.navigate', () => {
spyOn(router, 'navigate');
app.edit('foo');
expect(router.navigate).toHaveBeenCalled();
});
});

describe('width getter', () => {
it('should default to 100%', () => {
expect(app.width).toBe('100%');
});
it('should calculate the width based on dates', () => {
instance.configuration = {
...instance.configuration,
dates: [
new Date().toISOString(),
new Date().toISOString()
]
};
fixture.detectChanges();
expect(app.width).toBe('33%');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,8 @@ export class MetadataConfigurationComponent {
this.router.navigate(['../', 'edit', id], { relativeTo: this.activatedRoute.parent });
}

getItemType(items: Property): string {
return items.widget ? items.widget.id : 'default';
}

getKeys(schema): string[] {
return Object.keys(schema.properties);
}

get attributeList$(): Observable<{ key: string, label: string }[]> {
/*
if (this.property.widget && this.property.widget.hasOwnProperty('data')) {
return of(this.property.widget.data);
}
if (this.property.widget && this.property.widget.hasOwnProperty('dataUrl')) {
return this.attrService.query(this.property.widget.dataUrl);
}
*/
return of([]);
}

get width(): string {
return `${ Math.floor(100 / this.configuration.dates.length) }%`;
const columns = this.configuration.dates.length;
return `${Math.floor(100 / (columns + 1)) }%`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ export class ConfigurationComponent implements OnDestroy {
}
});

this.name$ = this.store
.select(fromReducer.getConfigurationModel)
.pipe(
filter(model => !!model),
map(model => model ? ('serviceProviderName' in model) ? model.serviceProviderName : model.name : false)
);
this.name$ = this.store.select(fromReducer.getConfigurationModelName);
}

ngOnDestroy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ import { ActivatedRouteStub } from '../../../../testing/activated-route.stub';
export const TestData = {
versions: [
{
versionNumber: 1,
saveDate: new Date(),
changedBy: 'admin',
actions: []
id: '1',
date: new Date().toDateString(),
creator: 'admin'
}
]
};
Expand All @@ -40,6 +39,7 @@ const MockHistoryService = {
describe('Metadata Version History Component', () => {
let fixture: ComponentFixture<MetadataHistoryComponent>;
let instance: MetadataHistoryComponent;
let router: Router;

beforeEach(() => {
TestBed.configureTestingModule({
Expand Down Expand Up @@ -68,10 +68,59 @@ describe('Metadata Version History Component', () => {

fixture = TestBed.createComponent(MetadataHistoryComponent);
instance = fixture.componentInstance;
router = TestBed.get(Router);
spyOn(router, 'navigate');
fixture.detectChanges();
});

it('should compile', () => {
expect(instance).toBeDefined();
});

describe('compare versions method', () => {
it('should call the router.navigate method', () => {
instance.compareVersions(TestData.versions);
expect(router.navigate).toHaveBeenCalled();
});
});

describe('sortVersionsByDate method', () => {
it('should sort the versions by their date', () => {
const nowTime = new Date().getTime();
const futureTime = nowTime + 10000;
const beforeTime = nowTime - 10000;
const nowDate = new Date(nowTime);
const futureDate = new Date(futureTime);
const beforeDate = new Date(beforeTime);

const versions = [
{
id: 'foo',
creator: 'bar',
date: nowDate.toISOString()
},
{
id: 'bar',
creator: 'baz',
date: beforeDate.toISOString()
},
{
id: 'baz',
creator: 'foo',
date: beforeDate.toISOString()
},
{
id: 'baz2',
creator: 'foo',
date: futureDate.toISOString()
}
];

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');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@ export class MetadataHistoryComponent {
this.history$ = this.store.select(getVersionCollection);
}

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;
});
}

compareVersions(versions: MetadataVersion[]): void {
const sorted = this.sortVersionsByDate(versions);
this.router.navigate(
['../', 'compare'],
{
queryParams: { versions: 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;
}).map(v => v.id) },
queryParams: { versions: sorted.map(v => v.id) },
relativeTo: this.route
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import {
getConfigurationModel,
getSelectedVersion,
getSelectedVersionNumber,
getSelectedIsCurrent
getSelectedIsCurrent,
getConfigurationModelEnabled
} from '../reducer';
import { MetadataConfiguration } from '../model/metadata-configuration';
import { MetadataVersion } from '../model/version';
import { map } from 'rxjs/operators';
import { Metadata } from '../../domain/domain.type';

@Component({
selector: 'metadata-options-page',
Expand All @@ -31,10 +33,8 @@ export class MetadataOptionsComponent {
constructor(
private store: Store<ConfigurationState>
) {
this.configuration$ = this.store.select(getConfigurationSections).pipe(map(config => config));
this.isEnabled$ = this.store.select(getConfigurationModel).pipe(
map(config => config ? ('serviceEnabled' in config) ? config.serviceEnabled : config.enabled : false)
);
this.configuration$ = this.store.select(getConfigurationSections);
this.isEnabled$ = this.store.select(getConfigurationModelEnabled);
this.version$ = this.store.select(getSelectedVersion);
this.versionNumber$ = this.store.select(getSelectedVersionNumber);
this.isCurrent$ = this.store.select(getSelectedIsCurrent);
Expand Down
53 changes: 53 additions & 0 deletions ui/src/app/metadata/configuration/reducer/compare.reducer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { reducer } from './compare.reducer';
import * as fromCompare from './compare.reducer';
import { MetadataResolver } from '../../domain/model';
import { SetMetadataVersions, ClearVersions } from '../action/compare.action';

describe('Comparison Reducer', () => {
const initialState: fromCompare.State = { ...fromCompare.initialState };
const models: MetadataResolver[] = [{
id: 'foo',
serviceProviderName: 'foo',
'@type': 'MetadataResolver',
createdBy: 'admin'
}];

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

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

describe('set versions action', () => {
it('should add the models to the state', () => {
const action = new SetMetadataVersions(models);
const result = reducer(initialState, action);
expect(result.models).toEqual(models);
expect(result.loaded).toBe(true);
});
});

describe('clear versions action', () => {
it('should remove the models from the state', () => {
const action = new ClearVersions();
const result = reducer(initialState, action);
expect(result.models).toEqual([]);
expect(result.loaded).toBe(false);
});
});

describe('selector functions', () => {
describe('getModel', () => {
it('should retrieve the model from state', () => {
expect(fromCompare.getVersionModels({ ...initialState, models })).toBe(models);
});
});
describe('getVersionModelsLoaded', () => {
it('should retrieve the loaded state', () => {
expect(fromCompare.getVersionModelsLoaded({ ...initialState, loaded: true })).toBe(true);
});
});
});
});
23 changes: 22 additions & 1 deletion ui/src/app/metadata/configuration/reducer/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { getConfigurationSectionsFn } from './index';
import {
getConfigurationSectionsFn,
getConfigurationModelNameFn,
getConfigurationModelEnabledFn
} from './index';
import { SCHEMA as schema } from '../../../../testing/form-schema.stub';
import { MetadataSourceEditor } from '../../domain/model/wizards/metadata-source-editor';
import { Metadata } from '../../domain/domain.type';

describe('Configuration Reducer', () => {
const model = {
Expand All @@ -16,4 +21,20 @@ describe('Configuration Reducer', () => {
expect(config.sections).toBeDefined();
});
});

describe('getConfigurationModelNameFn function', () => {
it('should return the name attribute', () => {
expect(getConfigurationModelNameFn({ serviceProviderName: 'foo' } as Metadata)).toBe('foo');
expect(getConfigurationModelNameFn({ name: 'bar' } as Metadata)).toBe('bar');
expect(getConfigurationModelNameFn(null)).toBe(false);
});
});

describe('getConfigurationModelEnabledFn function', () => {
it('should return the name attribute', () => {
expect(getConfigurationModelEnabledFn({ serviceEnabled: true } as Metadata)).toBe(true);
expect(getConfigurationModelEnabledFn({ enabled: true } as Metadata)).toBe(true);
expect(getConfigurationModelEnabledFn(null)).toBe(false);
});
});
});
10 changes: 10 additions & 0 deletions ui/src/app/metadata/configuration/reducer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getSplitSchema } from '../../../wizard/reducer';
import { getInCollectionFn } from '../../domain/domain.util';
import { MetadataConfiguration } from '../model/metadata-configuration';
import { Property } from '../../domain/model/property';
import { Metadata } from '../../domain/domain.type';

export interface ConfigurationState {
configuration: fromConfiguration.State;
Expand Down Expand Up @@ -97,6 +98,15 @@ export const getConfigurationSections = createSelector(
getConfigurationSectionsFn
);

export const getConfigurationModelEnabledFn =
(config: Metadata) => config ? ('serviceEnabled' in config) ? config.serviceEnabled : config.enabled : false;

export const getConfigurationModelNameFn =
(config: Metadata) => config ? ('serviceProviderName' in config) ? config.serviceProviderName : config.name : false;

export const getConfigurationModelEnabled = createSelector(getConfigurationModel, getConfigurationModelEnabledFn);
export const getConfigurationModelName = createSelector(getConfigurationModel, getConfigurationModelNameFn);

// Version History

export const getHistoryState = createSelector(getState, getHistoryStateFn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ export class NewResolverComponent implements OnDestroy {
this.actionsSubscription = this.route.queryParams.pipe(
takeUntil(this.ngUnsubscribe),
distinctUntilChanged(),
map(data => {
return new SelectDraftRequest(data.id);
})
map(data => new SelectDraftRequest(data.id))
).subscribe(this.store);
}
ngOnDestroy(): void {
Expand Down

0 comments on commit dd0ad86

Please sign in to comment.