From 1aa45536cc5fefdf03b0d6398a2fc9b41a272743 Mon Sep 17 00:00:00 2001 From: Sebastian Stehle Date: Tue, 23 Jul 2019 17:36:21 +0200 Subject: [PATCH] Tests added --- .../app/shared/state/contents.forms.spec.ts | 106 +++++++++++++++++- .../app/shared/state/contents.forms.ts | 9 +- .../ReferenceFormattingTests.cs | 31 ++++- 3 files changed, 133 insertions(+), 13 deletions(-) diff --git a/src/Squidex/app/shared/state/contents.forms.spec.ts b/src/Squidex/app/shared/state/contents.forms.spec.ts index 05aa3a27a..717253904 100644 --- a/src/Squidex/app/shared/state/contents.forms.spec.ts +++ b/src/Squidex/app/shared/state/contents.forms.spec.ts @@ -16,7 +16,10 @@ import { FieldPropertiesDto, FieldValidatorsFactory, GeolocationFieldPropertiesDto, + getContentValue, + HtmlValue, JsonFieldPropertiesDto, + LanguageDto, NumberFieldPropertiesDto, ReferencesFieldPropertiesDto, RootFieldDto, @@ -25,7 +28,6 @@ import { StringFieldPropertiesDto, TagsFieldPropertiesDto } from '@app/shared/internal'; -import { HtmlValue } from './contents.forms'; describe('SchemaDetailsDto', () => { it('should return label as display name', () => { @@ -314,7 +316,7 @@ describe('NumberField', () => { }); it('should format to number', () => { - expect(FieldFormatter.format(field, 42)).toEqual(42); + expect(FieldFormatter.format(field, 42)).toEqual('42'); }); it('should format to stars if html allowed', () => { @@ -344,7 +346,7 @@ describe('NumberField', () => { it('should not format to stars if html not allowed', () => { const field2 = createField(new NumberFieldPropertiesDto('Stars')); - expect(FieldFormatter.format(field2, 3, false)).toEqual(3); + expect(FieldFormatter.format(field2, 3, false)).toEqual('3'); }); it('should return default value for default properties', () => { @@ -400,10 +402,106 @@ describe('StringField', () => { }); }); +describe('GetContentValue', () => { + const language = new LanguageDto('en', 'English'); + const fieldInvariant = createField(new NumberFieldPropertiesDto('Input'), 1, 'invariant'); + const fieldLocalized = createField(new NumberFieldPropertiesDto('Input')); + + it('should resolve invariant field from references value', () => { + const content: any = { + referenceData: { + field1: { + iv: { + en: '13' + } + } + } + }; + + const result = getContentValue(content, language, fieldInvariant); + + expect(result).toEqual({ value: '13', formatted: '13' }); + }); + + it('should resolve localized field from references value', () => { + const content: any = { + referenceData: { + field1: { + en: { + en: '13' + } + } + } + }; + + const result = getContentValue(content, language, fieldLocalized); + + expect(result).toEqual({ value: '13', formatted: '13' }); + }); + + it('should return default value if reference field not found', () => { + const content: any = { + referenceData: { + field1: { + iv: { + en: '13' + } + } + } + }; + + const result = getContentValue(content, language, fieldLocalized); + + expect(result).toEqual({ value: '- No Value -', formatted: '- No Value -' }); + }); + + it('should resolve invariant field', () => { + const content: any = { + dataDraft: { + field1: { + iv: 13 + } + } + }; + + const result = getContentValue(content, language, fieldInvariant); + + expect(result).toEqual({ value: 13, formatted: '13' }); + }); + + it('should resolve localized field', () => { + const content: any = { + dataDraft: { + field1: { + en: 13 + } + } + }; + + const result = getContentValue(content, language, fieldLocalized); + + expect(result).toEqual({ value: 13, formatted: '13' }); + }); + + it('should return default values if field not found', () => { + const content: any = { + dataDraft: { + other: { + en: 13 + } + } + }; + + const result = getContentValue(content, language, fieldLocalized); + + expect(result).toEqual({ value: undefined, formatted: '' }); + }); +}); + function createSchema(properties: SchemaPropertiesDto, index = 1, fields: RootFieldDto[]) { return new SchemaDetailsDto({}, 'id' + index, 'schema' + index, '', properties, false, true, null!, null!, null!, null!, null!, fields); } -function createField(properties: FieldPropertiesDto, index = 1, partitioning = 'languages') { +function createField(properties: FieldPropertiesDto, index = 1, partitioning = 'language') { return new RootFieldDto({}, index, 'field' + index, properties, partitioning); } \ No newline at end of file diff --git a/src/Squidex/app/shared/state/contents.forms.ts b/src/Squidex/app/shared/state/contents.forms.ts index 7ad42e608..3f923e760 100644 --- a/src/Squidex/app/shared/state/contents.forms.ts +++ b/src/Squidex/app/shared/state/contents.forms.ts @@ -73,11 +73,12 @@ export function getContentValue(content: ContentDto, language: LanguageDto, fiel fieldValue = reference[fieldInvariant]; } - let value = ''; + let value: string | undefined = + fieldValue ? + fieldValue[language.iso2Code] : + undefined; - if (fieldValue) { - value = fieldValue[language.iso2Code] || ''; - } + value = value || '- No Value -' return { value, formatted: value }; } diff --git a/tests/Squidex.Domain.Apps.Core.Tests/Operations/ExtractReferenceIds/ReferenceFormattingTests.cs b/tests/Squidex.Domain.Apps.Core.Tests/Operations/ExtractReferenceIds/ReferenceFormattingTests.cs index abfa9bc9f..bfe371b33 100644 --- a/tests/Squidex.Domain.Apps.Core.Tests/Operations/ExtractReferenceIds/ReferenceFormattingTests.cs +++ b/tests/Squidex.Domain.Apps.Core.Tests/Operations/ExtractReferenceIds/ReferenceFormattingTests.cs @@ -47,11 +47,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ExtractReferenceIds { var data = CreateData(); - var schema = - new Schema("my-schema") - .AddString(1, "ref1", Partitioning.Invariant) - .AddString(2, "ref2", Partitioning.Invariant) - .AddString(3, "non-ref", Partitioning.Invariant); + var schema = CreateNoRefSchema(); var formatted = data.FormatReferences(schema, languages); @@ -63,6 +59,31 @@ namespace Squidex.Domain.Apps.Core.Operations.ExtractReferenceIds Assert.Equal(expected, formatted); } + [Fact] + public void Should_return_default_value_if_no_value_found() + { + var data = new NamedContentData(); + + var schema = CreateNoRefSchema(); + + var formatted = data.FormatReferences(schema, languages); + + var expected = + JsonValue.Object() + .Add("en", string.Empty) + .Add("de", string.Empty); + + Assert.Equal(expected, formatted); + } + + private static Schema CreateNoRefSchema() + { + return new Schema("my-schema") + .AddString(1, "ref1", Partitioning.Invariant) + .AddString(2, "ref2", Partitioning.Invariant) + .AddString(3, "non-ref", Partitioning.Invariant); + } + private static NamedContentData CreateData() { return new NamedContentData()