Browse Source

Tests for schema dtos.

pull/282/head
Sebastian Stehle 8 years ago
parent
commit
98eb788d00
  1. 3
      src/Squidex/app/features/content/pages/contents/contents-page.component.html
  2. 15
      src/Squidex/app/features/content/pages/contents/contents-page.component.ts
  3. 2
      src/Squidex/app/features/content/shared/content-item.component.html
  4. 15
      src/Squidex/app/features/content/shared/content-item.component.ts
  5. 3
      src/Squidex/app/features/content/shared/contents-selector.component.html
  6. 4
      src/Squidex/app/features/content/shared/contents-selector.component.ts
  7. 6
      src/Squidex/app/features/content/shared/references-editor.component.html
  8. 16
      src/Squidex/app/features/content/shared/references-editor.component.ts
  9. 100
      src/Squidex/app/shared/services/schemas.fields.spec.ts
  10. 184
      src/Squidex/app/shared/services/schemas.service.ts

3
src/Squidex/app/features/content/pages/contents/contents-page.component.html

@ -37,7 +37,7 @@
<th class="cell-select" *ngIf="!isReadOnly">
<input type="checkbox" class="form-control" [ngModel]="isAllSelected" (ngModelChange)="selectAll($event)" />
</th>
<th class="cell-auto" *ngFor="let field of contentFields">
<th class="cell-auto" *ngFor="let field of schema.listFields">
<span class="field">{{field.displayName}}</span>
</th>
<th class="cell-time">
@ -89,7 +89,6 @@
<tr [sqxContent]="content" [routerLink]="[content.id]" routerLinkActive="active"
[language]="languageSelected"
[schema]="schema"
[schemaFields]="contentFields"
[selected]="isItemSelected(content)"
(selectedChange)="selectItem(content, $event)"
(unpublishing)="unpublishContent(content)"

15
src/Squidex/app/features/content/pages/contents/contents-page.component.ts

@ -15,7 +15,6 @@ import {
ContentDto,
ContentsService,
DateTime,
FieldDto,
ImmutableArray,
ModalView,
Pager,
@ -40,7 +39,6 @@ export class ContentsPageComponent implements OnDestroy, OnInit {
public searchModal = new ModalView();
public contentItems: ImmutableArray<ContentDto>;
public contentFields: FieldDto[];
public contentsQuery = '';
public contentsPager = new Pager(0);
@ -316,19 +314,6 @@ export class ContentsPageComponent implements OnDestroy, OnInit {
this.selectedItems = {};
this.updateSelectionSummary();
this.loadFields();
}
private loadFields() {
this.contentFields = this.schema.fields.filter(x => x.properties.isListField);
if (this.contentFields.length === 0 && this.schema.fields.length > 0) {
this.contentFields = [this.schema.fields[0]];
}
if (this.contentFields.length === 0) {
this.contentFields = [<any>{}];
}
}
public confirmStatusChange() {

2
src/Squidex/app/features/content/shared/content-item.component.html

@ -5,7 +5,7 @@
(click)="$event.stopPropagation()" />
</td>
<td class="cell-auto" *ngFor="let field of schemaFields; let i = index" (click)="shouldStop($event)">
<td class="cell-auto" *ngFor="let field of schema.listFields; let i = index" (click)="shouldStop($event)">
<div *ngIf="field.properties.inlineEditable && !isReadOnly" [formGroup]="form" (click)="$event.stopPropagation()">
<div [ngSwitch]="field.properties.fieldType">
<div *ngSwitchCase="'Number'">

15
src/Squidex/app/features/content/shared/content-item.component.ts

@ -17,7 +17,7 @@ import {
FieldDto,
fieldInvariant,
ModalView,
SchemaDto,
SchemaDetailsDto,
Types,
Versioned
} from '@app/shared';
@ -64,10 +64,7 @@ export class ContentItemComponent implements OnInit, OnChanges {
public language: AppLanguageDto;
@Input()
public schemaFields: FieldDto[];
@Input()
public schema: SchemaDto;
public schema: SchemaDetailsDto;
@Input()
public isReadOnly = false;
@ -95,7 +92,7 @@ export class ContentItemComponent implements OnInit, OnChanges {
}
public ngOnInit() {
for (let field of this.schemaFields) {
for (let field of this.schema.listFields) {
if (field.properties['inlineEditable']) {
this.form.setControl(field.name, new FormControl(undefined, field.createValidators(this.language.isOptional)));
}
@ -119,7 +116,7 @@ export class ContentItemComponent implements OnInit, OnChanges {
const request = {};
for (let field of this.schemaFields) {
for (let field of this.schema.listFields) {
if (field.properties['inlineEditable']) {
const value = this.form.controls[field.name].value;
@ -152,8 +149,7 @@ export class ContentItemComponent implements OnInit, OnChanges {
private updateValues() {
this.values = [];
if (this.schemaFields) {
for (let field of this.schemaFields) {
for (let field of this.schema.listFields) {
const value = this.getRawValue(field);
if (Types.isUndefined(value)) {
@ -171,7 +167,6 @@ export class ContentItemComponent implements OnInit, OnChanges {
}
}
}
}
private getRawValue(field: FieldDto): any {
const contentField = this.content.data[field.name];

3
src/Squidex/app/features/content/shared/contents-selector.component.html

@ -21,7 +21,7 @@
<th class="cell-select">
<input type="checkbox" class="form-control" [ngModel]="isAllSelected" (ngModelChange)="selectAll($event)" />
</th>
<th class="cell-auto" *ngFor="let field of schemaFields">
<th class="cell-auto" *ngFor="let field of schema.listFields">
<span class="field">{{field.displayName}}</span>
</th>
<th class="cell-time">
@ -44,7 +44,6 @@
[selected]="isItemSelected(content)"
(selectedChange)="onContentSelected(content)"
[language]="languageSelected"
[schemaFields]="schemaFields"
[schema]="schema"
isReadOnly="true"></tr>
<tr class="spacer"></tr>

4
src/Squidex/app/features/content/shared/contents-selector.component.ts

@ -12,7 +12,6 @@ import {
ContentDto,
ContentsService,
DialogService,
FieldDto,
ImmutableArray,
ModalView,
Pager,
@ -32,9 +31,6 @@ export class ContentsSelectorComponent implements OnInit {
@Input()
public schema: SchemaDetailsDto;
@Input()
public schemaFields: FieldDto[];
@Output()
public selected = new EventEmitter<ContentDto[]>();

6
src/Squidex/app/features/content/shared/references-editor.component.html

@ -15,7 +15,6 @@
<tr [sqxContent]="content" dnd-sortable [sortableIndex]="i" (sqxSorted)="onContentsSorted($event)"
[language]="language"
[schema]="schema"
[schemaFields]="schemaFields"
(deleting)="onContentRemoving(content)"
isReadOnly="true"
isReference="true"></tr>
@ -26,10 +25,7 @@
</div>
<ng-container *sqxModalView="isModalVisibible;onRoot:true;closeAuto:false">
<sqx-contents-selector
[language]="language"
[schema]="schema"
[schemaFields]="schemaFields"
<sqx-contents-selector [language]="language" [schema]="schema"
(selected)="onContentsSelected($event)">
</sqx-contents-selector>
</ng-container>

16
src/Squidex/app/features/content/shared/references-editor.component.ts

@ -15,7 +15,6 @@ import {
AppsState,
ContentDto,
ContentsService,
FieldDto,
ImmutableArray,
MathHelper,
SchemaDetailsDto,
@ -48,7 +47,6 @@ export class ReferencesEditorComponent implements ControlValueAccessor, OnInit {
public isModalVisibible = false;
public schema: SchemaDetailsDto;
public schemaFields: FieldDto[];
public contentItems = ImmutableArray.empty<ContentDto>();
@ -70,8 +68,6 @@ export class ReferencesEditorComponent implements ControlValueAccessor, OnInit {
this.schemasService.getSchema(this.appsState.appName, this.schemaId)
.subscribe(dto => {
this.schema = dto;
this.loadFields();
}, error => {
this.isInvalidSchema = true;
});
@ -148,16 +144,4 @@ export class ReferencesEditorComponent implements ControlValueAccessor, OnInit {
this.callTouched();
this.callChange(ids);
}
private loadFields() {
this.schemaFields = this.schema.fields.filter(x => x.properties.isListField);
if (this.schemaFields.length === 0 && this.schema.fields.length > 0) {
this.schemaFields = [this.schema.fields[0]];
}
if (this.schemaFields.length === 0) {
this.schemaFields = [<any>{}];
}
}
}

100
src/Squidex/app/shared/services/schemas.fields.spec.ts

@ -17,10 +17,102 @@ import {
JsonFieldPropertiesDto,
NumberFieldPropertiesDto,
ReferencesFieldPropertiesDto,
SchemaDetailsDto,
SchemaPropertiesDto,
StringFieldPropertiesDto,
TagsFieldPropertiesDto
} from './../';
describe('SchemaDetailsDto', () => {
it('should return label as display name', () => {
const schema = createSchema(new SchemaPropertiesDto('Label'), 1, []);
expect(schema.displayName).toBe('Label');
});
it('should return name as display name if label is undefined', () => {
const schema = createSchema(new SchemaPropertiesDto(undefined), 1, []);
expect(schema.displayName).toBe('schema1');
});
it('should return name as display name label is empty', () => {
const schema = createSchema(new SchemaPropertiesDto(''), 1, []);
expect(schema.displayName).toBe('schema1');
});
it('should return configured fields as list fields if no schema field are declared', () => {
const field1 = createField(new AssetsFieldPropertiesDto(null, null, null, false, true), 1);
const field2 = createField(new AssetsFieldPropertiesDto(null, null, null, false, false), 2);
const field3 = createField(new AssetsFieldPropertiesDto(null, null, null, false, true), 3);
const schema = createSchema(new SchemaPropertiesDto(''), 1, [field1, field2, field3]);
expect(schema.listFields).toEqual([field1, field3]);
});
it('should return first fields as list fields if no schema field is declared', () => {
const field1 = createField(new AssetsFieldPropertiesDto(null, null, null, false, false), 1);
const field2 = createField(new AssetsFieldPropertiesDto(null, null, null, false, false), 2);
const field3 = createField(new AssetsFieldPropertiesDto(null, null, null, false, false), 3);
const schema = createSchema(new SchemaPropertiesDto(''), 1, [field1, field2, field3]);
expect(schema.listFields).toEqual([field1]);
});
it('should return empty list fields if fields is empty', () => {
const schema = createSchema(new SchemaPropertiesDto(''), 1, []);
expect(schema.listFields).toEqual([{}]);
});
});
describe('FieldDto', () => {
it('should return label as display name', () => {
const field = createField(new AssetsFieldPropertiesDto('Label', null, null, true, false), 1);
expect(field.displayName).toBe('Label');
});
it('should return name as display name if label is null', () => {
const field = createField(new AssetsFieldPropertiesDto(null, null, null, true, false), 1);
expect(field.displayName).toBe('field1');
});
it('should return name as display name label is empty', () => {
const field = createField(new AssetsFieldPropertiesDto('', null, null, true, false), 1);
expect(field.displayName).toBe('field1');
});
it('should return placeholder as display placeholder', () => {
const field = createField(new AssetsFieldPropertiesDto(null, null, 'Placeholder', true, false), 1);
expect(field.displayPlaceholder).toBe('Placeholder');
});
it('should return empty as display placeholder if placeholder is null', () => {
const field = createField(new AssetsFieldPropertiesDto(null, null, null, true, false));
expect(field.displayPlaceholder).toBe('');
});
it('should return localizable if partitioning is language', () => {
const field = createField(new AssetsFieldPropertiesDto(null, null, null, true, false), 1, 'language');
expect(field.isLocalizable).toBeTruthy();
});
it('should not return localizable if partitioning is invarient', () => {
const field = createField(new AssetsFieldPropertiesDto(null, null, null, true, false), 1, 'invariant');
expect(field.isLocalizable).toBeFalsy();
});
});
describe('AssetsField', () => {
const field = createField(new AssetsFieldPropertiesDto(null, null, null, true, false, 1, 1));
@ -250,6 +342,10 @@ describe('StringField', () => {
});
});
function createField(properties: FieldPropertiesDto) {
return new FieldDto(1, 'field1', false, false, false, 'languages', properties);
function createSchema(properties: SchemaPropertiesDto, index = 1, fields: FieldDto[]) {
return new SchemaDetailsDto('id' + index, 'schema' + index, properties, true, null!, null!, null!, null!, null!, fields);
}
function createField(properties: FieldPropertiesDto, index = 1, partitioning = 'languages') {
return new FieldDto(index, 'field' + index, false, false, false, partitioning, properties);
}

184
src/Squidex/app/shared/services/schemas.service.ts

@ -124,31 +124,11 @@ export class SchemaDto {
this.created, now || DateTime.now(),
version);
}
public unpublish(user: string, version: Version, now?: DateTime): SchemaDto {
return new SchemaDto(
this.id,
this.name,
this.properties,
false,
this.createdBy, user,
this.created, now || DateTime.now(),
version);
}
public update(properties: SchemaPropertiesDto, user: string, version: Version, now?: DateTime): SchemaDto {
return new SchemaDto(
this.id,
this.name,
properties,
this.isPublished,
this.createdBy, user,
this.created, now || DateTime.now(),
version);
}
}
export class SchemaDetailsDto extends SchemaDto {
public readonly listFields: FieldDto[];
constructor(id: string, name: string, properties: SchemaPropertiesDto, isPublished: boolean, createdBy: string, lastModifiedBy: string, created: DateTime, lastModified: DateTime, version: Version,
public readonly fields: FieldDto[],
public readonly scriptQuery?: string,
@ -158,142 +138,16 @@ export class SchemaDetailsDto extends SchemaDto {
public readonly scriptChange?: string
) {
super(id, name, properties, isPublished, createdBy, lastModifiedBy, created, lastModified, version);
}
public publish(user: string, version: Version, now?: DateTime): SchemaDetailsDto {
return new SchemaDetailsDto(
this.id,
this.name,
this.properties,
true,
this.createdBy, user,
this.created, now || DateTime.now(),
version,
this.fields,
this.scriptQuery,
this.scriptCreate,
this.scriptUpdate,
this.scriptDelete,
this.scriptChange);
}
public unpublish(user: string, version: Version, now?: DateTime): SchemaDetailsDto {
return new SchemaDetailsDto(
this.id,
this.name,
this.properties,
false,
this.createdBy, user,
this.created, now || DateTime.now(),
version,
this.fields,
this.scriptQuery,
this.scriptCreate,
this.scriptUpdate,
this.scriptDelete,
this.scriptChange);
}
public configureScripts(scripts: UpdateSchemaScriptsDto, user: string, version: Version, now?: DateTime): SchemaDetailsDto {
return new SchemaDetailsDto(
this.id,
this.name,
this.properties,
this.isPublished,
this.createdBy, user,
this.created, now || DateTime.now(),
version,
this.fields,
scripts.scriptQuery,
scripts.scriptCreate,
scripts.scriptUpdate,
scripts.scriptDelete,
scripts.scriptChange);
}
public update(properties: SchemaPropertiesDto, user: string, version: Version, now?: DateTime): SchemaDetailsDto {
return new SchemaDetailsDto(
this.id,
this.name,
properties,
this.isPublished,
this.createdBy, user,
this.created, now || DateTime.now(),
version,
this.fields,
this.scriptQuery,
this.scriptCreate,
this.scriptUpdate,
this.scriptDelete,
this.scriptChange);
}
this.listFields = this.fields.filter(x => x.properties.isListField);
public addField(field: FieldDto, user: string, version: Version, now?: DateTime): SchemaDetailsDto {
return new SchemaDetailsDto(
this.id,
this.name,
this.properties,
this.isPublished,
this.createdBy, user,
this.created, now || DateTime.now(),
version,
[...this.fields, field],
this.scriptQuery,
this.scriptCreate,
this.scriptUpdate,
this.scriptDelete,
this.scriptChange);
if (this.listFields.length === 0 && this.fields.length > 0) {
this.listFields = [this.fields[0]];
}
public updateField(field: FieldDto, user: string, version: Version, now?: DateTime): SchemaDetailsDto {
return new SchemaDetailsDto(
this.id,
this.name,
this.properties,
this.isPublished,
this.createdBy, user,
this.created, now || DateTime.now(),
version,
this.fields.map(f => f.fieldId === field.fieldId ? field : f),
this.scriptQuery,
this.scriptCreate,
this.scriptUpdate,
this.scriptDelete,
this.scriptChange);
}
public replaceFields(fields: FieldDto[], user: string, version: Version, now?: DateTime): SchemaDetailsDto {
return new SchemaDetailsDto(
this.id,
this.name,
this.properties,
this.isPublished,
this.createdBy, user,
this.created, now || DateTime.now(),
version,
fields,
this.scriptQuery,
this.scriptCreate,
this.scriptUpdate,
this.scriptDelete,
this.scriptChange);
if (this.listFields.length === 0) {
this.listFields = [<any>{}];
}
public removeField(field: FieldDto, user: string, version: Version, now?: DateTime): SchemaDetailsDto {
return new SchemaDetailsDto(
this.id,
this.name,
this.properties,
this.isPublished,
this.createdBy, user,
this.created, now || DateTime.now(),
version,
this.fields.filter(f => f.fieldId !== field.fieldId),
this.scriptQuery,
this.scriptCreate,
this.scriptUpdate,
this.scriptDelete,
this.scriptChange);
}
}
@ -314,30 +168,6 @@ export class FieldDto {
) {
}
public lock(): FieldDto {
return new FieldDto(this.fieldId, this.name, true, this.isHidden, this.isDisabled, this.partitioning, this.properties);
}
public show(): FieldDto {
return new FieldDto(this.fieldId, this.name, this.isLocked, false, this.isDisabled, this.partitioning, this.properties);
}
public hide(): FieldDto {
return new FieldDto(this.fieldId, this.name, this.isLocked, true, this.isDisabled, this.partitioning, this.properties);
}
public enable(): FieldDto {
return new FieldDto(this.fieldId, this.name, this.isLocked, this.isHidden, false, this.partitioning, this.properties);
}
public disable(): FieldDto {
return new FieldDto(this.fieldId, this.name, this.isLocked, this.isHidden, true, this.partitioning, this.properties);
}
public update(properties: FieldPropertiesDto): FieldDto {
return new FieldDto(this.fieldId, this.name, this.isLocked, this.isHidden, this.isDisabled, this.partitioning, properties);
}
public formatValue(value: any): string {
return this.properties.formatValue(value);
}

Loading…
Cancel
Save