@@ -5,13 +5,15 @@ import hbs from 'htmlbars-inline-precompile';
55import sinon , { SinonSpy } from 'sinon' ;
66import { TestContext } from 'ember-test-helpers' ;
77import { Sizes } from 'ember-element-query/-private/modifier' ;
8+ import pause from '../helpers/pause' ;
89
910interface TestContextCustom extends TestContext {
1011 callback ?: SinonSpy ;
1112 actualWidth ?: number ;
1213 actualHeight ?: number ;
1314 sizes ?: Sizes ;
1415 prefix ?: string ;
16+ isDisabled ?: boolean ;
1517}
1618
1719module ( 'Integration | Modifier | element-query' , function ( hooks ) {
@@ -75,6 +77,88 @@ module('Integration | Modifier | element-query', function (hooks) {
7577 assert . ok ( true ) ;
7678 } ) ;
7779
80+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
81+ test ( 'does not call the onResize when isDisabled is initially set' , async function ( this : TestContextCustom , assert ) {
82+ let m ;
83+ let callCount = 0 ;
84+ this . callback = sinon . spy ( ( ) => callCount ++ ) ;
85+
86+ await render ( hbs `
87+ {{! template-lint-disable no-inline-styles }}
88+ <div
89+ id="test-subject"
90+ style="width: 300px; height: 100px;"
91+ {{element-query onResize=this.callback isDisabled=true}}
92+ >
93+ </div>
94+ ` ) ;
95+
96+ const element = document . getElementById ( 'test-subject' ) ;
97+ if ( ! element ) throw new Error ( 'Expected element to exist' ) ;
98+
99+ m = 'Element is rendered' ;
100+ assert . ok ( true , m ) ;
101+
102+ await pause ( 500 ) ;
103+
104+ m = 'Call count' ;
105+ assert . equal ( callCount , 0 , m ) ;
106+
107+ m = 'Attr `at-xs` presence' ;
108+ assert . equal ( element . getAttribute ( 'at-xs' ) , null , m ) ;
109+ } ) ;
110+
111+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
112+ test ( 'stops calling the onResize callback after updating isDisabled after rendering' , async function ( this : TestContextCustom , assert ) {
113+ let m ;
114+ let callCount = 0 ;
115+ this . callback = sinon . spy ( ( ) => callCount ++ ) ;
116+ this . set ( 'isDisabled' , false ) ;
117+
118+ await render ( hbs `
119+ {{! template-lint-disable no-inline-styles }}
120+ <div
121+ id="test-subject"
122+ style="width: 300px; height: 100px;"
123+ {{element-query onResize=this.callback isDisabled=this.isDisabled}}
124+ >
125+ </div>
126+ ` ) ;
127+
128+ const element = document . getElementById ( 'test-subject' ) ;
129+ if ( ! element ) throw new Error ( 'Expected element to exist' ) ;
130+
131+ m = 'Element is rendered' ;
132+ assert . ok ( true , m ) ;
133+
134+ await waitUntil ( ( ) => callCount === 1 ) ;
135+
136+ m = 'Callback called once' ;
137+ assert . ok ( true , m ) ;
138+
139+ sinon . assert . calledWithExactly (
140+ this . callback . firstCall ,
141+ sinon . match ( {
142+ element,
143+ width : 300 ,
144+ height : 100 ,
145+ ratio : 3 ,
146+ } )
147+ ) ;
148+
149+ this . set ( 'isDisabled' , true ) ;
150+
151+ element . style . width = '400px' ;
152+
153+ await pause ( 500 ) ;
154+
155+ m = 'Call count' ;
156+ assert . equal ( callCount , 1 , m ) ;
157+
158+ m = 'Attr `at-s` presence' ;
159+ assert . equal ( element . getAttribute ( 'at-s' ) , null , m ) ;
160+ } ) ;
161+
78162 //
79163
80164 // eslint-disable-next-line @typescript-eslint/no-misused-promises
0 commit comments