1212 * http://polymer.github.io/PATENTS.txt
1313 */
1414
15- import { property , customElement } from '../../lib/decorators.js' ;
16- import { ComplexAttributeConverter , PropertyDeclarations , PropertyValues , UpdatingElement , PropertyDeclaration , defaultConverter } from '../../lib/updating-element.js' ;
15+ import { customElement , property } from '../../lib/decorators.js' ;
16+ import { ComplexAttributeConverter , defaultConverter , PropertyDeclaration , PropertyDeclarations , PropertyValues , UpdatingElement } from '../../lib/updating-element.js' ;
1717import { generateElementName } from '../test-helpers.js' ;
1818
1919// tslint:disable:no-any ok in tests
@@ -1429,36 +1429,39 @@ suite('UpdatingElement', () => {
14291429 assert . equal ( el . updatedText , '6' ) ;
14301430 } ) ;
14311431
1432- test ( 'setting properties in update after calling `super.update` *does* trigger update' , async ( ) => {
1433- class E extends UpdatingElement {
1434- static get properties ( ) {
1435- return { foo : { } } ;
1436- }
1437- promiseFulfilled = false ;
1438- foo = 0 ;
1439- updateCount = 0 ;
1440- updatedText = '' ;
1432+ test (
1433+ 'setting properties in update after calling `super.update` *does* trigger update' ,
1434+ async ( ) => {
1435+ class E extends UpdatingElement {
1436+ static get properties ( ) {
1437+ return { foo : { } } ;
1438+ }
1439+ promiseFulfilled = false ;
1440+ foo = 0 ;
1441+ updateCount = 0 ;
1442+ updatedText = '' ;
14411443
1442- update ( props : PropertyValues ) {
1443- this . updateCount ++ ;
1444- super . update ( props ) ;
1445- if ( this . foo < 1 ) {
1446- this . foo ++ ;
1447- }
1448- }
1444+ update ( props : PropertyValues ) {
1445+ this . updateCount ++ ;
1446+ super . update ( props ) ;
1447+ if ( this . foo < 1 ) {
1448+ this . foo ++ ;
1449+ }
1450+ }
14491451
1450- updated ( ) {
1451- this . updatedText = `${ this . foo } ` ;
1452- }
1453- }
1454- customElements . define ( generateElementName ( ) , E ) ;
1455- const el = new E ( ) ;
1456- container . appendChild ( el ) ;
1457- while ( ! ( await el . updateComplete ) ) { }
1458- assert . equal ( el . foo , 1 ) ;
1459- assert . equal ( el . updateCount , 2 ) ;
1460- assert . equal ( el . updatedText , '1' ) ;
1461- } ) ;
1452+ updated ( ) {
1453+ this . updatedText = `${ this . foo } ` ;
1454+ }
1455+ }
1456+ customElements . define ( generateElementName ( ) , E ) ;
1457+ const el = new E ( ) ;
1458+ container . appendChild ( el ) ;
1459+ while ( ! ( await el . updateComplete ) ) {
1460+ }
1461+ assert . equal ( el . foo , 1 ) ;
1462+ assert . equal ( el . updateCount , 2 ) ;
1463+ assert . equal ( el . updatedText , '1' ) ;
1464+ } ) ;
14621465
14631466 test (
14641467 'setting properties in update reflects to attribute and is included in `changedProperties`' ,
@@ -1802,68 +1805,62 @@ suite('UpdatingElement', () => {
18021805 assert . equal ( sub . getAttribute ( 'foo' ) , '5' ) ;
18031806 } ) ;
18041807
1805- test ( 'can provide a default property declaration' , async ( ) => {
1806-
1807- const SpecialNumber = { } ;
1808-
1809- const myPropertyDeclaration = {
1810- type : SpecialNumber ,
1811- reflect : true ,
1812- converter : {
1813- toAttribute : function ( value : unknown , type ?: unknown ) : unknown {
1814- switch ( type ) {
1815- case String :
1816- return value === undefined ? null : value ;
1817- default :
1818- return defaultConverter . toAttribute ! ( value , type ) ;
1819- }
1820- } ,
1821- fromAttribute : function ( value : string | null , type ?: unknown ) {
1822- switch ( type ) {
1823- case SpecialNumber :
1824- return Number ( value ) + 10 ;
1825- default :
1826- return defaultConverter . fromAttribute ! ( value , type ) ;
1827- }
1828- }
1808+ test ( 'can provide a default property declaration' , async ( ) => {
1809+ const SpecialNumber = { } ;
1810+
1811+ const myPropertyDeclaration = {
1812+ type : SpecialNumber ,
1813+ reflect : true ,
1814+ converter : {
1815+ toAttribute : function ( value : unknown , type ?: unknown ) : unknown {
1816+ switch ( type ) {
1817+ case String :
1818+ return value === undefined ? null : value ;
1819+ default :
1820+ return defaultConverter . toAttribute ! ( value , type ) ;
18291821 }
1830- } ;
1831-
1832- @customElement ( generateElementName ( ) )
1833- class E extends UpdatingElement {
1834-
1835- static createProperty (
1836- name : PropertyKey ,
1837- options : PropertyDeclaration ) {
1838- // Always mix into defaults to preserve custom converter.
1839- options = Object . assign ( Object . create ( myPropertyDeclaration ) , options ) ;
1840- super . createProperty ( name , options ) ;
1822+ } ,
1823+ fromAttribute : function ( value : string | null , type ?: unknown ) {
1824+ switch ( type ) {
1825+ case SpecialNumber :
1826+ return Number ( value ) + 10 ;
1827+ default :
1828+ return defaultConverter . fromAttribute ! ( value , type ) ;
18411829 }
1830+ }
1831+ }
1832+ } ;
18421833
1843- @property ( )
1844- foo = 5 ;
1834+ @customElement ( generateElementName ( ) )
1835+ class E extends UpdatingElement {
1836+ static createProperty ( name : PropertyKey , options : PropertyDeclaration ) {
1837+ // Always mix into defaults to preserve custom converter.
1838+ options = Object . assign ( Object . create ( myPropertyDeclaration ) , options ) ;
1839+ super . createProperty ( name , options ) ;
1840+ }
18451841
1846- @property ( { type : String } )
1847- bar ?: string = 'bar' ;
1848- }
1842+ @property ( ) foo = 5 ;
18491843
1850- const el = new E ( ) ;
1851- container . appendChild ( el ) ;
1852- el . setAttribute ( 'foo' , '10' ) ;
1853- el . setAttribute ( 'bar' , 'attrBar' ) ;
1854- await el . updateComplete ;
1855- assert . equal ( el . foo , 20 ) ;
1856- assert . equal ( el . bar , 'attrBar' ) ;
1857- el . foo = 5 ;
1858- el . bar = undefined ;
1859- await el . updateComplete ;
1860- assert . equal ( el . getAttribute ( 'foo' ) , '5' ) ;
1861- assert . isFalse ( el . hasAttribute ( 'bar' ) ) ;
1862- } ) ;
1844+ @property ( { type : String } ) bar ?: string = 'bar' ;
1845+ }
18631846
1864- test ( 'can customize property options and accessor creation' , async ( ) => {
1847+ const el = new E ( ) ;
1848+ container . appendChild ( el ) ;
1849+ el . setAttribute ( 'foo' , '10' ) ;
1850+ el . setAttribute ( 'bar' , 'attrBar' ) ;
1851+ await el . updateComplete ;
1852+ assert . equal ( el . foo , 20 ) ;
1853+ assert . equal ( el . bar , 'attrBar' ) ;
1854+ el . foo = 5 ;
1855+ el . bar = undefined ;
1856+ await el . updateComplete ;
1857+ assert . equal ( el . getAttribute ( 'foo' ) , '5' ) ;
1858+ assert . isFalse ( el . hasAttribute ( 'bar' ) ) ;
1859+ } ) ;
18651860
1866- interface MyPropertyDeclaration < TypeHint = unknown > extends PropertyDeclaration {
1861+ test ( 'can customize property options and accessor creation' , async ( ) => {
1862+ interface MyPropertyDeclaration < TypeHint = unknown > extends
1863+ PropertyDeclaration {
18671864 validator ?: ( value : any ) => TypeHint ;
18681865 observer ?: ( oldValue : TypeHint ) => void ;
18691866 }
@@ -1876,18 +1873,21 @@ suite('UpdatingElement', () => {
18761873
18771874 @customElement ( generateElementName ( ) )
18781875 class E extends UpdatingElement {
1879-
1880- static getPropertyDescriptor ( name : PropertyKey , key : string | symbol , options : MyPropertyDeclaration ) {
1881- const defaultDescriptor = super . getPropertyDescriptor ( name , key , options ) ;
1876+ static getPropertyDescriptor (
1877+ name : PropertyKey , key : string | symbol ,
1878+ options : MyPropertyDeclaration ) {
1879+ const defaultDescriptor =
1880+ super . getPropertyDescriptor ( name , key , options ) ;
18821881 return {
18831882 get : defaultDescriptor . get ,
18841883 set ( this : E , value : unknown ) {
18851884 const oldValue =
1886- ( this as unknown as { [ key : string ] : unknown } ) [ name as string ] ;
1885+ ( this as unknown as { [ key : string ] : unknown } ) [ name as string ] ;
18871886 if ( options . validator ) {
18881887 value = options . validator ( value ) ;
18891888 }
1890- ( this as unknown as { [ key : string ] : unknown } ) [ key as string ] = value ;
1889+ ( this as unknown as { [ key : string ] : unknown } ) [ key as string ] =
1890+ value ;
18911891 ( this as unknown as UpdatingElement ) . requestUpdate ( name , oldValue ) ;
18921892 } ,
18931893
@@ -1900,7 +1900,7 @@ suite('UpdatingElement', () => {
19001900 super . updated ( changedProperties ) ;
19011901 changedProperties . forEach ( ( value : unknown , key : PropertyKey ) => {
19021902 const options = ( this . constructor as typeof UpdatingElement )
1903- . getPropertyOptions ( key ) as MyPropertyDeclaration ;
1903+ . getPropertyOptions ( key ) as MyPropertyDeclaration ;
19041904 const observer = options . observer ;
19051905 if ( typeof observer === 'function' ) {
19061906 observer . call ( this , value ) ;
@@ -1909,11 +1909,13 @@ suite('UpdatingElement', () => {
19091909 }
19101910
19111911 // provide custom deorator expecting extended type
1912- @myProperty ( { type : Number , validator : ( value : number ) => Math . min ( 10 , Math . max ( value , 0 ) ) } )
1912+ @myProperty ( {
1913+ type : Number ,
1914+ validator : ( value : number ) => Math . min ( 10 , Math . max ( value , 0 ) )
1915+ } )
19131916 foo = 5 ;
19141917
1915- @property ( { } )
1916- bar = 'bar' ;
1918+ @property ( { } ) bar = 'bar' ;
19171919
19181920 // tslint:disable-next-line:no-any
19191921 _observedZot ?: any ;
@@ -1922,7 +1924,11 @@ suite('UpdatingElement', () => {
19221924 _observedZot2 ?: any ;
19231925
19241926 // use regular decorator and cast to type
1925- @property ( { observer : function ( this : E , oldValue : string ) { this . _observedZot = { value : this . zot , oldValue} ; } } as PropertyDeclaration )
1927+ @property ( {
1928+ observer : function ( this : E , oldValue : string ) {
1929+ this . _observedZot = { value : this . zot , oldValue} ;
1930+ }
1931+ } as PropertyDeclaration )
19261932 zot = '' ;
19271933
19281934 zot2 = '' ;
@@ -1933,9 +1939,16 @@ suite('UpdatingElement', () => {
19331939 static get properties ( ) : MyPropertyDeclarations {
19341940 return {
19351941 // object cast as type
1936- zot2 : { observer : function ( this : E , oldValue : string ) { this . _observedZot2 = { value : this . zot2 , oldValue} ; } } as PropertyDeclaration ,
1942+ zot2 : {
1943+ observer : function ( this : E , oldValue : string ) {
1944+ this . _observedZot2 = { value : this . zot2 , oldValue} ;
1945+ }
1946+ } as PropertyDeclaration ,
19371947 // object satisfying defined custom type.
1938- foo2 : { type : Number , validator : ( value : number ) => Math . min ( 10 , Math . max ( value , 0 ) ) }
1948+ foo2 : {
1949+ type : Number ,
1950+ validator : ( value : number ) => Math . min ( 10 , Math . max ( value , 0 ) )
1951+ }
19391952 } ;
19401953 }
19411954 }
0 commit comments