@@ -6,43 +6,117 @@ import FormAssociatedFalse from 'x/formAssociatedFalse';
66import NotFormAssociatedNoAttachInternals from 'x/notFormAssociatedNoAttachInternals' ;
77import FormAssociatedNoAttachInternals from 'x/formAssociatedNoAttachInternals' ;
88import FormAssociatedFalseNoAttachInternals from 'x/formAssociatedFalseNoAttachInternals' ;
9- import {
10- ENABLE_ELEMENT_INTERNALS_AND_FACE ,
11- IS_SYNTHETIC_SHADOW_LOADED ,
12- } from '../../../../../helpers/constants.js' ;
13-
14- describe . runIf (
15- ENABLE_ELEMENT_INTERNALS_AND_FACE &&
16- typeof ElementInternals !== 'undefined' &&
17- ! IS_SYNTHETIC_SHADOW_LOADED
18- ) ( 'should throw an error when duplicate tag name used' , ( ) => {
19- it ( 'with different formAssociated value' , ( ) => {
20- // Register tag with formAssociated = true
21- createElement ( 'x-form-associated' , { is : FormAssociated } ) ;
22- // Try to register again with formAssociated = false
23- expect ( ( ) => createElement ( 'x-form-associated' , { is : FormAssociatedFalse } ) ) . toThrowError (
24- / < x - f o r m - a s s o c i a t e d > w a s a l r e a d y r e g i s t e r e d w i t h f o r m A s s o c i a t e d = t r u e . I t c a n n o t b e r e - r e g i s t e r e d w i t h f o r m A s s o c i a t e d = f a l s e . P l e a s e r e n a m e y o u r c o m p o n e n t t o h a v e a d i f f e r e n t n a m e t h a n < x - f o r m - a s s o c i a t e d > /
25- ) ;
26- } ) ;
9+ import { ENABLE_ELEMENT_INTERNALS_AND_FACE } from '../../../../../helpers/constants.js' ;
2710
28- it ( 'should not throw when duplicate tag name used with the same formAssociated value' , ( ) => {
29- // formAssociated = true
30- createElement ( 'x-form-associated' , { is : FormAssociated } ) ;
31- expect ( ( ) => createElement ( 'x-form-associated' , { is : FormAssociated } ) ) . not . toThrow ( ) ;
32- // formAssociated = false
33- createElement ( 'x-form-associated-false' , { is : FormAssociatedFalse } ) ;
34- expect ( ( ) =>
35- createElement ( 'x-form-associated-false' , { is : FormAssociatedFalse } )
36- ) . not . toThrow ( ) ;
37- // formAssociated = undefined
38- createElement ( 'x-not-form-associated' , { is : NotFormAssociated } ) ;
39- expect ( ( ) =>
40- createElement ( 'x-not-form-associated' , { is : NotFormAssociated } )
41- ) . not . toThrow ( ) ;
42- } ) ;
43- } ) ;
11+ describe . runIf ( ENABLE_ELEMENT_INTERNALS_AND_FACE && typeof ElementInternals !== 'undefined' ) (
12+ 'should throw an error when duplicate tag name used' ,
13+ ( ) => {
14+ it ( 'with different formAssociated value' , ( ) => {
15+ // Register tag with formAssociated = true
16+ createElement ( 'x-form-associated' , { is : FormAssociated } ) ;
17+ // Try to register again with formAssociated = false
18+ expect ( ( ) =>
19+ createElement ( 'x-form-associated' , { is : FormAssociatedFalse } )
20+ ) . toThrowError (
21+ / < x - f o r m - a s s o c i a t e d > w a s a l r e a d y r e g i s t e r e d w i t h f o r m A s s o c i a t e d = t r u e . I t c a n n o t b e r e - r e g i s t e r e d w i t h f o r m A s s o c i a t e d = f a l s e . P l e a s e r e n a m e y o u r c o m p o n e n t t o h a v e a d i f f e r e n t n a m e t h a n < x - f o r m - a s s o c i a t e d > /
22+ ) ;
23+ } ) ;
24+
25+ it ( 'should not throw when duplicate tag name used with the same formAssociated value' , ( ) => {
26+ // formAssociated = true
27+ createElement ( 'x-form-associated' , { is : FormAssociated } ) ;
28+ expect ( ( ) => createElement ( 'x-form-associated' , { is : FormAssociated } ) ) . not . toThrow ( ) ;
29+ // formAssociated = false
30+ createElement ( 'x-form-associated-false' , { is : FormAssociatedFalse } ) ;
31+ expect ( ( ) =>
32+ createElement ( 'x-form-associated-false' , { is : FormAssociatedFalse } )
33+ ) . not . toThrow ( ) ;
34+ // formAssociated = undefined
35+ createElement ( 'x-not-form-associated' , { is : NotFormAssociated } ) ;
36+ expect ( ( ) =>
37+ createElement ( 'x-not-form-associated' , { is : NotFormAssociated } )
38+ ) . not . toThrow ( ) ;
39+ } ) ;
40+
41+ it ( 'should throw an error when accessing form related properties on a non-form associated component' , ( ) => {
42+ const form = document . createElement ( 'form' ) ;
43+ document . body . appendChild ( form ) ;
44+
45+ const testElements = {
46+ 'x-form-associated-false' : FormAssociatedFalse ,
47+ 'x-not-form-associated' : NotFormAssociated ,
48+ } ;
49+ let elm ;
50+ Object . entries ( testElements ) . forEach ( ( [ tagName , ctor ] ) => {
51+ elm = createElement ( `x-${ tagName } ` , { is : ctor } ) ;
52+ const { internals } = elm ;
53+ form . appendChild ( elm ) ;
54+ expect ( ( ) => internals . form ) . toThrow ( ) ;
55+ expect ( ( ) => internals . setFormValue ( '2019-03-15' ) ) . toThrow ( ) ;
56+ expect ( ( ) => internals . willValidate ) . toThrow ( ) ;
57+ expect ( ( ) => internals . validity ) . toThrow ( ) ;
58+ expect ( ( ) => internals . checkValidity ( ) ) . toThrow ( ) ;
59+ expect ( ( ) => internals . reportValidity ( ) ) . toThrow ( ) ;
60+ expect ( ( ) => internals . setValidity ( '' ) ) . toThrow ( ) ;
61+ expect ( ( ) => internals . validationMessage ) . toThrow ( ) ;
62+ expect ( ( ) => internals . labels ) . toThrow ( ) ;
63+ } ) ;
64+ document . body . removeChild ( form ) ;
65+ } ) ;
66+
67+ it ( 'should be able to use internals to validate form associated component' , ( ) => {
68+ const elm = createElement ( 'x-form-associated' , { is : FormAssociated } ) ;
69+ const { internals } = elm ;
70+ expect ( internals . willValidate ) . toBe ( true ) ;
71+ expect ( internals . validity . valid ) . toBe ( true ) ;
72+ expect ( internals . checkValidity ( ) ) . toBe ( true ) ;
73+ expect ( internals . reportValidity ( ) ) . toBe ( true ) ;
74+ expect ( internals . validationMessage ) . toBe ( '' ) ;
75+
76+ internals . setValidity ( { rangeUnderflow : true } , 'pick future date' ) ;
77+
78+ expect ( internals . validity . valid ) . toBe ( false ) ;
79+ expect ( internals . checkValidity ( ) ) . toBe ( false ) ;
80+ expect ( internals . reportValidity ( ) ) . toBe ( false ) ;
81+ expect ( internals . validationMessage ) . toBe ( 'pick future date' ) ;
82+ } ) ;
83+
84+ it ( 'should be able to use setFormValue on a form associated component' , ( ) => {
85+ const form = document . createElement ( 'form' ) ;
86+ document . body . appendChild ( form ) ;
87+
88+ const elm = createElement ( 'x-form-associated' , { is : FormAssociated } ) ;
89+ const { internals } = elm ;
90+ form . appendChild ( elm ) ;
91+
92+ expect ( internals . form ) . toBe ( form ) ;
93+
94+ elm . setAttribute ( 'name' , 'date' ) ;
95+ const inputElm = elm . shadowRoot
96+ . querySelector ( 'x-input' )
97+ . shadowRoot . querySelector ( 'input' ) ;
98+ internals . setFormValue ( '2019-03-15' , '3/15/2019' , inputElm ) ;
99+ const formData = new FormData ( form ) ;
100+ expect ( formData . get ( 'date' ) ) . toBe ( '2019-03-15' ) ;
101+ } ) ;
102+
103+ it ( 'should be able to associate labels to a form associated component' , ( ) => {
104+ const elm = createElement ( 'x-form-associated' , { is : FormAssociated } ) ;
105+ document . body . appendChild ( elm ) ;
106+ const { internals } = elm ;
107+
108+ expect ( internals . labels . length ) . toBe ( 0 ) ;
109+ elm . id = 'test-id' ;
110+ const label = document . createElement ( 'label' ) ;
111+ label . htmlFor = elm . id ;
112+ document . body . appendChild ( label ) ;
113+ expect ( internals . labels . length ) . toBe ( 1 ) ;
114+ expect ( internals . labels [ 0 ] ) . toBe ( label ) ;
115+ } ) ;
116+ }
117+ ) ;
44118
45- it . runIf ( typeof ElementInternals !== 'undefined' && ! IS_SYNTHETIC_SHADOW_LOADED ) (
119+ it . runIf ( typeof ElementInternals === 'undefined' ) (
46120 'disallows form association on older API versions' ,
47121 ( ) => {
48122 const isFormAssociated = ( elm ) => {
0 commit comments