Jump to content

User:Evad37/TextDiff/test.js: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
+1
 
(No difference)

Latest revision as of 07:34, 31 December 2017

$( function($) {
// Only load on script's pages
if ( mw.config.get('wgPageName').indexOf(':Evad37/TextDiff') === -1 ) {
	return;
}

// Create (hidden) output div, if needed
$('<div>').attr('id', 'qunit').hide().insertBefore('#firstHeading');

// Load QUnit
mw.loader.load( 'https://code.jquery.com/qunit/qunit-2.4.1.css', 'text/css' );

/* ========== Unit tests ======================================================================== */
var runTests = function() {
$.getScript( 'https://code.jquery.com/qunit/qunit-2.4.1.js', function() {
	
// Show output div
$('#qunit').empty().show();

// Import vars
var config = window.testTextDiff.config;
var api = window.testTextDiff.api;
var normaliseIds = window.testTextDiff.normaliseIds;
var lookupIdsFromRelation = window.testTextDiff.lookupIdsFromRelation;
var makeText = window.testTextDiff.makeText;
var grabWikitext = window.testTextDiff.grabWikitext;
var toPlaintext = window.testTextDiff.toPlaintext;
var makeDiff = window.testTextDiff.makeDiff;
var showDiff = window.testTextDiff.showDiff;
var doTextDiff = window.testTextDiff.doTextDiff;

/* ---------- normaliseIds ---------------------------------------------------------------------- */
QUnit.module( "normaliseIds");

QUnit.test( "Two numeric ids", function( assert ) {
	var done = assert.async();
	normaliseIds('11', '22', 'foo').always(function(ids) {
		assert.ok(ids, 'There is a return value');
		assert.equal(ids.from, '22', 'oldid becomes "from"');
		assert.equal(ids.to, '11', 'diff becomes "to"');
		done();
	});
});

QUnit.test( "Numeric oldid + prev", function( assert ) {
	var done = assert.async();
	normaliseIds('prev', '432016865', 'foo').always(function(ids) {
		console.log("normaliseIds('prev', '432016865', 'foo').always(function(ids)\n --> ids = "); console.log(ids);
		assert.ok(ids, 'There is a return value');
		assert.ok($.isNumeric(ids.from), '"from" is numeric');
		assert.ok($.isNumeric(ids.to), '"to" is numeric');
		assert.ok( (ids.from == '432016865' || ids.to == '432016865'), 'oldid become either "from" or "to"');
		assert.notEqual( ids.from, ids.to, '"from" is different to "to"');
		done();
	});
});

QUnit.test( "Numeric oldid + next", function( assert ) {
	var done = assert.async();
	normaliseIds('next', '432016865', 'foo').always(function(ids) {
		assert.ok(ids, 'There is a return value');
		assert.ok($.isNumeric(ids.from), '"from" is numeric');
		assert.ok($.isNumeric(ids.to), '"to" is numeric');
		assert.ok( (ids.from == '432016865' || ids.to == '432016865'), 'oldid become either "from" or "to"');
		assert.notEqual( ids.from, ids.to, '"from" is different to "to"');
		done();
	});
});

QUnit.test( "Numeric oldid + cur", function( assert ) {
	var done = assert.async();
	normaliseIds('cur', '432016865', 'foo').always(function(ids) {
		assert.ok(ids, 'There is a return value');
		assert.ok($.isNumeric(ids.from), '"from" is numeric');
		assert.ok($.isNumeric(ids.to), '"to" is numeric');
		assert.ok( (ids.from == '432016865' || ids.to == '432016865'), 'oldid become either "from" or "to"');
		assert.notEqual( ids.from, ids.to, '"from" is different to "to"');
		done();
	});
});

QUnit.test( "Numeric diff id only", function( assert ) {
	var done = assert.async();
	normaliseIds('cur', '432016865', 'foo').always(function(ids) {
		assert.ok(ids, 'There is a return value');
		assert.ok($.isNumeric(ids.from), '"from" is numeric');
		assert.ok($.isNumeric(ids.to), '"to" is numeric');
		assert.ok( (ids.from == '432016865' || ids.to == '432016865'), 'oldid become either "from" or "to"');
		assert.notEqual( ids.from, ids.to, '"from" is different to "to"');
		done();
	});
});

QUnit.test( "diff=cur & oldid=prev", function( assert ) {
	var done = assert.async();
	normaliseIds('cur', 'prev', 'foo').always(function(ids) {
		assert.ok(ids, 'There is a return value');
		assert.ok($.isNumeric(ids.from), '"from" is numeric');
		assert.ok($.isNumeric(ids.to), '"to" is numeric');
		//assert.ok( (ids.from == '432016865' || ids.to == '432016865'), 'oldid become either "from" or "to"');
		assert.notEqual( ids.from, ids.to, '"from" is different to "to"');
		done();
	});
});

/* ---------- grabWikitext ---------------------------------------------------------------------- */
QUnit.module( "grabWikitext");

QUnit.test( "Valid id", function( assert ) {
	var done = assert.async();
	grabWikitext('810337311').always(function(result) {
		assert.ok(result.wikitext, 'There is a wikitext value in the result');
		assert.equal(typeof result.wikitext, 'string', 'Wikitext value is a string');
		assert.ok(true, 'Wikitext value is:\n\n' + result.wikitext);
		done();
	});
});

/* ---------- toPlaintext ----------------------------------------------------------------------- */
QUnit.module( "toPlaintext");

QUnit.test( "Simple case", function( assert ) {
	var done = assert.async();
	toPlaintext({'wikitext': "I ''am'' a '''simple''' [[case|Case]]", pageTitle:'SimpleTest'}).always(function(result) {
		assert.ok(result, 'There is a wikitext value in the result');
		assert.equal(typeof result, 'string', 'Wikitext value is a string');
		assert.ok(true, 'Wikitext value is:\n\n' + result);
		done();
	});
});
/* ---------- makeText -------------------------------------------------------------------------- */
QUnit.module( "makeText");

QUnit.test( "Valid id", function( assert ) {
	var done = assert.async();
	makeText('810337311').always(function(result) {
		assert.ok(result, 'There is a result value');
		assert.equal(typeof result, 'string', 'Result value is a string');
		assert.ok(true, 'Value is:\n\n' + result);
		done();
	});
});


});
};

// Add portlet link
mw.util.addPortletLink( 'p-views', '#', 'Test script', 'ca-testScript', 'Run unit tests for this script' );
$('#ca-testScript').click(function(e) {
	e.preventDefault();
	runTests();
});

// On test page, run automatically after 2.5 seconds
if ( mw.config.get('wgPageName') === 'User:Evad37/TextDiff/test' ) {
	setTimeout(runTests, 2500);
}

});