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 ( / N o m d u f o r m u l a i r e / 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 ( / Q u e s t i o n [ 1 - 9 ] + : / i) ;
16
+ const questionInput : HTMLInputElement = screen . getByLabelText ( / Q u e s t i o n [ 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 é p o n s e [ 1 - 4 ] : / i) ;
31
+ const answerInputs : HTMLInputElement [ ] = screen . getAllByLabelText ( / R é p o n s e [ 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 ( / A j o u t e r u n e q u e s t i o n / i) ;
56
+ expect ( addQuestionButton ) . toBeInTheDocument ( ) ;
57
+ expect ( addQuestionButton . tagName ) . toBe ( 'BUTTON' ) ;
58
+
59
+ const firstQuestionLabel : HTMLLabelElement = screen . getByText ( / Q u e s t i o n [ 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 ( / Q u e s t i o n [ 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 ( / C r é e r l e f o r m u l a i r e / i) ;
86
+ expect ( createFormButton ) . toBeInTheDocument ( ) ;
87
+ expect ( createFormButton . tagName ) . toBe ( 'BUTTON' ) ;
88
+ } ) ;
0 commit comments