Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
andressscamacho opened this issue Jan 20, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@andressscamacho
Copy link

andressscamacho commented Jan 20, 2025

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

@andressscamacho andressscamacho added the bug Something isn't working label Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant