Skip to content

Commit 9581dfb

Browse files
committed
1. Added unit tests for StudentComponent and StudentContainer. 2. Passing room parameter with the StudentContainer props instead of using useParams inside. This makes possible to test the container rendering. 3. Several changes made that fixes some errors when running the tests
1 parent ca59819 commit 9581dfb

8 files changed

Lines changed: 138 additions & 11 deletions

File tree

‎front/config/test/jest.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ module.exports = {
55
moduleDirectories: ['<rootDir>/src', 'node_modules'],
66
setupFilesAfterEnv: ['<rootDir>/config/test/setup.ts'],
77
modulePathIgnorePatterns: ['cypress'],
8+
transform: {
9+
'^.+\\.svg$': 'jest-svg-transformer',
10+
},
811
};

‎front/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"file-loader": "^5.0.2",
6969
"html-webpack-plugin": "^3.2.0",
7070
"jest": "^24.9.0",
71+
"jest-svg-transformer": "^1.0.0",
7172
"lint-staged": "^9.5.0",
7273
"mini-css-extract-plugin": "^0.8.0",
7374
"npm-run-all": "^4.1.5",

‎front/src/core/api.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as ioClient from 'socket.io-client';
1+
import io from 'socket.io-client';
22
import { Socket } from 'socket.io';
33
import { baseSocketUrl } from './const';
44

@@ -18,5 +18,5 @@ export const createSocket = (connectionSetup: ConnectionSetup): Socket => {
1818
};
1919

2020
// TODO Add channel (room)
21-
return ioClient(socketParams.url, socketParams.options);
21+
return io(socketParams.url, socketParams.options);
2222
};
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import * as React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import { StudentComponent } from './student.component';
4+
5+
describe('StudentComponent tests', () => {
6+
it('It should show the session name and the text', () => {
7+
// Arrange
8+
const props = {
9+
room: 'Room I',
10+
log: 'This is test',
11+
};
12+
13+
const expectedSessionNameText = `Session name: ${props.room}`;
14+
15+
// Act
16+
render(<StudentComponent {...props} />);
17+
18+
// Assert
19+
const sessionName = screen.getByRole('heading');
20+
const textArea = screen.getByRole('textbox');
21+
22+
expect(sessionName).toHaveTextContent(expectedSessionNameText);
23+
expect(textArea).toHaveValue(props.log);
24+
});
25+
26+
it('It should show an empty session name when passing undefined', () => {
27+
// Arrange
28+
const props = {
29+
room: undefined,
30+
log: 'This is test',
31+
};
32+
33+
const expectedSessionNameText = `Session name:`;
34+
35+
// Act
36+
render(<StudentComponent {...props} />);
37+
38+
// Assert
39+
const sessionName = screen.getByRole('heading');
40+
expect(sessionName).toHaveTextContent(expectedSessionNameText);
41+
});
42+
43+
it('It should show an empty session name when passing null', () => {
44+
// Arrange
45+
const props = {
46+
room: null,
47+
log: 'This is test',
48+
};
49+
50+
const expectedSessionNameText = `Session name:`;
51+
52+
// Act
53+
render(<StudentComponent {...props} />);
54+
55+
// Assert
56+
const sessionName = screen.getByRole('heading');
57+
58+
expect(sessionName).toHaveTextContent(expectedSessionNameText);
59+
});
60+
61+
it('It should empty text when passing undefined', () => {
62+
// Arrange
63+
const props = {
64+
room: 'Room I',
65+
log: undefined,
66+
};
67+
68+
const expectedSessionNameText = `Session name: ${props.room}`;
69+
70+
// Act
71+
render(<StudentComponent {...props} />);
72+
73+
// Assert
74+
const textArea = screen.getByRole('textbox');
75+
expect(textArea).toHaveTextContent('');
76+
});
77+
78+
it('It should empty text when passing null', () => {
79+
// Arrange
80+
const props = {
81+
room: 'Room I',
82+
log: undefined,
83+
};
84+
85+
const expectedSessionNameText = `Session name: ${props.room}`;
86+
87+
// Act
88+
render(<StudentComponent {...props} />);
89+
90+
// Assert
91+
const textArea = screen.getByRole('textbox');
92+
expect(textArea).toHaveTextContent('');
93+
});
94+
});

‎front/src/pods/student/student.component.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const StudentComponent: React.FC<Props> = props => {
1616
return (
1717
<>
1818
<main className={mainContainer}>
19-
<Typography className={sessionName} variant="body1">
19+
<Typography className={sessionName} variant="body1" role="heading">
2020
Session name: {room}
2121
</Typography>
2222
<label className={labelTextarea} htmlFor="session">
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import { StudentContainer } from './student.container';
4+
5+
interface Params {
6+
room: string;
7+
}
8+
9+
describe('StudentComponent tests', () => {
10+
it('It should render the room name', () => {
11+
// Arrange
12+
const room = 'Room I';
13+
const expectedSessionName = `Session name: ${room}`;
14+
15+
// Act
16+
render(<StudentContainer room={room}/>);
17+
18+
// Assert
19+
const sessionName = screen.getByRole('heading');
20+
21+
expect(sessionName).toHaveTextContent(expectedSessionName);
22+
});
23+
});

‎front/src/pods/student/student.container.tsx‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as React from 'react';
2-
import { useParams } from 'react-router-dom';
32
import {
43
createSocket,
54
SocketOuputMessageLiteral,
@@ -11,12 +10,11 @@ import { StudentComponent } from './student.component';
1110
import { useWithRef } from 'common';
1211
import { getPreviousSessionContent } from 'common-app';
1312

14-
interface Params {
13+
interface Props {
1514
room: string;
1615
}
1716

18-
export const PlayerContainer = () => {
19-
const { room } = useParams<Params>();
17+
export const StudentContainer: React.FC<Props> = ({ room }) => {
2018
const { log, appendToLog, setLog } = useLog();
2119
const [socket, setSocket, socketRef] = useWithRef<SocketIO.Socket>(null);
2220

@@ -50,7 +48,10 @@ export const PlayerContainer = () => {
5048

5149
React.useEffect(() => {
5250
handleConnection();
53-
getPreviousSessionContent(socketRef, SocketEmitMessageTypes.STUDENT_REQUEST_FULL_CONTENT);
51+
getPreviousSessionContent(
52+
socketRef,
53+
SocketEmitMessageTypes.STUDENT_REQUEST_FULL_CONTENT
54+
);
5455
}, []);
5556

5657
return (

‎front/src/scenes/student.scene.tsx‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import * as React from 'react';
22
import { SessionLayout } from 'layout';
3-
import { PlayerContainer } from 'pods/student';
3+
import { StudentContainer } from 'pods/student';
4+
import { useParams } from 'react-router-dom';
5+
6+
interface Params {
7+
room: string;
8+
}
49

510
export const StudentScene = () => {
611
React.useEffect(() => {
712
document.title = `Student - Code Paster`;
813
}, []);
9-
14+
const { room } = useParams<Params>();
1015
return (
1116
<SessionLayout>
12-
<PlayerContainer />
17+
<StudentContainer room={room}/>
1318
</SessionLayout>
1419
);
1520
};

0 commit comments

Comments
 (0)