1
1
import type { FormComponentProps } from '../../src/widget/Form' ;
2
2
import { Form } from '../../src/widget/Form' ;
3
3
4
+ type NonNullableFields < T > = {
5
+ [ P in keyof T ] : NonNullable < T [ P ] > ;
6
+ } ;
7
+
4
8
function renderForm ( {
5
9
showName = true ,
6
10
showEmail = true ,
@@ -16,7 +20,7 @@ function renderForm({
16
20
cancelButtonLabel = 'Cancel!' ,
17
21
submitButtonLabel = 'Submit!' ,
18
22
...rest
19
- } : Partial < FormComponentProps > = { } ) {
23
+ } : Partial < FormComponentProps > = { } ) {
20
24
return Form ( {
21
25
isAnonymous,
22
26
showName,
@@ -32,7 +36,7 @@ function renderForm({
32
36
cancelButtonLabel,
33
37
submitButtonLabel,
34
38
...rest ,
35
- } ) ;
39
+ } ) as NonNullableFields < ReturnType < typeof Form > > ;
36
40
}
37
41
38
42
describe ( 'Form' , ( ) => {
@@ -50,7 +54,6 @@ describe('Form', () => {
50
54
51
55
const button = formComponent . el . querySelector ( 'button[type="submit"]' ) as HTMLButtonElement | null ;
52
56
expect ( button ?. textContent ) . toBe ( 'Submit!' ) ;
53
- expect ( button ?. disabled ) . toBe ( true ) ;
54
57
expect ( formComponent . el . querySelector ( 'button[type="button"]' ) ?. textContent ) . toBe ( 'Cancel!' ) ;
55
58
} ) ;
56
59
@@ -84,7 +87,7 @@ describe('Form', () => {
84
87
const messageLabel = formComponent . el . querySelector ( 'label[htmlFor="message"]' ) as HTMLLabelElement ;
85
88
expect ( nameLabel . textContent ) . toBe ( 'Name!' ) ;
86
89
expect ( emailLabel . textContent ) . toBe ( 'Email!' ) ;
87
- expect ( messageLabel . textContent ) . toBe ( 'Description!' ) ;
90
+ expect ( messageLabel . textContent ) . toBe ( 'Description! (required) ' ) ;
88
91
89
92
const nameInput = formComponent . el . querySelector ( '[name="name"]' ) as HTMLInputElement ;
90
93
const emailInput = formComponent . el . querySelector ( '[name="email"]' ) as HTMLInputElement ;
@@ -101,40 +104,24 @@ describe('Form', () => {
101
104
const message = formComponent . el . querySelector ( '[name="message"]' ) as HTMLTextAreaElement ;
102
105
const submit = formComponent . el . querySelector ( 'button[type="submit"]' ) as HTMLButtonElement ;
103
106
104
- expect ( submit . disabled ) . toBe ( true ) ;
105
-
106
107
message . value = 'Foo (message)' ;
107
108
message . dispatchEvent ( new KeyboardEvent ( 'keyup' ) ) ;
108
- expect ( submit . disabled ) . toBe ( false ) ;
109
109
110
110
message . value = '' ;
111
111
message . dispatchEvent ( new KeyboardEvent ( 'keyup' ) ) ;
112
- expect ( submit . disabled ) . toBe ( true ) ;
113
- } ) ;
114
-
115
- it ( 'can manually enable/disable submit button' , ( ) => {
116
- const formComponent = renderForm ( ) ;
117
- const submit = formComponent . el . querySelector ( 'button[type="submit"]' ) as HTMLButtonElement ;
118
- expect ( submit . disabled ) . toBe ( true ) ;
119
-
120
- formComponent . setSubmitEnabled ( ) ;
121
- expect ( submit . disabled ) . toBe ( false ) ;
122
-
123
- formComponent . setSubmitDisabled ( ) ;
124
- expect ( submit . disabled ) . toBe ( true ) ;
125
112
} ) ;
126
113
127
114
it ( 'can show error' , ( ) => {
128
115
const formComponent = renderForm ( ) ;
129
116
const errorEl = formComponent . el . querySelector ( '.form__error-container' ) as HTMLDivElement ;
130
- expect ( errorEl . getAttribute ( 'ariaHidden ' ) ) . toBe ( 'true' ) ;
117
+ expect ( errorEl . getAttribute ( 'aria-hidden ' ) ) . toBe ( 'true' ) ;
131
118
132
119
formComponent . showError ( 'My Error' ) ;
133
- expect ( errorEl . getAttribute ( 'ariaHidden ' ) ) . toBe ( 'false' ) ;
120
+ expect ( errorEl . getAttribute ( 'aria-hidden ' ) ) . toBe ( 'false' ) ;
134
121
expect ( errorEl . textContent ) . toBe ( 'My Error' ) ;
135
122
136
123
formComponent . hideError ( ) ;
137
- expect ( errorEl . getAttribute ( 'ariaHidden ' ) ) . toBe ( 'true' ) ;
124
+ expect ( errorEl . getAttribute ( 'aria-hidden ' ) ) . toBe ( 'true' ) ;
138
125
expect ( errorEl . textContent ) . toBe ( '' ) ;
139
126
} ) ;
140
127
0 commit comments