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

Snapshot of style rule(s) #117

Open
WickyNilliams opened this issue Jan 26, 2018 · 9 comments · May be fixed by #216
Open

Snapshot of style rule(s) #117

WickyNilliams opened this issue Jan 26, 2018 · 9 comments · May be fixed by #216

Comments

@WickyNilliams
Copy link

It would be nice if we could snapshot style rules, instead of explicitly asserting on them in tests.

e.g. instead of:

expect(component).toHaveStyleRule("transition", "height 50ms ease-out, opacity 50ms ease-out 50ms, transform 50ms ease-out 50ms;");

we could do something like (API up for discussion):

expect(component.getStyleRule("transition")).toMatchSnapshot();
@WickyNilliams
Copy link
Author

WickyNilliams commented Jan 26, 2018

I guess I just want some way to snapshot styles, without caring about the rest of the DOM. I don't want to mount and snapshot the whole component because the snapshots might be huge. And toHaveStyleRule can get verbose, especially when sometimes there are modifiers like hover or child selectors at play. I usually have existing tests that exercise the DOM, i want separate tests for styles.

So maybe something like expect(component).toHaveStylesMatchingSnapshot()? Jest has methods like toThrowErrorMatchingSnapshot() so it's not unheard of to have other snapshot methods beyond the standard/basic one

@WickyNilliams WickyNilliams changed the title Snapshot of style rule Snapshot of style rule(s) Jan 26, 2018
@MicheleBertoli
Copy link
Member

Thank you very much, @WickyNilliams.
Would you be happy to submit a PR?

@iilei
Copy link

iilei commented Jun 6, 2018

I would love this feature too 👍

@vctormb
Copy link

vctormb commented Sep 22, 2018

Will it be released? It will be so cool!!

@joebourne
Copy link

I see it's been a while since any activity on this issue so I'll take a look at it and see if I can submit a PR soon.

@joebourne
Copy link

joebourne commented Nov 21, 2018

@MicheleBertoli I have an idea for a basic initial implementation and would appreciate your thoughts before I submit a PR.

Currently we have toHaveStyleRule(). This could be split into two functions getStyleRule() and toHaveStyleRule() (which would use getStyleRule()). We could then expose getStyleRule() in the library's public API and assert on that.

An example usage:

import { getStyleRule } from 'jest-styled-components';

describe('MyComponent', () => {
  it('should ...', () => {
    expect(getStyleRule(<MyComponent />, 'color')).toMatchSnapshot();
  })
})

In a follow-up PR, we could look at creating an entirely new assertion, toHaveStyleRuleMatchingSnapshot().

@MicheleBertoli
Copy link
Member

+1 getStyleRule(<MyComponent />, 'color') seems quite a nice API.

I have a couple of questions:

  • Should we let the users decide how to render their components?

For example:

const tree = renderer.create(<Button />).toJSON() // or shallow(<Button />), or mount(<Button />)
expect(getStyleRule(tree, 'color')).toMatchSnapshot();
  • What about the modifiers (e.g. media queries)?

Thank you very much for working on this, @joebourne.

@joebourne
Copy link

As I'll be abstracting existing logic, users will indeed be able to choose how to render components, and the modifiers will also be available as options can be passed as a third argument to getStyleRule.

No problem, I'll submit a PR this week.

@joebourne joebourne linked a pull request Nov 30, 2018 that will close this issue
@AbhiPrasad
Copy link

Is this still in the works? Would love to use it our projects!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants