Skip to content

[prefer-to-have-style] support index accessing of style #264

@G-Rath

Description

@G-Rath

This was picked up in the smoke tests.

Currently prefer-to-have-style will error on indexed access of style, e.g.

expect(element.style[1]).toEqual('padding');

It wants you to replace the above with:

expect(element).toHaveStyle('padding');

What I'm not sure is if that by itself is valid as the examples for that matcher only show complete CSS.

Looking at our tests we have this case:

{
code: `expect(el.style).not.toContain("background-color")`,
errors,
output: `expect(el).not.toHaveStyle({backgroundColor: expect.anything()})`,
},

So then the rule should actually already handle this situation for us, we just need to implement support for index accessing - the logic should be something like:

if index access of style, attempt to determine the value being accessed from the matcher, and replace it with toHaveStyle({ <matcher>: expect.anything() });

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingreleased

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions