Skip to content

Commit

Permalink
Merge pull request #157 from ditrit/bugfix/no_error_empty_value
Browse files Browse the repository at this point in the history
Fix hasError when not required attribute has empty value
  • Loading branch information
Zorin95670 authored Feb 28, 2023
2 parents da91c9f + 36ffe83 commit bd74da7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 17 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fix the links width by multiplying it by the zoom scale.
- Fix `hasError` in `ComponentAttribute.js` to return false when the value of attribute is empty.

## [0.13.0] - 2023/02/09

Expand Down
4 changes: 2 additions & 2 deletions src/models/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ class Component extends FileInformation {
return !attribute
|| attribute.value === null
|| attribute.value === undefined
|| (attribute.type === 'Array' && attribute.value.length === 0)
|| (attribute.type === 'Object' && Object.keys(attribute.value).length === 0)
|| ((attribute.type === 'Array' || attribute.type === 'Object')
&& attribute.value.length === 0)
|| (attribute.type === 'String' && attribute.value.trim() === '');
});
}
Expand Down
8 changes: 4 additions & 4 deletions src/models/ComponentAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class ComponentAttribute {
return typeof this.value !== this.type.toLowerCase();
}

if (this.value === null || this.value === undefined) {
return false;
}

return this.__typeOfValueValidation()
|| this.__ruleValueValidation()
|| this.__ruleMinAndMaxValidation()
Expand All @@ -95,10 +99,6 @@ class ComponentAttribute {
return !Array.isArray(this.value);
}

if (type === 'object') {
return Object.prototype.toString.call(this.value) !== '[object Object]';
}

// eslint-disable-next-line valid-typeof
return typeof this.value !== type;
}
Expand Down
60 changes: 49 additions & 11 deletions tests/unit/models/Component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,29 @@ describe('Test class: Component', () => {
});

describe('Test method: hasError', () => {
describe('Test not required attribute with no value', () => {
const attributeDefinition = new ComponentAttributeDefinition({
name: 'attribute-string',
type: 'String',
});

it('Should return false if the attribute is not required', () => {
const attribute = new ComponentAttribute({
name: 'attribute-string',
type: 'String',
definition: attributeDefinition,
});

expect(new Component({
attributes: [attribute],
definition: {
definedAttributes: [attributeDefinition],
},
}).hasError())
.toEqual(false);
});
});

describe('Test required attribute', () => {
const attributeDefinition = new ComponentAttributeDefinition({
name: 'attribute-string',
Expand Down Expand Up @@ -629,11 +652,26 @@ describe('Test class: Component', () => {
}).hasError()).toEqual(true);
});

it('Should return true if the value is empty', () => {
const attribute = new ComponentAttribute({
name: 'attribute-string',
type: 'String',
definition: attributeDefinition,
});

expect(new Component({
attributes: [attribute],
definition: {
definedAttributes: [attributeDefinition],
},
}).hasError()).toEqual(true);
});

it('Should not fail on Object type attribute', () => {
const attribute = new ComponentAttribute({
name: 'attribute-object',
value: {},
type: 'Object',
type: 'Array',
});

let error = null;
Expand Down Expand Up @@ -1051,8 +1089,8 @@ describe('Test class: Component', () => {
it('Should return false if value type is an object', () => {
const attribute = new ComponentAttribute({
name: 'attribute-object',
value: {},
type: 'Object',
value: [{}],
type: 'Array',
definition: attributeDefinition,
});

Expand All @@ -1068,7 +1106,7 @@ describe('Test class: Component', () => {
const attribute = new ComponentAttribute({
name: 'attribute-object',
value: 'object',
type: 'Object',
type: 'Array',
definition: attributeDefinition,
});

Expand All @@ -1081,8 +1119,8 @@ describe('Test class: Component', () => {
});

it('Should return false if the "values" rule includes the attribute\'s value', () => {
const value1 = { test: 'test1' };
const value2 = { test: 'test2' };
const value1 = [{ test: 'test1' }];
const value2 = [{ test: 'test2' }];
const attributeDefinitionValue = new ComponentAttributeDefinition({
name: 'attribute-object',
type: 'Object',
Expand All @@ -1092,7 +1130,7 @@ describe('Test class: Component', () => {
const attribute = new ComponentAttribute({
name: 'attribute-object',
value: value2,
type: 'Object',
type: 'Array',
definition: attributeDefinitionValue,
});

Expand All @@ -1107,8 +1145,8 @@ describe('Test class: Component', () => {
it(
'Should return true if the "values" rule doesn\'t include the attribute\'s value',
() => {
const value1 = { test: 'test1' };
const value2 = { test: 'test2' };
const value1 = [{ test: 'test1' }];
const value2 = [{ test: 'test2' }];
const attributeDefinitionValue = new ComponentAttributeDefinition({
name: 'attribute-object',
type: 'Object',
Expand All @@ -1117,8 +1155,8 @@ describe('Test class: Component', () => {

const attribute = new ComponentAttribute({
name: 'attribute-object',
value: { test: 'test3' },
type: 'Object',
value: [{}, {}],
type: 'Array',
definition: attributeDefinitionValue,
});

Expand Down

0 comments on commit bd74da7

Please sign in to comment.