Browse Source

Tests added

pull/388/head
Sebastian Stehle 7 years ago
parent
commit
1aa45536cc
  1. 106
      src/Squidex/app/shared/state/contents.forms.spec.ts
  2. 9
      src/Squidex/app/shared/state/contents.forms.ts
  3. 31
      tests/Squidex.Domain.Apps.Core.Tests/Operations/ExtractReferenceIds/ReferenceFormattingTests.cs

106
src/Squidex/app/shared/state/contents.forms.spec.ts

@ -16,7 +16,10 @@ import {
FieldPropertiesDto, FieldPropertiesDto,
FieldValidatorsFactory, FieldValidatorsFactory,
GeolocationFieldPropertiesDto, GeolocationFieldPropertiesDto,
getContentValue,
HtmlValue,
JsonFieldPropertiesDto, JsonFieldPropertiesDto,
LanguageDto,
NumberFieldPropertiesDto, NumberFieldPropertiesDto,
ReferencesFieldPropertiesDto, ReferencesFieldPropertiesDto,
RootFieldDto, RootFieldDto,
@ -25,7 +28,6 @@ import {
StringFieldPropertiesDto, StringFieldPropertiesDto,
TagsFieldPropertiesDto TagsFieldPropertiesDto
} from '@app/shared/internal'; } from '@app/shared/internal';
import { HtmlValue } from './contents.forms';
describe('SchemaDetailsDto', () => { describe('SchemaDetailsDto', () => {
it('should return label as display name', () => { it('should return label as display name', () => {
@ -314,7 +316,7 @@ describe('NumberField', () => {
}); });
it('should format to number', () => { 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', () => { it('should format to stars if html allowed', () => {
@ -344,7 +346,7 @@ describe('NumberField', () => {
it('should not format to stars if html not allowed', () => { it('should not format to stars if html not allowed', () => {
const field2 = createField(new NumberFieldPropertiesDto('Stars')); 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', () => { 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[]) { function createSchema(properties: SchemaPropertiesDto, index = 1, fields: RootFieldDto[]) {
return new SchemaDetailsDto({}, 'id' + index, 'schema' + index, '', properties, false, true, null!, null!, null!, null!, null!, fields); 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); return new RootFieldDto({}, index, 'field' + index, properties, partitioning);
} }

9
src/Squidex/app/shared/state/contents.forms.ts

@ -73,11 +73,12 @@ export function getContentValue(content: ContentDto, language: LanguageDto, fiel
fieldValue = reference[fieldInvariant]; fieldValue = reference[fieldInvariant];
} }
let value = ''; let value: string | undefined =
fieldValue ?
fieldValue[language.iso2Code] :
undefined;
if (fieldValue) { value = value || '- No Value -'
value = fieldValue[language.iso2Code] || '';
}
return { value, formatted: value }; return { value, formatted: value };
} }

31
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 data = CreateData();
var schema = var schema = CreateNoRefSchema();
new Schema("my-schema")
.AddString(1, "ref1", Partitioning.Invariant)
.AddString(2, "ref2", Partitioning.Invariant)
.AddString(3, "non-ref", Partitioning.Invariant);
var formatted = data.FormatReferences(schema, languages); var formatted = data.FormatReferences(schema, languages);
@ -63,6 +59,31 @@ namespace Squidex.Domain.Apps.Core.Operations.ExtractReferenceIds
Assert.Equal(expected, formatted); 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() private static NamedContentData CreateData()
{ {
return new NamedContentData() return new NamedContentData()

Loading…
Cancel
Save