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

Use getAttribute instead of accessing id directly #287

Merged
merged 2 commits into from
Jul 18, 2023

Conversation

lgoeckener
Copy link
Contributor

If there is an input with name "id" in a form, element.id will target that input field instead of the id attribute of the form.

<form id="foo">
    <input type="hidden" name="id" value="1" />
</form>

Means, if in the current iteration of buildElementSelector, the element is the <form>-element,

element.id

will point to the input.

Therefore using element.getAttribute("id") will make sure, that the id-attribute of the current element is used.

@lgoeckener lgoeckener force-pushed the id-typecheck branch 3 times, most recently from 481ffc5 to 83c6a8b Compare June 29, 2023 09:59
@mshoho
Copy link
Member

mshoho commented Jul 17, 2023

@lgoeckener thanks! Could you please add a test for the case?

@lgoeckener
Copy link
Contributor Author

@mshoho I would love to! But I'm afraid I'm not quite sure in how to implement the test in the given environment.

I'm not familiar with the BroTest testing environment nor how I could create a scenario that would end up testing that particular branch.
Besides that I cannot create DOM elements since packages like JSDOM or react-testing-library are not installed.

If you give permission to install additional packages just for testing this branch (I would also need to export the buildElementSelector function), I could proceed doing so in a way that I'm familiar with.

@mshoho
Copy link
Member

mshoho commented Jul 18, 2023

We intentionally don't do tests in a virtualized environment because for Tabster they make no sense (and don't quite work because of the JSDOM's limitations). We use real Chromium.

It would be enough to go to Deloser.test.tsx and add the following there:

    it("should restore focus in <form> with named inputs", async () => {
        await new BroTest.BroTest(
            (
                <form {...getTabsterAttribute({ root: {}, deloser: {} })}>
                    <button>Button1</button>
                    <input name="id" />
                    <button>Button2</button>
                </form>
            )
        )
            .pressTab()
            .pressTab()
            .activeElement((el) => {
                expect(el?.attributes.name).toEqual("id");
            })
            .removeElement()
            .wait(300)
            .activeElement((el) => {
                expect(el?.textContent).toEqual("Button1");
            });
    });

To run the tests you can just npm run test (or npm run test deloser to limit the scope).

And we're moving BroTest to a separate package (to reuse it in another repository), it is work-in-progress, but some documentation is expected to eventually appear here: https://github.com/microsoft/testbro. But for now it would be enough to just copy-paste the test above.

@lgoeckener
Copy link
Contributor Author

@microsoft-github-policy-service agree

@mshoho mshoho merged commit 599c134 into microsoft:master Jul 18, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants