@@ -184,6 +184,54 @@ describe('Mosaic TagInput', () => {
184184 expect ( new FormData ( form ) . getAll ( 'emails' ) ) . toEqual ( [ 'preston@clerk.dev' ] ) ;
185185 } ) ;
186186
187+ describe ( 'outside the form named by form' , ( ) => {
188+ function submitInvite ( props : Pick < React . ComponentProps < typeof TagInput > , 'required' | 'defaultValue' > ) {
189+ const onSubmit = vi . fn ( ( event : React . FormEvent < HTMLFormElement > ) => {
190+ event . preventDefault ( ) ;
191+ return new FormData ( event . currentTarget ) . getAll ( 'emails' ) ;
192+ } ) ;
193+ render (
194+ < >
195+ < form
196+ id = 'invite'
197+ data-testid = 'form'
198+ onSubmit = { onSubmit }
199+ />
200+ < TagInput
201+ aria-label = 'Email'
202+ name = 'emails'
203+ form = 'invite'
204+ { ...props }
205+ />
206+ </ > ,
207+ ) ;
208+ const form = screen . getByTestId ( 'form' ) ;
209+ if ( ! ( form instanceof HTMLFormElement ) ) {
210+ throw new Error ( 'expected a form' ) ;
211+ }
212+ form . requestSubmit ( ) ;
213+ return onSubmit ;
214+ }
215+
216+ it ( 'blocks the submit while required and empty' , ( ) => {
217+ const onSubmit = submitInvite ( { required : true } ) ;
218+
219+ expect ( onSubmit ) . not . toHaveBeenCalled ( ) ;
220+ } ) ;
221+
222+ it ( 'submits a required value once it has a tag' , ( ) => {
223+ const onSubmit = submitInvite ( { required : true , defaultValue : [ 'preston@clerk.dev' ] } ) ;
224+
225+ expect ( onSubmit ) . toHaveReturnedWith ( [ 'preston@clerk.dev' ] ) ;
226+ } ) ;
227+
228+ it ( 'submits while optional and empty' , ( ) => {
229+ const onSubmit = submitInvite ( { } ) ;
230+
231+ expect ( onSubmit ) . toHaveReturnedWith ( [ ] ) ;
232+ } ) ;
233+ } ) ;
234+
187235 it ( 'forwards the ref and native input props to the text input' , ( ) => {
188236 const ref = React . createRef < HTMLInputElement > ( ) ;
189237 render (
0 commit comments