@@ -46,6 +46,9 @@ export class Component {
4646 this . data = args . data ;
4747 this . syncUrl = `${ this . messageUrl } /${ this . name } ` ;
4848
49+ this . document = args . document || document ;
50+ this . walker = args . walker || walk ;
51+
4952 this . root = undefined ;
5053 this . modelEls = [ ] ;
5154 this . errors = { } ;
@@ -64,7 +67,7 @@ export class Component {
6467 * Initializes the Component.
6568 */
6669 init ( ) {
67- this . root = $ ( `[unicorn\\:id="${ this . id } "]` ) ;
70+ this . root = $ ( `[unicorn\\:id="${ this . id } "]` , this . document ) ;
6871
6972 if ( ! this . root ) {
7073 throw Error ( "No id found" ) ;
@@ -79,7 +82,7 @@ export class Component {
7982 * events when attached directly to the element.
8083 */
8184 addActionEventListener ( eventType ) {
82- document . addEventListener ( eventType , ( event ) => {
85+ this . document . addEventListener ( eventType , ( event ) => {
8386 const targetElement = new Element ( event . target ) ;
8487
8588 if ( targetElement && targetElement . isUnicorn && targetElement . actions . length > 0 ) {
@@ -160,7 +163,7 @@ export class Component {
160163 refreshEventListeners ( ) {
161164 this . actionEvents = { } ;
162165
163- walk ( this . root , ( el ) => {
166+ this . walker ( this . root , ( el ) => {
164167 if ( el . isSameNode ( this . root ) ) {
165168 // Skip the component root element
166169 return ;
@@ -220,8 +223,8 @@ export class Component {
220223 this . poll = rootElement . poll ;
221224 this . poll . timer = null ;
222225
223- document . addEventListener ( "visibilitychange" , ( ) => {
224- if ( document . hidden ) {
226+ this . document . addEventListener ( "visibilitychange" , ( ) => {
227+ if ( this . document . hidden ) {
225228 if ( this . poll . timer ) {
226229 clearInterval ( this . poll . timer ) ;
227230 }
@@ -239,13 +242,14 @@ export class Component {
239242 */
240243 startPolling ( ) {
241244 this . poll . timer = null ;
245+ const { timer } = this . poll ;
242246
243247 function handleError ( err ) {
244248 if ( err ) {
245249 console . error ( err ) ;
246250 }
247- if ( this . poll . timer ) {
248- clearInterval ( this . poll . timer ) ;
251+ if ( timer ) {
252+ clearInterval ( timer ) ;
249253 }
250254 }
251255
0 commit comments