Skip to content

NodeList gets the first element when you pass and empty array as index #1698

Open
@andressscamacho

Description

Describe the bug
When you use an empty array as an index of a NodeList it returns the first element instead of undefined.

To Reproduce
Steps to reproduce the behavior:
We have the following function:

const getActiveTabTrackId = () => {
    const bar = document.querySelector(".container");
    const active = bar && Object.keys(bar.childNodes).filter((key) => bar.childNodes[key].classList.contains('active');
    return bar && bar.childNodes[active] ? bar.childNodes[active].getAttribute('data-track-id') : null;
}

And we are testing it using:

test('returns null if no active tab are present', () => {
    document.body.innerHTML = '<div class="container">' +
    '<div class="tab" data-track-id="one"></div>' +
    '<div class="tab" data-track-id="two"></div>' +
    '</div>';

    expect(getActiveTabTrackId()).toBe(null);
})

When we run it, test fails because NodeList its returning the first element instead of undefined

Expected behavior
document.querySelector(".container").childNodes[[]] should return undefined instead of the first element.

Device:

  • OS: macos
  • Browser: happy-dom and jest
  • Version: 16.6.0

Additional context
We are using Jest and @happy-dom/jest-environment

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions