You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CONTRIBUTING.md
+44-1Lines changed: 44 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ Feel free to make changes wherever you see bit but please be mindful that removi
14
14
15
15
Check the existing props still work by checking against the story in the components `.stories.mdx` file. Each prop usually means the component will behave in a different way so please check the component still does what it's supposed to do when you're altering prop _values_ or code.
16
16
17
-
## To add a new Embed
17
+
## To add a new component
18
18
19
19
To add a new component there are several locations the new component needs to go, the rough outline is as follows:
20
20
@@ -38,3 +38,46 @@ The team are on hand should you require any assistance of have any questions at
38
38
- Paul Scanlon | [@pauliescanlon](https://twitter.com/PaulieScanlon)
39
39
- Scott Spence | [@spences10](https://twitter.com/spences10)
Each component will at some point require at least one test to check it renders correctly.
45
+
Each component in `mdx-embed/src/components/` is wrapped in what we call the **GeneralObserver** This component is responsible for ensuring the 3rd part embed script isn't invoked until the component has entered the viewport. This is particularly important for Lighthouse scores and page load speeds. If a blog post page for example has 10 or 15 embed codes, Twitter, CodePend, YouTube etc on the page we don't want to load all that 3rd party JavaScript until the reader has scrolled down to where the component is used. When it scrolls in to view **GeneralObserver** which is an [Intersection Observer](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) triggers a React state change which causes the children to be rendered in the DOM
46
+
47
+
This presents an issue when we come to test the components because we need to fake the "scrolled in to view" action which allows the children of **GeneralObserver** to render.
48
+
49
+
To _mock_ this is a test we've globally exposed a method called `triggerGeneralObserver()` and since we're using Testing Library this state change must be wrapped in an `act`.
50
+
51
+
With that said we require each `test(() => {})` to contain an `act()` and a call to `triggerGeneralObserver()`
52
+
53
+
That looks like something like this: the `(window as any)` is a TypeScript thing... Paul is working on it.
The other important part of the test is the use of `getByTestId` this is a special data attribute we use from Testing Library called `data-testid` and will need to be added to components with _new_ tests.
71
+
72
+
In the `CodePen` example that looks a bit like this:
0 commit comments