@@ -17,8 +17,15 @@ describe('FormData', () => {
1717 vi . restoreAllMocks ( ) ;
1818 } ) ;
1919
20+ beforeEach ( ( ) => {
21+ window = new Window ( ) ;
22+ document = window . document ;
23+ } ) ;
24+ afterEach ( ( ) => {
25+ vi . restoreAllMocks ( ) ;
26+ } ) ;
2027 describe ( 'constructor' , ( ) => {
21- it ( 'Supports sending in an HTMLFormElement to the contructor .' , ( ) => {
28+ it ( 'Supports sending in an HTMLFormElement to the constructor .' , ( ) => {
2229 const form = document . createElement ( 'form' ) ;
2330 const file = new File ( [ Buffer . from ( 'fileContent' ) ] , 'file.txt' , { type : 'text/plain' } ) ;
2431 const textInput = document . createElement ( 'input' ) ;
@@ -37,33 +44,26 @@ describe('FormData', () => {
3744 textInput . type = 'text' ;
3845 textInput . name = 'textInput' ;
3946 textInput . value = 'text value' ;
40-
4147 hiddenInput . type = 'hidden' ;
4248 hiddenInput . name = 'hiddenInput' ;
4349 hiddenInput . value = 'hidden value 1' ;
44-
4550 hiddenInput2 . type = 'hidden' ;
4651 hiddenInput2 . name = 'hiddenInput' ;
4752 hiddenInput2 . value = 'hidden value 2' ;
48-
4953 fileInput . type = 'file' ;
5054 fileInput . name = 'fileInput' ;
5155 fileInput . files . push ( file ) ;
52-
5356 radioInput1 . type = 'radio' ;
5457 radioInput1 . name = 'radioInput' ;
5558 radioInput1 . value = 'radio value 1' ;
5659 radioInput1 . checked = false ;
57-
5860 radioInput2 . type = 'radio' ;
5961 radioInput2 . name = 'radioInput' ;
6062 radioInput2 . value = 'radio value 2' ;
6163 radioInput2 . checked = true ;
62-
6364 checkboxInput1 . type = 'checkbox' ;
6465 checkboxInput1 . name = 'checkboxInput' ;
6566 checkboxInput1 . value = 'checkbox value 1' ;
66-
6767 checkboxInput2 . type = 'checkbox' ;
6868 checkboxInput2 . name = 'checkboxInput' ;
6969 checkboxInput2 . value = 'checkbox value 2' ;
@@ -106,8 +106,106 @@ describe('FormData', () => {
106106 expect ( formData . getAll ( 'checkboxInput' ) ) . toEqual ( [ 'checkbox value 2' ] ) ;
107107 expect ( formData . get ( 'button1' ) ) . toBe ( null ) ;
108108 expect ( formData . get ( 'button2' ) ) . toBe ( null ) ;
109- expect ( formData . get ( 'button3' ) ) . toBe ( 'button3' ) ;
110- expect ( formData . get ( 'button4' ) ) . toBe ( 'button4' ) ;
109+ expect ( formData . get ( 'button3' ) ) . toBe ( null ) ;
110+ expect ( formData . get ( 'button4' ) ) . toBe ( null ) ;
111+ } ) ;
112+
113+ it ( 'Supports sending in an HTMLFormElement and a submitter to the constructor.' , ( ) => {
114+ const form = document . createElement ( 'form' ) ;
115+ const file = new File ( [ Buffer . from ( 'fileContent' ) ] , 'file.txt' , { type : 'text/plain' } ) ;
116+ const textInput = document . createElement ( 'input' ) ;
117+ const hiddenInput = document . createElement ( 'input' ) ;
118+ const hiddenInput2 = document . createElement ( 'input' ) ;
119+ const fileInput = document . createElement ( 'input' ) ;
120+ const radioInput1 = document . createElement ( 'input' ) ;
121+ const radioInput2 = document . createElement ( 'input' ) ;
122+ const checkboxInput1 = document . createElement ( 'input' ) ;
123+ const checkboxInput2 = document . createElement ( 'input' ) ;
124+ const button = document . createElement ( 'button' ) ;
125+
126+ textInput . type = 'text' ;
127+ textInput . name = 'textInput' ;
128+ textInput . value = 'text value' ;
129+ hiddenInput . type = 'hidden' ;
130+ hiddenInput . name = 'hiddenInput' ;
131+ hiddenInput . value = 'hidden value 1' ;
132+ hiddenInput2 . type = 'hidden' ;
133+ hiddenInput2 . name = 'hiddenInput' ;
134+ hiddenInput2 . value = 'hidden value 2' ;
135+ fileInput . type = 'file' ;
136+ fileInput . name = 'fileInput' ;
137+ fileInput . files . push ( file ) ;
138+ radioInput1 . type = 'radio' ;
139+ radioInput1 . name = 'radioInput' ;
140+ radioInput1 . value = 'radio value 1' ;
141+ radioInput1 . checked = false ;
142+ radioInput2 . type = 'radio' ;
143+ radioInput2 . name = 'radioInput' ;
144+ radioInput2 . value = 'radio value 2' ;
145+ radioInput2 . checked = true ;
146+ checkboxInput1 . type = 'checkbox' ;
147+ checkboxInput1 . name = 'checkboxInput' ;
148+ checkboxInput1 . value = 'checkbox value 1' ;
149+ checkboxInput2 . type = 'checkbox' ;
150+ checkboxInput2 . name = 'checkboxInput' ;
151+ checkboxInput2 . value = 'checkbox value 2' ;
152+ checkboxInput2 . checked = true ;
153+
154+ button . type = 'submit' ;
155+ button . name = 'button1' ;
156+ button . value = 'button' ;
157+
158+ form . appendChild ( textInput ) ;
159+ form . appendChild ( hiddenInput ) ;
160+ form . appendChild ( hiddenInput2 ) ;
161+ form . appendChild ( fileInput ) ;
162+ form . appendChild ( radioInput1 ) ;
163+ form . appendChild ( radioInput2 ) ;
164+ form . appendChild ( checkboxInput1 ) ;
165+ form . appendChild ( checkboxInput2 ) ;
166+ form . appendChild ( button ) ;
167+
168+ const formData = new window . FormData ( form , button ) ;
169+
170+ expect ( formData . get ( 'textInput' ) ) . toBe ( 'text value' ) ;
171+ expect ( formData . get ( 'hiddenInput' ) ) . toBe ( 'hidden value 1' ) ;
172+ expect ( formData . get ( 'fileInput' ) ) . toBe ( file ) ;
173+ expect ( formData . get ( 'radioInput' ) ) . toBe ( 'radio value 2' ) ;
174+ expect ( formData . get ( 'checkboxInput' ) ) . toBe ( 'checkbox value 2' ) ;
175+ expect ( formData . getAll ( 'hiddenInput' ) ) . toEqual ( [ 'hidden value 1' , 'hidden value 2' ] ) ;
176+ expect ( formData . getAll ( 'radioInput' ) ) . toEqual ( [ 'radio value 2' ] ) ;
177+ expect ( formData . getAll ( 'checkboxInput' ) ) . toEqual ( [ 'checkbox value 2' ] ) ;
178+ expect ( formData . get ( 'button1' ) ) . toBe ( 'button' ) ;
179+ } ) ;
180+
181+ it ( 'Only sends button value if the button has a name and value.' , ( ) => {
182+ const form = document . createElement ( 'form' ) ;
183+ const button1 = document . createElement ( 'button' ) ;
184+ const button2 = document . createElement ( 'input' ) ;
185+ const button3 = document . createElement ( 'button' ) ;
186+ const button4 = document . createElement ( 'input' ) ;
187+
188+ button1 . name = 'button1' ;
189+
190+ button2 . type = 'submit' ;
191+ button2 . name = 'button2' ;
192+
193+ button3 . name = 'button3' ;
194+ button3 . value = 'button3' ;
195+
196+ button4 . type = 'submit' ;
197+ button4 . name = 'button4' ;
198+ button4 . value = 'button4' ;
199+
200+ form . appendChild ( button1 ) ;
201+ form . appendChild ( button2 ) ;
202+ form . appendChild ( button3 ) ;
203+ form . appendChild ( button4 ) ;
204+
205+ expect ( new window . FormData ( form , button1 ) . get ( 'button1' ) ) . toBe ( null ) ;
206+ expect ( new window . FormData ( form , button2 ) . get ( 'button2' ) ) . toBe ( null ) ;
207+ expect ( new window . FormData ( form , button3 ) . get ( 'button3' ) ) . toBe ( 'button3' ) ;
208+ expect ( new window . FormData ( form , button4 ) . get ( 'button4' ) ) . toBe ( 'button4' ) ;
111209 } ) ;
112210
113211 it ( 'Supports input elements with empty values.' , ( ) => {
0 commit comments