Skip to content

Commit

Permalink
Fix fail when required attribute is Boolean or Number
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitriChauvel committed Feb 9, 2023
1 parent a2aa992 commit 0a2301e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/models/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,12 @@ class Component extends FileInformation {
.some((defAttribute) => {
const attribute = this.getAttributeByName(defAttribute.name);

return !attribute || attribute.value === null || (
attribute.type === 'Object' || attribute.type === 'Array'
? attribute.value.length === 0
: attribute.value.trim() === '');
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 === 'String' && attribute.value.trim() === '');
});
}

Expand Down
80 changes: 79 additions & 1 deletion tests/unit/models/Component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ describe('Test class: Component', () => {
it('Should not fail on Object type attribute', () => {
const attribute = new ComponentAttribute({
name: 'attribute-object',
value: [],
value: {},
type: 'Object',
});

Expand Down Expand Up @@ -680,6 +680,84 @@ describe('Test class: Component', () => {
}
expect(error).toBeNull();
});

it('Should not fail on Number type attribute', () => {
const attribute = new ComponentAttribute({
name: 'attribute-number',
value: 1,
type: 'Number',
});

let error = null;

try {
new Component({
attributes: [attribute],
definition: {
definedAttributes: [new ComponentAttributeDefinition({
name: 'attribute-number',
type: 'Number',
required: true,
})],
},
}).hasError();
} catch (e) {
error = e;
}
expect(error).toBeNull();
});

it('Should not fail on Boolean type attribute', () => {
const attribute = new ComponentAttribute({
name: 'attribute-boolean',
value: true,
type: 'Boolean',
});

let error = null;

try {
new Component({
attributes: [attribute],
definition: {
definedAttributes: [new ComponentAttributeDefinition({
name: 'attribute-boolean',
type: 'Boolean',
required: true,
})],
},
}).hasError();
} catch (e) {
error = e;
}
expect(error).toBeNull();
});

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

let error = null;

try {
new Component({
attributes: [attribute],
definition: {
definedAttributes: [new ComponentAttributeDefinition({
name: 'attribute-string',
type: 'String',
required: true,
})],
},
}).hasError();
} catch (e) {
error = e;
}
expect(error).toBeNull();
});
});

describe('Test String attribute', () => {
Expand Down

0 comments on commit 0a2301e

Please sign in to comment.