testing-library / dom-testing-library

🐙 Simple and complete DOM testing utilities that encourage good testing practices.
https://testing-library.com/dom
MIT License
3.26k stars 467 forks source link

Support datetime-local querying #1163

Closed DarkTrick closed 1 year ago

DarkTrick commented 2 years ago

Describe the feature you'd like:

Add support for querying what datetime-local fields are showing. Goal: Tests like this should be possible:

    test('mytest', () => {
        render (<input type="datetime-local" defaultValue={'2022-08-08T09:00'}/>)
        expect(screen.getByText("2022/08/08 09:00")).toBeTruthy();
    });

Describe alternatives you've considered:

Currently you can do:

    test('mytest', () => {
        render (<input data-testid="mytestid" type="datetime-local" defaultValue={'2022-08-08T09:00'}/>)

        expect((screen.getByTestId('mytestid') as any).value).toBe("2022/08/08 09:00"));
    });

Teachability, Documentation, Adoption, Migration Strategy:

As stated above:

    test('mytest', () => {
        render (<input type="datetime-local" defaultValue={'2022-08-08T09:00'}/>)
        expect(screen.getByText("2022/08/08 09:00")).toBeTruthy();
    });

Note however, that getByText is just an example. There might be better calling methods.

MatanBobi commented 1 year ago

Hi @DarkTrick, thanks for opening this one. Testing-library doesn't offer a way to query for every type of element, we aim to query elements by anything which isn't an implementation detail. The type of the input is an implementation detail. Since <input type="datetime-local"/> doesn't have an implicit role, in this case I'd suggest to add a label to that input and query the element by it's label and check if the value matches what you're expecting. I'm closing this one as this isn't something we'd like to support.

Thanks again :)