12
12
* http://polymer.github.io/PATENTS.txt
13
13
*/
14
14
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' ;
17
17
import { generateElementName } from '../test-helpers.js' ;
18
18
19
19
// tslint:disable:no-any ok in tests
@@ -1429,36 +1429,39 @@ suite('UpdatingElement', () => {
1429
1429
assert . equal ( el . updatedText , '6' ) ;
1430
1430
} ) ;
1431
1431
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 = '' ;
1441
1443
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
+ }
1449
1451
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
+ } ) ;
1462
1465
1463
1466
test (
1464
1467
'setting properties in update reflects to attribute and is included in `changedProperties`' ,
@@ -1802,68 +1805,62 @@ suite('UpdatingElement', () => {
1802
1805
assert . equal ( sub . getAttribute ( 'foo' ) , '5' ) ;
1803
1806
} ) ;
1804
1807
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 ) ;
1829
1821
}
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 ) ;
1841
1829
}
1830
+ }
1831
+ }
1832
+ } ;
1842
1833
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
+ }
1845
1841
1846
- @property ( { type : String } )
1847
- bar ?: string = 'bar' ;
1848
- }
1842
+ @property ( ) foo = 5 ;
1849
1843
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
+ }
1863
1846
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
+ } ) ;
1865
1860
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 {
1867
1864
validator ?: ( value : any ) => TypeHint ;
1868
1865
observer ?: ( oldValue : TypeHint ) => void ;
1869
1866
}
@@ -1876,18 +1873,21 @@ suite('UpdatingElement', () => {
1876
1873
1877
1874
@customElement ( generateElementName ( ) )
1878
1875
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 ) ;
1882
1881
return {
1883
1882
get : defaultDescriptor . get ,
1884
1883
set ( this : E , value : unknown ) {
1885
1884
const oldValue =
1886
- ( this as unknown as { [ key : string ] : unknown } ) [ name as string ] ;
1885
+ ( this as unknown as { [ key : string ] : unknown } ) [ name as string ] ;
1887
1886
if ( options . validator ) {
1888
1887
value = options . validator ( value ) ;
1889
1888
}
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 ;
1891
1891
( this as unknown as UpdatingElement ) . requestUpdate ( name , oldValue ) ;
1892
1892
} ,
1893
1893
@@ -1900,7 +1900,7 @@ suite('UpdatingElement', () => {
1900
1900
super . updated ( changedProperties ) ;
1901
1901
changedProperties . forEach ( ( value : unknown , key : PropertyKey ) => {
1902
1902
const options = ( this . constructor as typeof UpdatingElement )
1903
- . getPropertyOptions ( key ) as MyPropertyDeclaration ;
1903
+ . getPropertyOptions ( key ) as MyPropertyDeclaration ;
1904
1904
const observer = options . observer ;
1905
1905
if ( typeof observer === 'function' ) {
1906
1906
observer . call ( this , value ) ;
@@ -1909,11 +1909,13 @@ suite('UpdatingElement', () => {
1909
1909
}
1910
1910
1911
1911
// 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
+ } )
1913
1916
foo = 5 ;
1914
1917
1915
- @property ( { } )
1916
- bar = 'bar' ;
1918
+ @property ( { } ) bar = 'bar' ;
1917
1919
1918
1920
// tslint:disable-next-line:no-any
1919
1921
_observedZot ?: any ;
@@ -1922,7 +1924,11 @@ suite('UpdatingElement', () => {
1922
1924
_observedZot2 ?: any ;
1923
1925
1924
1926
// 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 )
1926
1932
zot = '' ;
1927
1933
1928
1934
zot2 = '' ;
@@ -1933,9 +1939,16 @@ suite('UpdatingElement', () => {
1933
1939
static get properties ( ) : MyPropertyDeclarations {
1934
1940
return {
1935
1941
// 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 ,
1937
1947
// 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
+ }
1939
1952
} ;
1940
1953
}
1941
1954
}
0 commit comments