1- import { expect } from '@open-wc/testing'
1+ import { expect , fixture , html } from '@open-wc/testing'
22import { initializeAttrs , defineObservedAttributes , attr } from '../src/attr.js'
33
44describe ( 'initializeAttrs' , ( ) => {
55 class InitializeAttrTestElement extends HTMLElement { }
66 window . customElements . define ( 'initialize-attr-test-element' , InitializeAttrTestElement )
77
8+ let instance
9+ beforeEach ( async ( ) => {
10+ instance = await fixture ( html `< initialize-attr-test-element /> ` )
11+ } )
12+
813 it ( 'creates a getter/setter pair for each given attr name' , ( ) => {
9- const instance = document . createElement ( 'initialize-attr-test-element' )
1014 expect ( instance ) . to . not . have . ownPropertyDescriptor ( 'foo' )
1115 initializeAttrs ( instance , [ 'foo' ] )
1216 expect ( instance ) . to . have . ownPropertyDescriptor ( 'foo' )
1317 } )
1418
1519 it ( 'reflects the `data-*` attribute name of the given key' , ( ) => {
16- const instance = document . createElement ( 'initialize-attr-test-element' )
1720 initializeAttrs ( instance , [ 'foo' ] )
1821 expect ( instance . foo ) . to . equal ( '' )
1922 instance . foo = 'bar'
@@ -24,7 +27,6 @@ describe('initializeAttrs', () => {
2427 } )
2528
2629 it ( 'sets the attribute to a previously defined value on the key' , ( ) => {
27- const instance = document . createElement ( 'initialize-attr-test-element' )
2830 instance . foo = 'hello'
2931 initializeAttrs ( instance , [ 'foo' ] )
3032 expect ( instance . foo ) . to . equal ( 'hello' )
@@ -33,7 +35,6 @@ describe('initializeAttrs', () => {
3335 } )
3436
3537 it ( 'prioritises the value in the attribute over the property' , ( ) => {
36- const instance = document . createElement ( 'initialize-attr-test-element' )
3738 instance . foo = 'goodbye'
3839 instance . setAttribute ( 'data-foo' , 'hello' )
3940 initializeAttrs ( instance , [ 'foo' ] )
@@ -44,7 +45,6 @@ describe('initializeAttrs', () => {
4445
4546 describe ( 'types' , ( ) => {
4647 it ( 'infers number types from property and casts as number always' , ( ) => {
47- const instance = document . createElement ( 'initialize-attr-test-element' )
4848 instance . foo = 1
4949 initializeAttrs ( instance , [ 'foo' ] )
5050 expect ( instance . foo ) . to . equal ( 1 )
@@ -63,7 +63,6 @@ describe('initializeAttrs', () => {
6363 } )
6464
6565 it ( 'infers boolean types from property and uses has/toggleAttribute' , ( ) => {
66- const instance = document . createElement ( 'initialize-attr-test-element' )
6766 instance . foo = false
6867 initializeAttrs ( instance , [ 'foo' ] )
6968 expect ( instance . foo ) . to . equal ( false )
@@ -86,7 +85,6 @@ describe('initializeAttrs', () => {
8685 } )
8786
8887 it ( 'defaults to inferring string type for non-boolean non-number types' , ( ) => {
89- const instance = document . createElement ( 'initialize-attr-test-element' )
9088 instance . foo = / ^ a r e g e x p $ /
9189 initializeAttrs ( instance , [ 'foo' ] )
9290 expect ( instance . foo ) . to . equal ( '/^a regexp$/' )
@@ -97,23 +95,20 @@ describe('initializeAttrs', () => {
9795
9896 describe ( 'naming' , ( ) => {
9997 it ( 'converts camel cased property names to their HTML dasherized equivalents' , ( ) => {
100- const instance = document . createElement ( 'initialize-attr-test-element' )
10198 initializeAttrs ( instance , [ 'fooBarBazBing' ] )
10299 expect ( instance . fooBarBazBing ) . to . equal ( '' )
103100 instance . fooBarBazBing = 'bar'
104101 expect ( instance . getAttributeNames ( ) ) . to . eql ( [ 'data-foo-bar-baz-bing' ] )
105102 } )
106103
107104 it ( 'will intuitively dasherize acryonyms' , ( ) => {
108- const instance = document . createElement ( 'initialize-attr-test-element' )
109105 initializeAttrs ( instance , [ 'URLBar' ] )
110106 expect ( instance . URLBar ) . to . equal ( '' )
111107 instance . URLBar = 'bar'
112108 expect ( instance . getAttributeNames ( ) ) . to . eql ( [ 'data-url-bar' ] )
113109 } )
114110
115111 it ( 'dasherizes cap suffixed names correctly' , ( ) => {
116- const instance = document . createElement ( 'initialize-attr-test-element' )
117112 initializeAttrs ( instance , [ 'ClipX' ] )
118113 expect ( instance . ClipX ) . to . equal ( '' )
119114 instance . ClipX = 'bar'
@@ -127,8 +122,11 @@ describe('initializeAttrs', () => {
127122 }
128123 customElements . define ( 'class-field-attr-test-element' , ClassFieldAttrTestElement )
129124
125+ beforeEach ( async ( ) => {
126+ instance = await fixture ( html `< class-field-attr-test-element /> ` )
127+ } )
128+
130129 it ( 'overrides any getters assigned in constructor (like class fields)' , ( ) => {
131- const instance = document . createElement ( 'class-field-attr-test-element' )
132130 initializeAttrs ( instance , [ 'foo' ] )
133131 instance . foo = 2
134132 expect ( instance . foo ) . to . equal ( 2 )
@@ -138,14 +136,12 @@ describe('initializeAttrs', () => {
138136 } )
139137
140138 it ( 'defaults to class field value attribute not present' , ( ) => {
141- const instance = document . createElement ( 'class-field-attr-test-element' )
142139 initializeAttrs ( instance , [ 'foo' ] )
143140 expect ( instance . foo ) . to . equal ( 1 )
144141 expect ( instance . getAttribute ( 'data-foo' ) ) . to . equal ( '1' )
145142 } )
146143
147144 it ( 'ignores class field value if element has attribute already' , ( ) => {
148- const instance = document . createElement ( 'class-field-attr-test-element' )
149145 instance . setAttribute ( 'data-foo' , '2' )
150146 initializeAttrs ( instance , [ 'foo' ] )
151147 expect ( instance . foo ) . to . equal ( 2 )
@@ -166,8 +162,12 @@ describe('attr', () => {
166162 }
167163 window . customElements . define ( 'extended-attr-test-element' , ExtendedAttrTestElement )
168164
165+ let instance
166+ beforeEach ( async ( ) => {
167+ instance = await fixture ( html `< attr-test-element /> ` )
168+ } )
169+
169170 it ( 'populates the "default" list for initializeAttrs' , ( ) => {
170- const instance = document . createElement ( 'attr-test-element' )
171171 instance . foo = 'hello'
172172 initializeAttrs ( instance )
173173 expect ( instance ) . to . have . property ( 'foo' , 'hello' )
@@ -177,8 +177,8 @@ describe('attr', () => {
177177 expect ( instance . getAttribute ( 'data-bar' ) ) . to . equal ( '' )
178178 } )
179179
180- it ( 'includes attrs from extended elements' , ( ) => {
181- const instance = document . createElement ( ' extended-attr-test-element' )
180+ it ( 'includes attrs from extended elements' , async ( ) => {
181+ instance = await fixture ( html ` < extended-attr-test-element /> ` )
182182 instance . bar = 'hello'
183183 instance . baz = 'world'
184184 initializeAttrs ( instance )
@@ -191,8 +191,8 @@ describe('attr', () => {
191191 expect ( instance . getAttribute ( 'data-baz' ) ) . to . equal ( 'world' )
192192 } )
193193
194- it ( 'can be initialized multiple times without error' , ( ) => {
195- const instance = document . createElement ( ' initialize-attr-test-element' )
194+ it ( 'can be initialized multiple times without error' , async ( ) => {
195+ instance = await fixture ( html ` < initialize-attr-test-element /> ` )
196196 expect ( instance ) . to . not . have . ownPropertyDescriptor ( 'foo' )
197197 initializeAttrs ( instance , [ 'foo' ] )
198198 expect ( instance ) . to . have . ownPropertyDescriptor ( 'foo' )
0 commit comments