Skip to content

Commit b4209ae

Browse files
committed
Tests
Signed-off-by: DimitriDR <dimitridroeck@gmail.com>
1 parent afbdd3c commit b4209ae

File tree

5 files changed

+132
-2
lines changed

5 files changed

+132
-2
lines changed

.github/workflows/node.js.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ jobs:
2222
steps:
2323
- uses: actions/checkout@v3
2424
- run: npm ci
25-
- run: npm test
25+
- run: npm __tests__
File renamed without changes.

src/__tests__/CreateQuiz.test.tsx

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import React from 'react';
2+
import {render, screen, act} from '@testing-library/react';
3+
import App from '../App';
4+
5+
test('Form name', () => {
6+
render(<App/>);
7+
const formName = screen.getByPlaceholderText(/Nom du formulaire/i);
8+
expect(formName).toBeInTheDocument();
9+
expect(formName.tagName).toBe('INPUT');
10+
expect(formName.getAttribute('type')).toBe('text');
11+
});
12+
13+
test('Question X', () => {
14+
render(<App/>);
15+
const questionLabel: HTMLLabelElement = screen.getByText(/Question [1-9]+ :/i);
16+
const questionInput: HTMLInputElement = screen.getByLabelText(/Question [1-9]+ :/i);
17+
18+
expect(questionLabel).toBeInTheDocument();
19+
expect(questionLabel.tagName).toBe('LABEL');
20+
21+
// Making sure the text input is in the document and label is linked to it
22+
expect(questionInput).toBeInTheDocument();
23+
expect(questionInput.tagName).toBe('INPUT');
24+
expect(questionInput.getAttribute('type')).toBe('text');
25+
expect(questionLabel.htmlFor).toBe(questionInput.id);
26+
});
27+
28+
test('Answer X', () => {
29+
render(<App/>);
30+
const answerLabels: HTMLLabelElement[] = screen.getAllByText(/Réponse [1-4] :/i);
31+
const answerInputs: HTMLInputElement[] = screen.getAllByLabelText(/Réponse [1-4] :/i);
32+
33+
// Making sure there's only four labels and inputs
34+
expect(answerLabels).toHaveLength(4);
35+
expect(answerInputs).toHaveLength(4);
36+
37+
// For each label, we test if it's in the document, if it's a label.
38+
answerLabels.forEach((label: HTMLLabelElement, index: number) => {
39+
expect(label).toBeInTheDocument();
40+
expect(label.tagName).toBe('LABEL');
41+
expect(label.htmlFor).toBe(answerInputs[index].id);
42+
});
43+
44+
// For each input, we test if it's in the document, if it's an input, if it's a text input and if it's linked to a label.
45+
answerInputs.forEach((input: HTMLInputElement, index: number) => {
46+
expect(input).toBeInTheDocument();
47+
expect(input.tagName).toBe('INPUT');
48+
expect(input.getAttribute('type')).toBe('text');
49+
expect(input.id).toBe(answerLabels[index].htmlFor);
50+
});
51+
});
52+
53+
test('Add new question', () => {
54+
render(<App/>);
55+
const addQuestionButton: HTMLElement = screen.getByText(/Ajouter une question/i);
56+
expect(addQuestionButton).toBeInTheDocument();
57+
expect(addQuestionButton.tagName).toBe('BUTTON');
58+
59+
const firstQuestionLabel: HTMLLabelElement = screen.getByText(/Question [1-9]+ :/i);
60+
const firstQuestionNumber: number = parseInt(firstQuestionLabel.textContent!.split(' ')[1]);
61+
62+
// Should be 1 as there's only one question
63+
expect(firstQuestionLabel).toBeInTheDocument();
64+
expect(firstQuestionNumber).toBe(1);
65+
expect(firstQuestionLabel.tagName).toBe('LABEL');
66+
67+
// eslint-disable-next-line testing-library/no-unnecessary-act
68+
act(() => {
69+
// Now clicking on the button to add a new question
70+
addQuestionButton.click();
71+
72+
// Get the new question label and input after firstQuestionLabel (second)
73+
const afterClickQuestionLabels: HTMLLabelElement[] = screen.getAllByText(/Question [1-9]+ :/i);
74+
75+
afterClickQuestionLabels.forEach((label: HTMLLabelElement, index: number) => {
76+
expect(label).toBeInTheDocument();
77+
expect(label.tagName).toBe('LABEL');
78+
expect(label.textContent).toBe(`Question ${index + 1} : `);
79+
});
80+
});
81+
});
82+
83+
test('Create form button', () => {
84+
render(<App/>);
85+
const createFormButton: HTMLElement = screen.getByText(/Créer le formulaire/i);
86+
expect(createFormButton).toBeInTheDocument();
87+
expect(createFormButton.tagName).toBe('BUTTON');
88+
});

src/__tests__/Login.test.tsx

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from 'react';
2+
import {render, screen, act} from '@testing-library/react';
3+
import App from '../App';
4+
import AuthPage from "../view/authPage";
5+
import Login from "../composant/login";
6+
import LoginPage from "../view/loginPage";
7+
8+
test('HTML elements', () => {
9+
render(<LoginPage />)
10+
const username : HTMLInputElement = screen.getByPlaceholderText(/Username/i);
11+
const password : HTMLInputElement = screen.getByPlaceholderText("********");
12+
13+
expect(username).toBeInTheDocument();
14+
expect(username.tagName).toBe("INPUT");
15+
expect(username.type).toBe("text");
16+
17+
expect(password).toBeInTheDocument();
18+
expect(password.tagName).toBe("INPUT");
19+
expect(password.type).toBe("password");
20+
21+
const button : HTMLButtonElement = screen.getByRole("button", {name: /Se connecter/i});
22+
expect(button).toBeInTheDocument();
23+
expect(button.tagName).toBe("BUTTON");
24+
});
25+
26+
test('Not existing user', () => {
27+
render(<LoginPage />)
28+
const username : HTMLInputElement = screen.getByPlaceholderText(/Username/i);
29+
const password : HTMLInputElement = screen.getByPlaceholderText("********");
30+
const button : HTMLButtonElement = screen.getByRole("button", {name: /Se connecter/i});
31+
32+
act(() => {
33+
username.value = "test";
34+
password.value = "test";
35+
button.click();
36+
});
37+
38+
expect(username.value).toBe("test");
39+
expect(password.value).toBe("test");
40+
});

tsconfig.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@
2222
},
2323
"include": [
2424
"src"
25-
, "src/test/App.test.tsx" ]
25+
,
26+
"src/__tests__/App.test.tsx"
27+
]
2628
}

0 commit comments

Comments
 (0)