Skip to content

Commit 47d5105

Browse files
authored
Migrate component tests to cypress (#5759)
1 parent 03ec344 commit 47d5105

Some content is hidden

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

50 files changed

+644
-937
lines changed

.drone.yml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ steps:
245245
- corepack enable
246246
- pnpm --filter lego-webapp preview
247247

248-
- name: cypress
248+
- name: cypress_e2e
249249
image: cypress/browsers:node-20.18.0-chrome-130.0.6723.69-1-ff-131.0.3-edge-130.0.2849.52-1
250250
shm_size: 512m
251251
ipc: host
@@ -270,7 +270,24 @@ steps:
270270
- ./wait-for-it.sh -t 180 legocypresshelper:3030
271271
- npm i -g corepack
272272
- corepack enable
273-
- pnpm --filter lego-webapp exec cypress run --record
273+
- pnpm --filter lego-webapp exec cypress run --e2e --record
274+
275+
- name: cypress_component
276+
image: cypress/browsers:node-20.18.0-chrome-130.0.6723.69-1-ff-131.0.3-edge-130.0.2849.52-1
277+
when:
278+
event: [push]
279+
branch:
280+
exclude: [build]
281+
depends_on:
282+
- install_cypress
283+
- build
284+
environment:
285+
TZ: Europe/Oslo
286+
CYPRESS_CACHE_FOLDER: /drone/src/.cypress_cache
287+
commands:
288+
- npm i -g corepack
289+
- corepack enable
290+
- pnpm --filter lego-webapp exec cypress run --component --config video=false,screenshotOnRunFailure=false
274291

275292
- name: docker
276293
image: plugins/docker
@@ -285,7 +302,8 @@ steps:
285302
- lint
286303
- typescript
287304
- frontend
288-
- cypress
305+
- cypress_e2e
306+
- cypress_component
289307
environment:
290308
SENTRY_AUTH_TOKEN:
291309
from_secret: sentry_auth_token

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ A coverage report can be generated by running `pnpm test -- --coverage`.
8787

8888
</details>
8989

90-
<details><summary><code>Cypress E2E (End-to-end tests)</code></summary>
90+
<details><summary><code>Cypress E2E and Component tests</code></summary>
9191

9292
### End to end tests (cypress)
9393

@@ -140,6 +140,20 @@ And you run cypress headlessly (no visible browser) in another terminal
140140
pnpm cypress run
141141
```
142142

143+
### Component tests (cypress)
144+
Component tests does not depend on anything to run
145+
146+
Simply open cypress
147+
```bash
148+
$ pnpm cypress open
149+
```
150+
151+
or run in terminal
152+
```bash
153+
pnpm cypress run --component
154+
```
155+
156+
143157
#### STRIPE
144158

145159
In order to run the payment end-2-end tests, a few extra steps are required. First one has to install the stripe cli, log in and then run

lego-webapp/components/Comments/Comment.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const Comment = ({
4040

4141
return (
4242
<>
43-
<div className={styles.comment}>
43+
<div className={styles.comment} data-testid="comment">
4444
{author ? (
4545
<Flex alignItems="center" justifyContent="space-between">
4646
<Flex alignItems="center" gap="var(--spacing-md)">

lego-webapp/components/Comments/CommentTree.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function CommentTree({
6666
);
6767
});
6868

69-
return <div>{tree}</div>;
69+
return <div data-testid="comment-tree">{tree}</div>;
7070
}
7171

7272
export default CommentTree;

lego-webapp/components/Comments/__tests__/CommentTree.spec.tsx

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

lego-webapp/components/Comments/__tests__/CommentView.spec.tsx

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

lego-webapp/components/LoginForm/LoginForm.spec.tsx

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

lego-webapp/components/Time/Time.spec.tsx

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

lego-webapp/cypress.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ export default defineConfig({
1717
baseUrl: 'http://localhost:3000',
1818
specPattern: 'cypress/e2e/**/*.{js,jsx,ts,tsx}',
1919
},
20+
component: {
21+
specPattern: 'cypress/component/**/*.cy.{js,jsx,ts,tsx}',
22+
devServer: {
23+
framework: 'react',
24+
bundler: 'vite',
25+
},
26+
},
2027
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import CommentView from '~/components/Comments/CommentView';
2+
import comments from '~/cypress/fixtures/comments';
3+
4+
describe('<CommentView />', () => {
5+
beforeEach(() => {
6+
cy.mount(
7+
<CommentView contentTarget="articles.article-1" comments={comments} />,
8+
);
9+
});
10+
11+
it('renders a comment tree', () => {
12+
cy.get('[data-testid="comment-tree"]').should('exist');
13+
});
14+
15+
it('renders all comments', () => {
16+
cy.get('[data-testid="comment"]').then(($comments) => {
17+
expect($comments.length).to.equal(comments.length);
18+
});
19+
});
20+
21+
it('renders the top level comments at root level', () => {
22+
cy.get('[data-ischild="false"]').then(($rootComments) => {
23+
const rootCommentsCount = comments.filter(
24+
(comment) => comment.parent === null,
25+
).length;
26+
expect($rootComments.length).to.equal(rootCommentsCount);
27+
});
28+
});
29+
30+
it('renders nested comments', () => {
31+
cy.get('[data-ischild="true"]').then(($nestedComments) => {
32+
const nestedCommentsCount = comments.filter(
33+
(comment) => comment.parent !== null,
34+
).length;
35+
expect($nestedComments.length).to.equal(nestedCommentsCount);
36+
});
37+
});
38+
});

0 commit comments

Comments
 (0)