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

Jest utilities for Styled Components #76

Closed
3 tasks done
arcticicestudio opened this issue Dec 13, 2018 · 0 comments
Closed
3 tasks done

Jest utilities for Styled Components #76

arcticicestudio opened this issue Dec 13, 2018 · 0 comments

Comments

@arcticicestudio
Copy link
Contributor

arcticicestudio commented Dec 13, 2018

Associated epics: #38
Associated issues: #39

This issue documents the integration of the jest-styled-components, a set of utilities for testing Styled Components with Jest that improves the snapshot testing experience and provides a brand new matcher to make expectations on the style rules.
This is the officially recommended library styled-components for Jest integration.

Instead of storing the generated class names in snapshots it allows to track the actual CSS rules and attributes for better comparison what changed and if the change is even really related to the affected component.

const Button = styled.button`
  color: red;
`
exports[`snapshot`] = `
<button
  className="sc-bdVaJa rOCEJ"
/>
`;

Before

- Snapshot
+ Received

 <button
-  className="sc-bdVaJa rOCEJ"
+  className="sc-bdVaJa hUzqNt"
 />

After

- Snapshot
+ Received

+.c0 {
+  color: green;
+}
+
 <button
-  className="sc-bdVaJa rOCEJ"
+  className="c0"
 />

Configuration

To enable this feature the package can simply being imported in the setup file defined in Jest's setupTestFrameworkScriptFile field implemented in #39.

import "jest-styled-components";

Custom Style Matcher

The custom toHaveStyleRule matcher is useful to test if a given rule is applied to a component. It will be added to the extended global except object when the main package file is being imported.

Tasks

  • Install the required packages:
  • Import the main package file into the defined Jest test framework script file (test/setup.js).
  • Update existing and implement missing tests for style-only components.
@arcticicestudio arcticicestudio added this to the 0.3.0 milestone Dec 13, 2018
@arcticicestudio arcticicestudio self-assigned this Dec 13, 2018
arcticicestudio added a commit that referenced this issue Dec 13, 2018
This commit integrates jest-styled-components (1), a set of utilities
for testing "Styled Components" (2) with Jest () that improves the
napshot testing experience and provides a brand new matcher to make
expectations on the style rules.
This is the officially recommended library styled-components for Jest
integration (3).

Instead of storing the generated class names in snapshots it allows to
track the actual CSS rules and attributes for better comparison what
changed and if the change is even really related to the affected
component.

> Configuration

To enable this features the package is simply imported in the test
framework setup file defined in Jest's `setupTestFrameworkScriptFile`
field (GH-39).

> Custom Style Matcher

The custom `toHaveStyleRule` matcher (4) is useful to test if a given
rule is applied to a component. It will be added to the extended global
`except` object when the main package file is being imported.

References:
  (1) https://github.com/styled-components/jest-styled-components
  (2) https://www.styled-components.com
  (3) https://jestjs.io
  (4) https://github.com/styled-components/jest-styled-components#tohavestylerule

Associated epics: GH-38
Associated issues: GH-39
GH-76
arcticicestudio added a commit that referenced this issue Dec 15, 2018
Instead of returning a simple object with the default `ReactComponent`
key the mock now returns an real `<svg>` React component.

GH-76
arcticicestudio added a commit that referenced this issue Dec 15, 2018
This improves the configuratibility and simplifies the testing.

GH-76
arcticicestudio added a commit that referenced this issue Dec 15, 2018
…ies-for-styled-components

Jest utilities for Styled Components
@arcticicestudio arcticicestudio removed their assignment Dec 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant