@@ -14,10 +14,11 @@ import {Subject} from 'rxjs';
1414import { createCustomElement , NgElementConstructor } from '../src/create-custom-element' ;
1515import { NgElementStrategy , NgElementStrategyEvent , NgElementStrategyFactory } from '../src/element-strategy' ;
1616
17- type WithFooBar = {
18- fooFoo : string ,
19- barBar : string
20- } ;
17+ interface WithFooBar {
18+ fooFoo : string ;
19+ barBar : string ;
20+ fooTransformed : unknown ;
21+ }
2122
2223describe ( 'createCustomElement' , ( ) => {
2324 let selectorUid = 0 ;
@@ -52,18 +53,20 @@ describe('createCustomElement', () => {
5253 } ) ;
5354
5455 it ( 'should use a default strategy for converting component inputs' , ( ) => {
55- expect ( NgElementCtor . observedAttributes ) . toEqual ( [ 'foo-foo' , 'barbar' ] ) ;
56+ expect ( NgElementCtor . observedAttributes ) . toEqual ( [ 'foo-foo' , 'barbar' , 'foo-transformed' ] ) ;
5657 } ) ;
5758
5859 it ( 'should send input values from attributes when connected' , ( ) => {
5960 const element = new NgElementCtor ( injector ) ;
6061 element . setAttribute ( 'foo-foo' , 'value-foo-foo' ) ;
6162 element . setAttribute ( 'barbar' , 'value-barbar' ) ;
63+ element . setAttribute ( 'foo-transformed' , 'truthy' ) ;
6264 element . connectedCallback ( ) ;
6365 expect ( strategy . connectedElement ) . toBe ( element ) ;
6466
6567 expect ( strategy . getInputValue ( 'fooFoo' ) ) . toBe ( 'value-foo-foo' ) ;
6668 expect ( strategy . getInputValue ( 'barBar' ) ) . toBe ( 'value-barbar' ) ;
69+ expect ( strategy . getInputValue ( 'fooTransformed' ) ) . toBe ( true ) ;
6770 } ) ;
6871
6972 it ( 'should work even if the constructor is not called (due to polyfill)' , ( ) => {
@@ -78,11 +81,13 @@ describe('createCustomElement', () => {
7881
7982 element . setAttribute ( 'foo-foo' , 'value-foo-foo' ) ;
8083 element . setAttribute ( 'barbar' , 'value-barbar' ) ;
84+ element . setAttribute ( 'foo-transformed' , 'truthy' ) ;
8185 element . connectedCallback ( ) ;
8286
8387 expect ( strategy . connectedElement ) . toBe ( element ) ;
8488 expect ( strategy . getInputValue ( 'fooFoo' ) ) . toBe ( 'value-foo-foo' ) ;
8589 expect ( strategy . getInputValue ( 'barBar' ) ) . toBe ( 'value-barbar' ) ;
90+ expect ( strategy . getInputValue ( 'fooTransformed' ) ) . toBe ( true ) ;
8691 } ) ;
8792
8893 it ( 'should listen to output events after connected' , ( ) => {
@@ -154,9 +159,11 @@ describe('createCustomElement', () => {
154159 const element = new NgElementCtor ( injector ) ;
155160 element . fooFoo = 'foo-foo-value' ;
156161 element . barBar = 'barBar-value' ;
162+ element . fooTransformed = 'truthy' ;
157163
158164 expect ( strategy . inputs . get ( 'fooFoo' ) ) . toBe ( 'foo-foo-value' ) ;
159165 expect ( strategy . inputs . get ( 'barBar' ) ) . toBe ( 'barBar-value' ) ;
166+ expect ( strategy . inputs . get ( 'fooTransformed' ) ) . toBe ( true ) ;
160167 } ) ;
161168
162169 it ( 'should properly handle getting/setting properties on the element even if the constructor is not called' ,
@@ -170,9 +177,11 @@ describe('createCustomElement', () => {
170177
171178 element . fooFoo = 'foo-foo-value' ;
172179 element . barBar = 'barBar-value' ;
180+ element . fooTransformed = 'truthy' ;
173181
174182 expect ( strategy . inputs . get ( 'fooFoo' ) ) . toBe ( 'foo-foo-value' ) ;
175183 expect ( strategy . inputs . get ( 'barBar' ) ) . toBe ( 'barBar-value' ) ;
184+ expect ( strategy . inputs . get ( 'fooTransformed' ) ) . toBe ( true ) ;
176185 } ) ;
177186
178187 it ( 'should capture properties set before upgrading the element' , ( ) => {
@@ -181,18 +190,22 @@ describe('createCustomElement', () => {
181190 const element = Object . assign ( document . createElement ( selector ) , {
182191 fooFoo : 'foo-prop-value' ,
183192 barBar : 'bar-prop-value' ,
193+ fooTransformed : 'truthy' as unknown ,
184194 } ) ;
185195 expect ( element . fooFoo ) . toBe ( 'foo-prop-value' ) ;
186196 expect ( element . barBar ) . toBe ( 'bar-prop-value' ) ;
197+ expect ( element . fooTransformed ) . toBe ( 'truthy' ) ;
187198
188199 // Upgrade the element to a Custom Element and insert it into the DOM.
189200 customElements . define ( selector , ElementCtor ) ;
190201 testContainer . appendChild ( element ) ;
191202 expect ( element . fooFoo ) . toBe ( 'foo-prop-value' ) ;
192203 expect ( element . barBar ) . toBe ( 'bar-prop-value' ) ;
204+ expect ( element . fooTransformed ) . toBe ( true ) ;
193205
194206 expect ( strategy . inputs . get ( 'fooFoo' ) ) . toBe ( 'foo-prop-value' ) ;
195207 expect ( strategy . inputs . get ( 'barBar' ) ) . toBe ( 'bar-prop-value' ) ;
208+ expect ( strategy . inputs . get ( 'fooTransformed' ) ) . toBe ( true ) ;
196209 } ) ;
197210
198211 it ( 'should capture properties set after upgrading the element but before inserting it into the DOM' ,
@@ -202,25 +215,31 @@ describe('createCustomElement', () => {
202215 const element = Object . assign ( document . createElement ( selector ) , {
203216 fooFoo : 'foo-prop-value' ,
204217 barBar : 'bar-prop-value' ,
218+ fooTransformed : 'truthy' as unknown ,
205219 } ) ;
206220 expect ( element . fooFoo ) . toBe ( 'foo-prop-value' ) ;
207221 expect ( element . barBar ) . toBe ( 'bar-prop-value' ) ;
222+ expect ( element . fooTransformed ) . toBe ( 'truthy' ) ;
208223
209224 // Upgrade the element to a Custom Element (without inserting it into the DOM) and update a
210225 // property.
211226 customElements . define ( selector , ElementCtor ) ;
212227 customElements . upgrade ( element ) ;
213228 element . barBar = 'bar-prop-value-2' ;
229+ element . fooTransformed = '' ;
214230 expect ( element . fooFoo ) . toBe ( 'foo-prop-value' ) ;
215231 expect ( element . barBar ) . toBe ( 'bar-prop-value-2' ) ;
232+ expect ( element . fooTransformed ) . toBe ( '' ) ;
216233
217234 // Insert the element into the DOM.
218235 testContainer . appendChild ( element ) ;
219236 expect ( element . fooFoo ) . toBe ( 'foo-prop-value' ) ;
220237 expect ( element . barBar ) . toBe ( 'bar-prop-value-2' ) ;
238+ expect ( element . fooTransformed ) . toBe ( false ) ;
221239
222240 expect ( strategy . inputs . get ( 'fooFoo' ) ) . toBe ( 'foo-prop-value' ) ;
223241 expect ( strategy . inputs . get ( 'barBar' ) ) . toBe ( 'bar-prop-value-2' ) ;
242+ expect ( strategy . inputs . get ( 'fooTransformed' ) ) . toBe ( false ) ;
224243 } ) ;
225244
226245 it ( 'should allow overwriting properties with attributes after upgrading the element but before inserting it into the DOM' ,
@@ -230,25 +249,31 @@ describe('createCustomElement', () => {
230249 const element = Object . assign ( document . createElement ( selector ) , {
231250 fooFoo : 'foo-prop-value' ,
232251 barBar : 'bar-prop-value' ,
252+ fooTransformed : 'truthy' as unknown ,
233253 } ) ;
234254 expect ( element . fooFoo ) . toBe ( 'foo-prop-value' ) ;
235255 expect ( element . barBar ) . toBe ( 'bar-prop-value' ) ;
256+ expect ( element . fooTransformed ) . toBe ( 'truthy' ) ;
236257
237258 // Upgrade the element to a Custom Element (without inserting it into the DOM) and set an
238259 // attribute.
239260 customElements . define ( selector , ElementCtor ) ;
240261 customElements . upgrade ( element ) ;
241262 element . setAttribute ( 'barbar' , 'bar-attr-value' ) ;
263+ element . setAttribute ( 'foo-transformed' , '' ) ;
242264 expect ( element . fooFoo ) . toBe ( 'foo-prop-value' ) ;
243265 expect ( element . barBar ) . toBe ( 'bar-attr-value' ) ;
266+ expect ( element . fooTransformed ) . toBe ( false ) ;
244267
245268 // Insert the element into the DOM.
246269 testContainer . appendChild ( element ) ;
247270 expect ( element . fooFoo ) . toBe ( 'foo-prop-value' ) ;
248271 expect ( element . barBar ) . toBe ( 'bar-attr-value' ) ;
272+ expect ( element . fooTransformed ) . toBe ( false ) ;
249273
250274 expect ( strategy . inputs . get ( 'fooFoo' ) ) . toBe ( 'foo-prop-value' ) ;
251275 expect ( strategy . inputs . get ( 'barBar' ) ) . toBe ( 'bar-attr-value' ) ;
276+ expect ( strategy . inputs . get ( 'fooTransformed' ) ) . toBe ( false ) ;
252277 } ) ;
253278
254279 // Helpers
@@ -274,6 +299,7 @@ describe('createCustomElement', () => {
274299 class TestComponent {
275300 @Input ( ) fooFoo : string = 'foo' ;
276301 @Input ( 'barbar' ) barBar ! : string ;
302+ @Input ( { transform : ( value : unknown ) => ! ! value } ) fooTransformed ! : boolean ;
277303
278304 @Output ( ) bazBaz = new EventEmitter < boolean > ( ) ;
279305 @Output ( 'quxqux' ) quxQux = new EventEmitter < Object > ( ) ;
@@ -306,8 +332,8 @@ describe('createCustomElement', () => {
306332 return this . inputs . get ( propName ) ;
307333 }
308334
309- setInputValue ( propName : string , value : string ) : void {
310- this . inputs . set ( propName , value ) ;
335+ setInputValue ( propName : string , value : string , transform ?: ( value : any ) => any ) : void {
336+ this . inputs . set ( propName , transform ? transform ( value ) : value ) ;
311337 }
312338
313339 reset ( ) : void {
0 commit comments