(function($) {
/*
======== A Handy Little QUnit Reference ========
http://api.qunitjs.com/
Test methods:
QUnit.module(name, {[setup][ ,teardown]})
QUnit.test(name, callback)
expect(numberOfAssertions)
stop(increment)
start(decrement)
Test assertions:
assert.ok(value, [message])
assert.equal(actual, expected, [message])
notEqual(actual, expected, [message])
deepEqual(actual, expected, [message])
notDeepEqual(actual, expected, [message])
strictEqual(actual, expected, [message])
notStrictEqual(actual, expected, [message])
throws(block, [expected], [message])
*/
function loadScriptPromise(src) {
return new Promise((resolve, reject) => {
var scr = document.createElement("script");
scr.async = true;
scr.src = src;
scr.onload = resolve;
document.head.appendChild(scr);
});
}
function loadTablesawDynamically() {
return loadScriptPromise("../dist/tablesaw.js");
}
function initializeTablesawUsingMethodCall() {
if (!!window.Tablesaw) {
Tablesaw.init();
}
}
function initializeTablesawUsingInitScript() {
return loadScriptPromise("../dist/tablesaw-init.js");
}
function createTableDynamically() {
var tableSelection = $([
'
',
'',
'',
'| Header 1 | ',
'Header 2 | ',
'Header 3 | ',
'Header 4 | ',
'
',
'',
'',
'',
'| Body Row 1 | ',
'1 | ',
'foo | ',
'bar | ',
'
',
'',
'| Body Row 2 | ',
'2 | ',
'foo | ',
'bar | ',
'
',
'',
'| Body Row 3 | ',
'3 | ',
'foo | ',
'bar | ',
'
',
'',
'
'].join(''));
$( '#qunit-fixture' ).append(tableSelection);
return tableSelection;
}
var tableCounter = 0;
async function createAndInitializeTable(assert, initializeUsingInitScript) {
tableCounter++;
var tableSelection = createTableDynamically();
var tableText = 'Table ' + tableCounter+': ';
var methodText = 'using ' + (initializeUsingInitScript ? 'tablesaw-init.js script.' : 'the init() method call.');
assert.ok( !tableSelection.is('.tablesaw-sortable'), tableText + ' initialization class not found before being initialized ' + methodText );
if (initializeUsingInitScript)
await initializeTablesawUsingInitScript();
else
await initializeTablesawUsingMethodCall();
assert.ok( tableSelection.is('.tablesaw-sortable'), tableText + ' initialization class found before being initialized ' + methodText );
}
QUnit.module( 'Global' );
QUnit.test( 'tablesaw is dynamically loaded and tables are dynamically created and initialized', async function( assert ) {
assert.ok( !window.Tablesaw, 'Tablesaw object is not available.' );
assert.ok( !$( 'html' ).is('.tablesaw-enhanced'), 'Html initialization class is not set before Tablesaw is loaded.' );
await loadTablesawDynamically();
assert.ok( !!window.Tablesaw && !!window.Tablesaw.init, 'Tablesaw object is available.' );
assert.ok( $( 'html' ).is('.tablesaw-enhanced'), 'Html initialization class is set after Tablesaw is loaded.' );
await createAndInitializeTable(assert, false);
await createAndInitializeTable(assert, true);
await createAndInitializeTable(assert, false);
await createAndInitializeTable(assert, true);
});
}( window.shoestring || window.jQuery || window.$jQ ));