Icontem

File: test/core/polling.js

Recommend this page to a friend!
  Classes of Sebastian Schlapkohl  >  jQuery Annex  >  test/core/polling.js  >  Download  
File: test/core/polling.js
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: jQuery Annex
General purpose functions missing in jQuery
Author: By
Last change:
Date: 7 months ago
Size: 1,801 bytes
 

Contents

Class file image Download
import test from 'ava';



$versions.forEach($ => {
	let jqueryVersionString = ' @ jQuery '+$().jquery;



	test.cb('$.poll'+jqueryVersionString, (assert) => {
		let foo = 0,
			bar = 0,
			firedCounter = 0,
			changedCounter = 0,
			elseCounter = 0,
			elseChangedCounter = 0;

		$.poll(
			'foo-poll',
			function(){
				return (foo > 0) && (foo < 3);
			},
			function(changed){
				firedCounter++;
				if( changed ){
					changedCounter++;
				}
			},
			function(changed){
				elseCounter++;
				if( changed ){
					elseChangedCounter++;
				}
			},
			240
		);

		$.poll(
			'bar-poll',
			function(){
				return foo > 0;
			},
			function(changed){
				bar++;
				if( foo > 1 ){
					return true;
				}
			},
			$.noop,
			110,
			true
		);

		window.setTimeout(function(){
			foo++;
		}, 250);

		window.setTimeout(function(){
			foo++;
		}, 500);

		window.setTimeout(function(){
			foo++;
		}, 750);

		window.setTimeout(function(){
			assert.is(foo, 3);
			assert.is(firedCounter, 2);
			assert.is(changedCounter, 1);
			assert.is(elseCounter, 6);
			assert.is(elseChangedCounter, 1);
			assert.is(bar, 3);
			assert.end();
		}, 2000);
	});



	test.cb('$.unpoll'+jqueryVersionString, (assert) => {
		let foo = 0,
			bar = false,
			foobar = false,
			boo = 0;

		$.poll(
			'foo-poll2',
			function(){
				return (foo > 0) && (foo < 3);
			},
			function(){
				boo++;
			},
			$.noop,
			240
		);

		window.setTimeout(function(){
			foo++;
			bar = $.unpoll('bar-poll2');
		}, 250);

		window.setTimeout(function(){
			foobar = $.unpoll('foo-poll2');
			foo++;
		}, 500);

		window.setTimeout(function(){
			foo++;
		}, 750);

		window.setTimeout(function(){
			assert.is(foo, 3);
			assert.is(boo, 1);
			assert.false(bar);
			assert.true(foobar);
			assert.end();
		}, 2000);
	});

});