Skip to content

Commit

Permalink
LPS-101618 Only set disabled if a boolean is passed
Browse files Browse the repository at this point in the history
In this function we set the disabled attribute on a collection of
`Node`s, which should be a boolean value according to
https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled
but up to now this function would accept any value
  • Loading branch information
Julien Castelain committed Apr 30, 2020
1 parent 9e8c412 commit d87f904
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Expand Up @@ -30,8 +30,14 @@ export default function toggleDisabled(nodes, state) {
else if (nodes.nodeType === 1) {
nodes = [nodes];
}

nodes.forEach((node) => {
if (typeof state !== 'boolean') {
return;
}

node.disabled = state;

if (state) {
node.classList.add('disabled');
}
Expand Down
Expand Up @@ -35,8 +35,18 @@ describe('Liferay.Uitl.toggleDisabled', () => {
}
});

it('sets the `disabled` attribute on a collection nodes according to the `state` argument', () => {
it("doesn't set the `disabled` attribute on a collection of nodes if the `state` argument is not a boolean", () => {
toggleDisabled('button.test-button', 'disabled');

const nodes = document.querySelectorAll('button.test-button');
nodes.forEach((node) => {
expect(node.disabled).toEqual(false);
});
});

it('sets the `disabled` attribute on a collection of nodes if the `state` argument is a boolean', () => {
toggleDisabled('button.test-button', true);

const nodes = document.querySelectorAll('button.test-button');
nodes.forEach((node) => {
expect(node.disabled).toEqual(true);
Expand Down

0 comments on commit d87f904

Please sign in to comment.