Skip to content

Commit 9405790

Browse files
committed
refactor tests - chagne CONTRIBUTING.md
1 parent 824718f commit 9405790

File tree

7 files changed

+66
-26
lines changed

7 files changed

+66
-26
lines changed

CONTRIBUTING.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Feel free to make changes wherever you see bit but please be mindful that removi
1414

1515
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.
1616

17-
## To add a new Embed
17+
## To add a new component
1818

1919
To add a new component there are several locations the new component needs to go, the rough outline is as follows:
2020

@@ -38,3 +38,46 @@ The team are on hand should you require any assistance of have any questions at
3838
- Paul Scanlon | [@pauliescanlon](https://twitter.com/PaulieScanlon)
3939
- Scott Spence | [@spences10](https://twitter.com/spences10)
4040
- Rich Haines | [@studio_hungry](https://twitter.com/studio_hungry)
41+
42+
## Testing WIP
43+
44+
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.
54+
55+
```javascript
56+
describe('<CodePen />', () => {
57+
test('it renders the component', () => {
58+
render(<CodePen codePenId="PNaGbb" />);
59+
60+
act(() => {
61+
(window as any).triggerGeneralObserver();
62+
return undefined;
63+
});
64+
65+
expect(screen.getByTestId('codepen')).toBeDefined();
66+
});
67+
});
68+
```
69+
70+
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:
73+
74+
```javascript
75+
...
76+
<GeneralObserver>
77+
<iframe
78+
data-testid="codepen"
79+
...
80+
/>
81+
</GeneralObserver>
82+
...
83+
```

packages/mdx-embed/jest.config.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ module.exports = {
55
},
66
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
77
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
8-
moduleNameMapper: {
9-
'\\.(css|less)$': '<rootDir>/test/jest/__mocks__/file-mock.js',
10-
},
8+
setupFiles: ['<rootDir>/test/jest/__mocks__/intersection-observer.js'],
119
collectCoverageFrom: ['src/**/*.{ts,tsx}', '!src/**/index.ts', '!src/**/*.stories.tsx'],
10+
watchPlugins: ['jest-watch-typeahead/filename', 'jest-watch-typeahead/testname'],
1211
};

packages/mdx-embed/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"eslint-plugin-react": "^7.20.6",
6666
"husky": "^4.2.5",
6767
"jest": "^26.4.2",
68+
"jest-watch-typeahead": "^0.6.1",
6869
"prettier": "^2.1.1",
6970
"ts-jest": "^26.3.0",
7071
"ts-loader": "^8.0.3",

packages/mdx-embed/src/components/codepen/codepen.test.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import React from 'react';
22
import { render, screen, act } from '@testing-library/react';
3-
import '../../test/jest/__mocks__/intersection-observer';
43

54
import { CodePen } from './';
65

76
describe('<CodePen />', () => {
87
test('it renders the component', () => {
98
render(<CodePen codePenId="PNaGbb" />);
109

11-
const observerCallback = (window as any).IntersectionObserver.mock.calls[0][0];
12-
1310
act(() => {
14-
observerCallback([{ intersectionRatio: 1 }]);
11+
(window as any).triggerGeneralObserver();
1512
return undefined;
1613
});
1714

packages/mdx-embed/src/test/jest/__mocks__/file-mock.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

packages/mdx-embed/src/test/jest/__mocks__/intersection-observer.js renamed to packages/mdx-embed/test/jest/__mocks__/intersection-observer.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ const intersectionObserverMock = () => ({
44
});
55

66
window.IntersectionObserver = jest.fn().mockImplementation(intersectionObserverMock);
7+
8+
window.triggerGeneralObserver = () => {
9+
window.IntersectionObserver.mock.calls[0][0]([{ intersectionRatio: 1 }]);
10+
};

yarn.lock

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4912,7 +4912,7 @@ ansi-escapes@^3.0.0, ansi-escapes@^3.1.0, ansi-escapes@^3.2.0:
49124912
resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-3.2.0.tgz#8780b98ff9dbf5638152d1f1fe5c1d7b4442976b"
49134913
integrity sha512-cBhpre4ma+U0T1oM5fXg7Dy1Jw7zzwv7lt/GoCpr+hDQJoYnKVPLL4dCvSEFMmQurOQvSrwT7SL/DAlhBI97RQ==
49144914

4915-
ansi-escapes@^4.2.1:
4915+
ansi-escapes@^4.2.1, ansi-escapes@^4.3.1:
49164916
version "4.3.1"
49174917
resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-4.3.1.tgz#a5c47cc43181f1f38ffd7076837700d395522a61"
49184918
integrity sha512-JWF7ocqNrp8u9oqpgV+wH5ftbt+cfvv+PTjOvKLT3AdYly/LmORARfEVT1iyjwN+4MqE5UmVKoAdIBqeoCHgLA==
@@ -13320,6 +13320,19 @@ jest-validate@^26.4.2:
1332013320
leven "^3.1.0"
1332113321
pretty-format "^26.4.2"
1332213322

13323+
jest-watch-typeahead@^0.6.1:
13324+
version "0.6.1"
13325+
resolved "https://registry.yarnpkg.com/jest-watch-typeahead/-/jest-watch-typeahead-0.6.1.tgz#45221b86bb6710b7e97baaa1640ae24a07785e63"
13326+
integrity sha512-ITVnHhj3Jd/QkqQcTqZfRgjfyRhDFM/auzgVo2RKvSwi18YMvh0WvXDJFoFED6c7jd/5jxtu4kSOb9PTu2cPVg==
13327+
dependencies:
13328+
ansi-escapes "^4.3.1"
13329+
chalk "^4.0.0"
13330+
jest-regex-util "^26.0.0"
13331+
jest-watcher "^26.3.0"
13332+
slash "^3.0.0"
13333+
string-length "^4.0.1"
13334+
strip-ansi "^6.0.0"
13335+
1332313336
jest-watcher@^26.3.0:
1332413337
version "26.3.0"
1332513338
resolved "https://registry.yarnpkg.com/jest-watcher/-/jest-watcher-26.3.0.tgz#f8ef3068ddb8af160ef868400318dc4a898eed08"

0 commit comments

Comments
 (0)