<commit_before>(function () {

	var span;

	module('events', {
		setup: function () {
			span = $('<span data-ok-button="OK">Q</span>').appendTo($('#qunit-fixture'));
		}
	});

	function click_ok() {
		var ok = span.find('input[type=button]');
		ok.click();
	}

	test('event jinplace:done triggered after finish', function() {
		var all_ok = false;
		span.on('jinplace:done', function (ev, data) {
			all_ok = data;
			console.log('all done', data);
		});
		span.jinplace().click();

		var input = span.find('input');
		input.val('hello');
		click_ok();

		equal(all_ok, 'hello');
	});

	test('failure triggers jinplace:fail event', function() {
		// We need to revert to the real ajax method for this test.
		var saved = $.ajax;
		$.ajax = $.orig_ajax;
		try {
			span = $('<span data-url="/no-such-page" data-ok-button="OK">Q</span>')
					.appendTo($('#qunit-fixture'));

			var d = $.Deferred();

			// Set up handlers
			span.on('jinplace:done', function() {
				d.reject('fail due to successful request');
			});
			span.on('jinplace:fail', function(ev, xhr, textStatus, errorThrown) {
				console.log(errorThrown); // Should be 'Not found' if running on the jinplace.org website
				d.resolve(textStatus);
			});

			span.jinplace().click();
			var input = span.find('input');
			input.val('hello');
			click_ok();
			
			QUnit.stop();
			var t = setTimeout(function () {
				// This is here in case there is no response. The test is failed with
				// a timeout message.
				console.log("after timeout", span.text());
				d.reject('fail due to timeout');
			}, 300);

			// Whatever happens we check the data passed in the resolve or reject call and
			// restart the test runner.
			d.always(function(result) {
				clearTimeout(t);
				equal(result, "error");
				QUnit.start();
			});

		} finally {
			// Restore the mock ajax method.
			$.ajax = saved;
		}
	});

	test('success also triggers jinplace:always event', function() {
		var all_ok = '';
		span.on('jinplace:done', function (ev, data) {
			all_ok = data;
			console.log('all done', data);
		});
		span.on('jinplace:always', function(ev, data) {
			all_ok += ' always ' + data;
		});

		span.jinplace().click();

		var input = span.find('input');

		const changed_data = 'hello';
		input.val(changed_data);
		click_ok();<commit_msg>Change const -> var as not supported in ie6<commit_after>(function () {

	var span;

	module('events', {
		setup: function () {
			span = $('<span data-ok-button="OK">Q</span>').appendTo($('#qunit-fixture'));
		}
	});

	function click_ok() {
		var ok = span.find('input[type=button]');
		ok.click();
	}

	test('event jinplace:done triggered after finish', function() {
		var all_ok = false;
		span.on('jinplace:done', function (ev, data) {
			all_ok = data;
			console.log('all done', data);
		});
		span.jinplace().click();

		var input = span.find('input');
		input.val('hello');
		click_ok();

		equal(all_ok, 'hello');
	});

	test('failure triggers jinplace:fail event', function() {
		// We need to revert to the real ajax method for this test.
		var saved = $.ajax;
		$.ajax = $.orig_ajax;
		try {
			span = $('<span data-url="/no-such-page" data-ok-button="OK">Q</span>')
					.appendTo($('#qunit-fixture'));

			var d = $.Deferred();

			// Set up handlers
			span.on('jinplace:done', function() {
				d.reject('fail due to successful request');
			});
			span.on('jinplace:fail', function(ev, xhr, textStatus, errorThrown) {
				console.log(errorThrown); // Should be 'Not found' if running on the jinplace.org website
				d.resolve(textStatus);
			});

			span.jinplace().click();
			var input = span.find('input');
			input.val('hello');
			click_ok();
			
			QUnit.stop();
			var t = setTimeout(function () {
				// This is here in case there is no response. The test is failed with
				// a timeout message.
				console.log("after timeout", span.text());
				d.reject('fail due to timeout');
			}, 300);

			// Whatever happens we check the data passed in the resolve or reject call and
			// restart the test runner.
			d.always(function(result) {
				clearTimeout(t);
				equal(result, "error");
				QUnit.start();
			});

		} finally {
			// Restore the mock ajax method.
			$.ajax = saved;
		}
	});

	test('success also triggers jinplace:always event', function() {
		var all_ok = '';
		span.on('jinplace:done', function (ev, data) {
			all_ok = data;
			console.log('all done', data);
		});
		span.on('jinplace:always', function(ev, data) {
			all_ok += ' always ' + data;
		});

		span.jinplace().click();

		var input = span.find('input');

		var changed_data = 'hello';
		input.val(changed_data);
		click_ok();
{'fraction_non_alphanumeric': 0.17954251186879586, 'fraction_numerical': 0.0015105740181268882, 'mean_word_length': 3.2679558011049723, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4391387', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1529, 'n_words': 400}