Skip to content

Commit 209ce95

Browse files
committed
Add tests for Feedback component
1 parent 424cf79 commit 209ce95

File tree

5 files changed

+315
-7
lines changed

5 files changed

+315
-7
lines changed

jest.setup.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Setup file to extend jest-dom, referenced in packages.json under "jest"
2+
import '@testing-library/jest-dom/extend-expect';

package.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
"@stencil/eslint-plugin": "^0.2.1",
5656
"@stencil/router": "^1.0.1",
5757
"@stencil/sass": "^1.3.1",
58+
"@testing-library/jest-dom": "^5.16.2",
59+
"@testing-library/react": "^12.1.3",
60+
"@testing-library/user-event": "^13.5.0",
5861
"@types/fs-extra": "^9.0.1",
5962
"@types/jest": "^26.0.19",
6063
"@types/node": "^12.12.9",
@@ -94,12 +97,22 @@
9497
"<rootDir>/src"
9598
],
9699
"transform": {
97-
"^.+\\.(ts|tsx)$": "ts-jest"
100+
"^.+\\.(ts|tsx|js)$": [
101+
"babel-jest",
102+
{
103+
"presets": [
104+
"next/babel"
105+
]
106+
}
107+
]
98108
},
99109
"testPathIgnorePatterns": [
100110
"capi",
101111
".next",
102112
"client"
113+
],
114+
"setupFilesAfterEnv": [
115+
"<rootDir>/jest.setup.js"
103116
]
104117
},
105118
"scripts": {
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import Feedback from '../index';
2+
import { render, screen, waitFor } from '@testing-library/react';
3+
import userEvent from '@testing-library/user-event';
4+
5+
jest.mock('next/router', () => ({
6+
useRouter() {
7+
return {
8+
route: '/',
9+
pathname: '',
10+
query: '',
11+
asPath: ''
12+
};
13+
}
14+
}));
15+
16+
jest.mock('@aws-amplify/api', () => ({
17+
API: {
18+
post: (apiName, path, init) => {
19+
return Promise.resolve('success');
20+
}
21+
}
22+
}));
23+
24+
describe('Feedback', () => {
25+
it('should render component with thumbs up and thumbs down button', () => {
26+
const component = <Feedback />;
27+
28+
render(component);
29+
30+
const thumbsUp = screen.getByText('Yes');
31+
const thumbsDown = screen.getByText('No');
32+
33+
expect(thumbsUp).toBeInTheDocument();
34+
expect(thumbsDown).toBeInTheDocument();
35+
});
36+
37+
it('should hide buttons after user clicks Yes button', async () => {
38+
const component = <Feedback />;
39+
40+
render(component);
41+
42+
const thumbsUp = screen.getByText('Yes');
43+
const thumbsDown = screen.getByText('No');
44+
45+
expect(thumbsUp).toBeInTheDocument();
46+
expect(thumbsDown).toBeInTheDocument();
47+
48+
await userEvent.click(thumbsUp);
49+
50+
await waitFor(() => {
51+
expect(thumbsUp).not.toBeInTheDocument();
52+
expect(thumbsDown).not.toBeInTheDocument();
53+
});
54+
});
55+
56+
it('should hide buttons after user clicks No button', async () => {
57+
const component = <Feedback/>;
58+
59+
render(component);
60+
61+
const thumbsUp = screen.getByText('Yes');
62+
const thumbsDown = screen.getByText('No');
63+
64+
expect(thumbsUp).toBeInTheDocument();
65+
expect(thumbsDown).toBeInTheDocument();
66+
67+
await userEvent.click(thumbsDown);
68+
69+
await waitFor(() => {
70+
expect(thumbsUp).not.toBeInTheDocument();
71+
expect(thumbsDown).not.toBeInTheDocument();
72+
});
73+
});
74+
});

src/components/Feedback/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import Amplify, { API } from 'aws-amplify';
1+
import Amplify from '@aws-amplify/core';
2+
import { API } from '@aws-amplify/api';
23
import { useState } from 'react';
34
import { FeedbackStyle, VoteButtonStyle, VoteButtonDivStyle } from './styles';
45
import { useRouter } from 'next/router';
@@ -23,6 +24,8 @@ export default function Feedback() {
2324
const [state, setState] = useState<FeedbackState>(FeedbackState.START);
2425
const [feedbackId, setFeedbackId] = useState(undefined);
2526
const router = useRouter();
27+
const feedbackQuestion = 'Was this page helpful?';
28+
const feedbackAppreciation = 'Thank you for your feedback!';
2629

2730
useEffect(() => {
2831
// UUID of feedback if it exists.
@@ -73,7 +76,7 @@ export default function Feedback() {
7376
<FeedbackStyle>
7477
{state === FeedbackState.START ? (
7578
<>
76-
<p>Was this page helpful?</p>
79+
<p>{feedbackQuestion}</p>
7780
<VoteButtonDivStyle>
7881
<VoteButtonStyle onClick={() => submitVote(true)}>
7982
<img src="/assets/thumbs-up.svg" alt="Thumbs up" />
@@ -86,7 +89,7 @@ export default function Feedback() {
8689
</VoteButtonDivStyle>
8790
</>
8891
) : (
89-
<p>Thank you for your feedback!</p>
92+
<p>{feedbackAppreciation}</p>
9093
)}
9194
</FeedbackStyle>
9295
);

0 commit comments

Comments
 (0)