From 98eb788d00908cc1f632f53dba8815baf6d75d9d Mon Sep 17 00:00:00 2001 From: Sebastian Stehle Date: Sun, 15 Apr 2018 11:47:27 +0200 Subject: [PATCH] Tests for schema dtos. --- .../contents/contents-page.component.html | 3 +- .../pages/contents/contents-page.component.ts | 15 -- .../shared/content-item.component.html | 2 +- .../content/shared/content-item.component.ts | 35 ++-- .../shared/contents-selector.component.html | 3 +- .../shared/contents-selector.component.ts | 4 - .../shared/references-editor.component.html | 6 +- .../shared/references-editor.component.ts | 16 -- .../shared/services/schemas.fields.spec.ts | 100 +++++++++- .../app/shared/services/schemas.service.ts | 188 +----------------- 10 files changed, 126 insertions(+), 246 deletions(-) diff --git a/src/Squidex/app/features/content/pages/contents/contents-page.component.html b/src/Squidex/app/features/content/pages/contents/contents-page.component.html index 193498671..92225d4eb 100644 --- a/src/Squidex/app/features/content/pages/contents/contents-page.component.html +++ b/src/Squidex/app/features/content/pages/contents/contents-page.component.html @@ -37,7 +37,7 @@ - + {{field.displayName}} @@ -89,7 +89,6 @@ ; - 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 = [{}]; - } } public confirmStatusChange() { diff --git a/src/Squidex/app/features/content/shared/content-item.component.html b/src/Squidex/app/features/content/shared/content-item.component.html index 362a7ef72..947d71196 100644 --- a/src/Squidex/app/features/content/shared/content-item.component.html +++ b/src/Squidex/app/features/content/shared/content-item.component.html @@ -5,7 +5,7 @@ (click)="$event.stopPropagation()" /> - +
diff --git a/src/Squidex/app/features/content/shared/content-item.component.ts b/src/Squidex/app/features/content/shared/content-item.component.ts index 2b287ab97..c941403a2 100644 --- a/src/Squidex/app/features/content/shared/content-item.component.ts +++ b/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,22 +149,20 @@ export class ContentItemComponent implements OnInit, OnChanges { private updateValues() { this.values = []; - if (this.schemaFields) { - for (let field of this.schemaFields) { - const value = this.getRawValue(field); + for (let field of this.schema.listFields) { + const value = this.getRawValue(field); - if (Types.isUndefined(value)) { - this.values.push(''); - } else { - this.values.push(field.formatValue(value)); - } + if (Types.isUndefined(value)) { + this.values.push(''); + } else { + this.values.push(field.formatValue(value)); + } - if (this.form) { - const formControl = this.form.controls[field.name]; + if (this.form) { + const formControl = this.form.controls[field.name]; - if (formControl) { - formControl.setValue(value); - } + if (formControl) { + formControl.setValue(value); } } } diff --git a/src/Squidex/app/features/content/shared/contents-selector.component.html b/src/Squidex/app/features/content/shared/contents-selector.component.html index 0e319adb5..f904d8fb8 100644 --- a/src/Squidex/app/features/content/shared/contents-selector.component.html +++ b/src/Squidex/app/features/content/shared/contents-selector.component.html @@ -21,7 +21,7 @@ - + {{field.displayName}} @@ -44,7 +44,6 @@ [selected]="isItemSelected(content)" (selectedChange)="onContentSelected(content)" [language]="languageSelected" - [schemaFields]="schemaFields" [schema]="schema" isReadOnly="true"> diff --git a/src/Squidex/app/features/content/shared/contents-selector.component.ts b/src/Squidex/app/features/content/shared/contents-selector.component.ts index 8c73a0461..fd2e272d1 100644 --- a/src/Squidex/app/features/content/shared/contents-selector.component.ts +++ b/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(); diff --git a/src/Squidex/app/features/content/shared/references-editor.component.html b/src/Squidex/app/features/content/shared/references-editor.component.html index 93440aef4..a2ad6ff77 100644 --- a/src/Squidex/app/features/content/shared/references-editor.component.html +++ b/src/Squidex/app/features/content/shared/references-editor.component.html @@ -15,7 +15,6 @@ @@ -26,10 +25,7 @@
- \ No newline at end of file diff --git a/src/Squidex/app/features/content/shared/references-editor.component.ts b/src/Squidex/app/features/content/shared/references-editor.component.ts index f8bfd5f70..62fcc7dea 100644 --- a/src/Squidex/app/features/content/shared/references-editor.component.ts +++ b/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(); @@ -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 = [{}]; - } - } } \ No newline at end of file diff --git a/src/Squidex/app/shared/services/schemas.fields.spec.ts b/src/Squidex/app/shared/services/schemas.fields.spec.ts index 263db61a1..88078ba02 100644 --- a/src/Squidex/app/shared/services/schemas.fields.spec.ts +++ b/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); } \ No newline at end of file diff --git a/src/Squidex/app/shared/services/schemas.service.ts b/src/Squidex/app/shared/services/schemas.service.ts index c350a5d04..17c85b220 100644 --- a/src/Squidex/app/shared/services/schemas.service.ts +++ b/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); - } + this.listFields = this.fields.filter(x => x.properties.isListField); - 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); - } - - 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); - } - - 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.fields.length > 0) { + this.listFields = [this.fields[0]]; + } - 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); + if (this.listFields.length === 0) { + this.listFields = [{}]; + } } } @@ -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); }