@@ -60,71 +60,14 @@ describe('Attrable', () => {
6060 expect ( instance ) . to . have . property ( 'baz' , 'universe' )
6161 } )
6262
63- it ( 'resets to the default value when the attribute is removed' , async ( ) => {
64- instance . setAttribute ( 'data-foo' , 'goodbye' )
65- expect ( instance ) . to . have . property ( 'foo' , 'goodbye' )
66- instance . removeAttribute ( 'data-foo' )
67- await Promise . resolve ( )
68- expect ( instance ) . to . have . property ( 'foo' , 'hello' )
69- } )
70-
7163 it ( 'changes the attribute when the property changes' , ( ) => {
7264 instance . foo = 'goodbye'
7365 expect ( instance ) . to . have . attribute ( 'data-foo' , 'goodbye' )
7466 instance . baz = 'universe'
7567 expect ( instance ) . to . have . attribute ( 'data-baz' , 'universe' )
7668 } )
7769
78- it ( 'calls underlying get/set' , async ( ) => {
79- instance . getCount = 0
80- instance . setCount = 0
81- instance . baz
82- expect ( instance ) . to . have . property ( 'getCount' , 1 )
83- expect ( instance ) . to . have . property ( 'setCount' , 0 )
84- instance . baz = 2
85- expect ( instance ) . to . have . property ( 'getCount' , 1 )
86- expect ( instance ) . to . have . property ( 'setCount' , 1 )
87- } )
88-
89- it ( 'does not overly eagerly call get/set on attribute change' , async ( ) => {
90- instance . getCount = 0
91- instance . setCount = 0
92- instance . setAttribute ( 'data-baz' , 'one' )
93- instance . setAttribute ( 'data-baz' , 'one' )
94- instance . setAttribute ( 'data-baz' , 'one' )
95- instance . setAttribute ( 'data-baz' , 'one' )
96- await Promise . resolve ( )
97- expect ( instance ) . to . have . property ( 'getCount' , 0 )
98- expect ( instance ) . to . have . property ( 'setCount' , 4 )
99- } )
100-
10170 describe ( 'types' , ( ) => {
102- it ( 'infers number types from property and casts as number always' , async ( ) => {
103- @controller
104- // eslint-disable-next-line @typescript-eslint/no-unused-vars
105- class NumberAttrTest extends HTMLElement {
106- @attr foo = 1
107- }
108- instance = await fixture ( html `< number-attr-test /> ` )
109-
110- expect ( instance ) . to . have . property ( 'foo' , 1 )
111- expect ( instance ) . to . have . attribute ( 'data-foo' , '1' )
112- instance . setAttribute ( 'data-foo' , '7' )
113- await Promise . resolve ( )
114- expect ( instance ) . to . have . property ( 'foo' , 7 )
115- instance . setAttribute ( 'data-foo' , '-3.14' )
116- await Promise . resolve ( )
117- expect ( instance ) . to . have . property ( 'foo' , - 3.14 )
118- instance . setAttribute ( 'data-foo' , 'Not a Number' )
119- await Promise . resolve ( )
120- expect ( instance ) . to . have . property ( 'foo' ) . satisfy ( Number . isNaN )
121- instance . foo = 3.14
122- expect ( instance . getAttribute ( 'data-foo' ) ) . to . equal ( '3.14' )
123- instance . removeAttribute ( 'data-foo' )
124- await Promise . resolve ( )
125- expect ( instance ) . to . have . property ( 'foo' , 1 )
126- } )
127-
12871 it ( 'infers boolean types from property and uses has/toggleAttribute' , async ( ) => {
12972 @controller
13073 // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -157,48 +100,6 @@ describe('Attrable', () => {
157100 expect ( instance ) . to . have . property ( 'foo' , false )
158101 } )
159102
160- it ( 'defaults to inferring string type for non-boolean non-number types' , async ( ) => {
161- const regexp = / ^ a r e g e x p $ /
162- @controller
163- // eslint-disable-next-line @typescript-eslint/no-unused-vars
164- class RegExpAttrTest extends HTMLElement {
165- @attr foo = regexp
166- }
167- instance = await fixture ( html `< reg-exp-attr-test /> ` )
168-
169- expect ( instance ) . to . have . property ( 'foo' , '/^a regexp$/' )
170- expect ( instance ) . to . have . attribute ( 'data-foo' , '/^a regexp$/' )
171- instance . setAttribute ( 'data-foo' , '/^another$/' )
172- await Promise . resolve ( )
173- expect ( instance ) . to . have . property ( 'foo' , '/^another$/' )
174- instance . removeAttribute ( 'data-foo' )
175- await Promise . resolve ( )
176- expect ( instance ) . to . have . property ( 'foo' , regexp )
177- } )
178-
179- it ( 'defers to custom set logic if present' , async ( ) => {
180- const regexp = / ^ a r e g e x p $ /
181- @controller
182- // eslint-disable-next-line @typescript-eslint/no-unused-vars
183- class RegExpCastAttrTest extends HTMLElement {
184- #reg = regexp
185- @attr
186- get foo ( ) {
187- return this . #reg
188- }
189- set foo ( value ) {
190- this . #reg = value instanceof RegExp ? value : new RegExp ( String ( value ) . replace ( / ^ \/ | \/ $ / g, '' ) )
191- }
192- }
193- instance = await fixture ( html `< reg-exp-cast-attr-test /> ` )
194-
195- expect ( instance ) . to . have . property ( 'foo' , regexp )
196- expect ( instance ) . to . have . attribute ( 'data-foo' , '/^a regexp$/' )
197- instance . setAttribute ( 'data-foo' , '/^another$/' )
198- await Promise . resolve ( )
199- expect ( instance ) . to . have . property ( 'foo' ) . a ( 'regexp' ) . property ( 'source' , '^another$' )
200- } )
201-
202103 it ( 'avoids infinite loops' , async ( ) => {
203104 @controller
204105 // eslint-disable-next-line @typescript-eslint/no-unused-vars
0 commit comments