Skip to content

Commit 3662087

Browse files
committed
feat: add component tests module
1 parent b401987 commit 3662087

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+4229
-65
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { ReactSelect } from '/assets/ReactSelect.js';
2+
3+
function App() {
4+
const languageCookie = document.cookie
5+
.split(';')
6+
.map((c) => c.trimStart())
7+
.find((c) => c.startsWith('language='));
8+
const languageCookieValue = languageCookie?.split('=')[1];
9+
return ReactSelect({
10+
value: languageCookieValue,
11+
options: ['en', 'fr'],
12+
onChange: (newLang) => {
13+
document.location.href = `${document.location.pathname}?lang=${newLang}`;
14+
},
15+
});
16+
}
17+
18+
ReactDOM.render(App(), document.querySelector('#vue-language-switch'));

demo-app/src/assets/ReactSelect.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const e = React.createElement;
2+
3+
export function ReactSelect({ value, options, onChange }) {
4+
return e(
5+
'select',
6+
{
7+
value,
8+
onChange: (event) => {
9+
onChange(event.target.value);
10+
},
11+
},
12+
options.map((option) => e('option', { key: option, value: option }, option))
13+
);
14+
}

demo-app/src/components/header.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export function Header() {
2323
<li>
2424
<a href="/about">{aboutLink}</a>
2525
</li>
26+
<li id="vue-language-switch"></li>
2627
</ul>
2728
</nav>
2829
);

demo-app/src/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ app
2929
// middlewares
3030
.use('/favicon.ico', serveStatic({ path: './src/assets/favicon.ico' }))
3131
.use('/assets/*', serveStatic({ root: './src/' }))
32-
.use(languageDetector({ supportedLanguages: ['en', 'fr'], fallbackLanguage: 'en' }))
32+
.use(
33+
languageDetector({
34+
supportedLanguages: ['en', 'fr'],
35+
fallbackLanguage: 'en',
36+
cookieOptions: { httpOnly: false },
37+
})
38+
)
3339
// html pages
3440
.get('/', (c) => c.html(withContexts(Home, c)))
3541
.get('/about', (c) => c.html(withContexts(About, c)))

demo-app/src/layouts/base.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export function BaseLayout({ children, title }: BaseLayoutProps) {
2626
<Header />
2727
{children}
2828
<Footer />
29+
<script src="https://unpkg.com/react@17/umd/react.development.js" defer></script>
30+
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" defer></script>
31+
<script src="/assets/ReactLanguageSwitch.js" type="module" defer></script>
2932
</body>
3033
</html>
3134
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!-- .slide: class="transition" -->
2+
3+
# Component Tests reminder
4+
5+
##==##
6+
7+
# Component Tests reminder
8+
9+
- Test a UI brick
10+
- Simulate realistic behaviors
11+
- Isolation
12+
<!-- .element: class="list-fragment" -->
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!-- .slide: class="transition" -->
2+
3+
# Mount a component
4+
5+
Notes:
6+
7+
- Next we will work with React component because we need to choose
8+
- It works only for React, Svelte and Vue for now
9+
- It's experimental
10+
11+
##==##
12+
13+
<!-- .slide: class="with-code" -->
14+
15+
# Mount a React component
16+
17+
```TypeScript [1|2|4,6|5|1-6]
18+
import { test, expect } from '@playwright/experimental-ct-react';
19+
import LearnReactButton from './LearnReactButton';
20+
21+
test('should work', async ({ mount }) => {
22+
const component = await mount(<LearnReactButton />);
23+
});
24+
```
25+
26+
<!-- .element: class="big-code" -->
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<!-- .slide: class="transition" -->
2+
3+
# Patterns
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!-- .slide: class="with-code" -->
2+
3+
# Check component content
4+
5+
```TypeScript [3]
6+
test('should work', async ({ mount }) => {
7+
const component = await mount(<LearnReactButton />);
8+
await expect(component).toContainText('Learn React');
9+
});
10+
```
11+
12+
<!-- .element: class="big-code" -->
13+
14+
Notes:
15+
16+
- we can use every assertions we used to use with Playwright
17+
- we no more use the page fixture but directly the component instance
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!-- .slide: class="with-code" -->
2+
3+
# Listen to event
4+
5+
```TypeScript
6+
test('should work', async ({ mount }) => {
7+
let clicked = false;
8+
const component = await mount(<LearnReactButton onClick={() => clicked = true} />);
9+
expect(clicked).toBeTruthy();
10+
});
11+
```
12+
13+
<!-- .element: class="big-code" -->
14+
15+
Notes:
16+
17+
- to get event from a component we can listen events
18+
- we need to save the emitted value in the variable
19+
- then we can make any assertion

0 commit comments

Comments
 (0)