Skip to content

Commit c60f4c1

Browse files
authored
Merge pull request #104 from serconde/#100--Accessibility-check-html-element-roles-trainer
Added unit tests for all the subcomponents of the trainer component
2 parents 7f268d6 + 84e1b46 commit c60f4c1

File tree

5 files changed

+182
-3
lines changed

5 files changed

+182
-3
lines changed

front/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"@testing-library/jest-dom": "^4.2.4",
5050
"@testing-library/react": "^9.3.2",
5151
"@testing-library/react-hooks": "^3.2.1",
52-
"@testing-library/user-event": "^12.1.6",
52+
"@testing-library/user-event": "^12.2.2",
5353
"@types/jest": "^24.0.23",
5454
"@types/react": "^16.9.13",
5555
"@types/react-dom": "^16.9.4",
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import { HeaderComponent } from './header.component';
4+
5+
describe('HeaderComponent unit tests', () => {
6+
it('should show the trainer and the students links when passing valid values', () => {
7+
// Arrange
8+
const props = {
9+
currentTrainerUrl: 'http://current.trainer.url',
10+
currentStudentUrl: 'http://current.student.url',
11+
};
12+
13+
// Act
14+
render(<HeaderComponent {...props} />);
15+
const linkCopyInputs = screen.getAllByRole('textbox');
16+
const linkCopyButtons = screen.getAllByRole('button');
17+
18+
// Assert
19+
20+
expect(linkCopyInputs.length).toBe(2);
21+
expect(linkCopyButtons.length).toBe(2);
22+
linkCopyInputs.forEach(input => expect(input).toHaveAttribute('readonly'));
23+
expect(linkCopyInputs[0]).toHaveValue(props.currentTrainerUrl);
24+
expect(linkCopyInputs[1]).toHaveValue(props.currentStudentUrl);
25+
});
26+
27+
it('should show empty trainer and the students link when passing undefined values', () => {
28+
// Arrange
29+
const props = {
30+
currentTrainerUrl: undefined,
31+
currentStudentUrl: undefined,
32+
};
33+
34+
// Act
35+
render(<HeaderComponent {...props} />);
36+
const linkCopyInputs = screen.getAllByRole('textbox');
37+
38+
// Assert
39+
40+
expect(linkCopyInputs[0]).toBeEmpty();
41+
expect(linkCopyInputs[1]).toBeEmpty();
42+
});
43+
44+
it('should show empty trainer and the students link when passing null values', () => {
45+
// Arrange
46+
const props = {
47+
currentTrainerUrl: null,
48+
currentStudentUrl: null,
49+
};
50+
51+
// Act
52+
render(<HeaderComponent {...props} />);
53+
const linkCopyInputs = screen.getAllByRole('textbox');
54+
55+
// Assert
56+
57+
expect(linkCopyInputs[0]).toBeEmpty();
58+
expect(linkCopyInputs[1]).toBeEmpty();
59+
});
60+
});

front/src/pods/trainer/components/header.component.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ export const HeaderComponent: React.FC<Props> = props => {
1818
<CopyFieldComponent
1919
labelName="Trainer Link"
2020
inputId="trainer-link"
21-
urlLink={currentTrainerUrl}
21+
urlLink={currentTrainerUrl ?? ''}
2222
/>
2323
<CopyFieldComponent
2424
labelName="Students Link"
2525
inputId="student-link"
26-
urlLink={currentStudentUrl}
26+
urlLink={currentStudentUrl ?? ''}
2727
/>
2828
</div>
2929
);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React from 'react';
2+
import { fireEvent, render, screen } from '@testing-library/react';
3+
import userEvent from '@testing-library/user-event';
4+
import { NewTextComponent } from './new-text.component';
5+
6+
describe('NewTextComponent unit tests', () => {
7+
it('should handle the entered text when clicking on the send button', () => {
8+
// Arrange
9+
const handleAppendTrainerText = jest.fn();
10+
11+
// Act
12+
render(
13+
<NewTextComponent handleAppendTrainerText={handleAppendTrainerText} />
14+
);
15+
16+
const inputText = screen.getByRole('textbox');
17+
const sendButton = screen.getByRole('button');
18+
19+
userEvent.type(inputText, 'Test text');
20+
userEvent.click(sendButton);
21+
22+
// Assert
23+
expect(handleAppendTrainerText).toHaveBeenCalledTimes(1);
24+
});
25+
26+
it('should not handle any text when there is not entered text and clicking on the send button', () => {
27+
// Arrange
28+
const handleAppendTrainerText = jest.fn();
29+
30+
// Act
31+
render(
32+
<NewTextComponent handleAppendTrainerText={handleAppendTrainerText} />
33+
);
34+
35+
const sendButton = screen.getByRole('button');
36+
37+
userEvent.click(sendButton);
38+
39+
// Assert
40+
expect(handleAppendTrainerText).not.toHaveBeenCalled();
41+
});
42+
});
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import React from 'react';
2+
import {render, screen} from '@testing-library/react';
3+
import { SessionComponent } from './session.component';
4+
import userEvent from '@testing-library/user-event';
5+
6+
describe('SessionComponent unit tests', () => {
7+
it('should send the concatenation of the session saved text plus the entered one when clicking the send full content button', () => {
8+
// Arrange
9+
const props = {
10+
log: 'Hello! ',
11+
handleSendFullContentLog: jest.fn(),
12+
}
13+
const expectedSentText = 'Hello! How are you doing?';
14+
15+
// Act
16+
17+
render(<SessionComponent {...props} />);
18+
19+
const textArea = screen.getByRole('textbox');
20+
const sendFullContentButton = screen.getAllByRole('button')[1];
21+
22+
userEvent.type(textArea, 'How are you doing?');
23+
userEvent.click(sendFullContentButton);
24+
25+
// Assert
26+
27+
expect(props.handleSendFullContentLog).toHaveBeenCalledTimes(1);
28+
expect(props.handleSendFullContentLog).toHaveBeenCalledWith(expectedSentText);
29+
});
30+
31+
it('should send the concatenation of the previous session text plus the entered one when clicking the send full content button', () => {
32+
// Arrange
33+
const props = {
34+
log: 'Hello! ',
35+
handleSendFullContentLog: jest.fn(),
36+
}
37+
const expectedSentText = 'Hello! How are you doing?';
38+
39+
// Act
40+
41+
render(<SessionComponent {...props} />);
42+
43+
const textArea = screen.getByRole('textbox');
44+
const sendFullContentButton = screen.getAllByRole('button')[1];
45+
46+
userEvent.type(textArea, 'How are you doing?');
47+
userEvent.click(sendFullContentButton);
48+
49+
// Assert
50+
51+
expect(props.handleSendFullContentLog).toHaveBeenCalledTimes(1);
52+
expect(props.handleSendFullContentLog).toHaveBeenCalledWith(expectedSentText);
53+
});
54+
55+
it('should reset the session text to the original one when clicking the undo button', () => {
56+
// Arrange
57+
const props = {
58+
log: 'Hello! ',
59+
handleSendFullContentLog: jest.fn(),
60+
}
61+
const expectedSentText = 'Hello! ';
62+
63+
// Act
64+
65+
render(<SessionComponent {...props} />);
66+
67+
const textArea = screen.getByRole('textbox');
68+
const undoButton = screen.getAllByRole('button')[0];
69+
70+
userEvent.type(textArea, 'How are you doing?');
71+
undoButton.click();
72+
73+
// Assert
74+
75+
expect(textArea).toHaveValue(expectedSentText);
76+
});
77+
});

0 commit comments

Comments
 (0)