myrentokil-components-library 1.0.64 → 1.0.82

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -7,21 +7,93 @@ This library can be installed using npm.
7
7
  ```
8
8
  npm install myrentokil-components-library
9
9
  ```
10
-
10
+
11
11
  ## Available Scripts:
12
12
 
13
- ### Run tests
13
+ ### Run and update snapshot tests
14
+
14
15
  ```
15
16
  npm run test
17
+ npm run test:updateSnapshot
18
+ ```
19
+
20
+ ## Run lint
21
+
22
+ ```
23
+ npm run lint
16
24
  ```
17
25
 
18
26
  ### Run storybook locally
27
+
19
28
  This project uses Storybook. To see the components in the browser, run:
29
+
20
30
  ```
21
31
  npm run storybook
22
32
  ```
23
33
 
24
34
  ### Build storybook
35
+
25
36
  ```
26
37
  npm run build-storybook
27
38
  ```
39
+
40
+ # Testing React Components: Why Use React Testing Library and Snapshot Testing
41
+
42
+ ## Introduction
43
+
44
+ Testing is a crucial part of any software development process. It helps ensure that our code works as expected, catches bugs early, and provides a safety net for refactoring and future changes. In the
45
+ context of React applications, there are various testing libraries and approaches available. In this README, we'll discuss why we chose to use React Testing Library and Snapshot Testing for testing
46
+ our components. Additionally, we'll explain the need for `jest-styled-components` when working with Styled Components.
47
+
48
+ ## Why Use React Testing Library?
49
+
50
+ React Testing Library (RTL) is a popular testing utility for testing React components. Unlike other testing libraries that focus on testing implementation details, RTL emphasizes testing user
51
+ interactions and behavior. It follows the philosophy of "testing the component as a user would use it," making the tests more reliable and maintainable.
52
+
53
+ Key advantages of using React Testing Library:
54
+
55
+ 1. **User-Centric Testing:** RTL encourages testing components from the user's perspective, simulating real user interactions such as clicking buttons, typing, and navigating. This approach ensures
56
+ that the components are tested in a way that closely resembles how users will interact with the application.
57
+
58
+ 2. **Accessibility Testing:** RTL promotes accessibility testing by providing utilities to query components based on their accessibility roles and attributes. This helps ensure that our components are
59
+ usable by all users, including those who rely on assistive technologies.
60
+
61
+ 3. **Encourages Best Practices:** By focusing on user interactions, RTL encourages writing tests that are less coupled to the implementation details. This promotes better component design and
62
+ encourages developers to follow best practices when building components.
63
+
64
+ 4. **Wide Community Adoption:** React Testing Library is widely adopted in the React ecosystem. It has a large community and active development, which means that you can find plenty of resources,
65
+ tutorials, and community support.
66
+
67
+ ## Why Use Snapshot Testing?
68
+
69
+ Snapshot testing is a type of testing that captures the output (e.g., rendered UI) of a component and stores it as a "snapshot." On subsequent test runs, the output is compared to the stored snapshot
70
+ to check for any unintended changes.
71
+
72
+ Benefits of using Snapshot Testing:
73
+
74
+ 1. **Quick Feedback on UI Changes:** Snapshot testing provides a quick way to verify if any unintended changes were introduced to the UI. It can catch visual regressions or unexpected changes that
75
+ might not be immediately apparent during development.
76
+
77
+ 2. **Maintainable Test Suites:** Snapshots serve as documentation of how the UI should look for different scenarios. When the UI changes intentionally, you can update the snapshots, and when changes
78
+ occur unintentionally, you can investigate and address them appropriately.
79
+
80
+ 3. **Less Boilerplate:** Snapshot testing reduces the need for writing assertions manually, making test creation faster and less error-prone.
81
+
82
+ ## Why Include jest-styled-components?
83
+
84
+ When using Styled Components to style our React components, the class names generated by Styled Components include unique hashes. This ensures that the styles are scoped correctly and avoids
85
+ conflicts.
86
+
87
+ However, this uniqueness can pose a challenge when using Snapshot Testing. The class names in snapshots may differ each time tests are run, causing unnecessary snapshot changes.
88
+
89
+ To solve this issue, we include `jest-styled-components`, a library that provides a serializer for Jest. This serializer normalizes the class names generated by Styled Components during snapshot
90
+ testing. It ensures that the class names in snapshots remain consistent, regardless of the test run environment.
91
+
92
+ ## Useful Links
93
+
94
+ - [React Testing Library Documentation](https://testing-library.com/docs/react-testing-library/intro/)
95
+ - [Snapshot Testing in Jest](https://jestjs.io/docs/snapshot-testing)
96
+ - [jest-styled-components GitHub Repository](https://github.com/styled-components/jest-styled-components)
97
+
98
+ By using React Testing Library, Snapshot Testing, and `jest-styled-components`, we can create comprehensive and reliable tests for our React components while ensuring consistent styles in our
99
+ snapshots. These practices contribute to maintaining a robust and stable codebase, allowing us to confidently build and iterate on our React applications.