1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-27 10:30:28 +00:00
TiddlyWiki5/plugins/tiddlywiki/jasmine/run-wiki-based-tests.js

94 lines
2.7 KiB
JavaScript
Raw Normal View History

2022-04-24 20:24:38 +00:00
/*\
title: $:/plugins/tiddlywiki/jasmine/run-wiki-based-tests.js
type: application/javascript
tags: [[$:/tags/test-spec]]
Tests the wiki based tests
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var TEST_WIKI_TIDDLER_FILTER = "[all[tiddlers+shadows]type[text/vnd.tiddlywiki-multiple]tag[$:/tags/wiki-test-spec]]";
2022-04-24 20:24:38 +00:00
var widget = require("$:/core/modules/widgets/widget.js");
describe("Wiki-based tests", function() {
// Step through the test tiddlers
var tests = $tw.wiki.filterTiddlers(TEST_WIKI_TIDDLER_FILTER);
$tw.utils.each(tests,function(title) {
var tiddler = $tw.wiki.getTiddler(title);
it(tiddler.fields.title + ": " + tiddler.fields.description, function() {
// Add our tiddlers
var wiki = new $tw.Wiki();
wiki.addTiddlers(readMultipleTiddlersTiddler(title));
// Complain if we don't have the ouput and expected results
if(!wiki.tiddlerExists("Output")) {
throw "Missing 'Output' tiddler";
}
if(!wiki.tiddlerExists("ExpectedResult")) {
throw "Missing 'ExpectedResult' tiddler";
}
// Construct the widget node
var text = "{{Output}}\n\n";
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Clear changes queue
wiki.clearTiddlerEventQueue();
2022-04-26 12:05:44 +00:00
// Run the actions if provided
if(wiki.tiddlerExists("Actions")) {
widgetNode.invokeActionString(wiki.getTiddlerText("Actions"));
refreshWidgetNode(widgetNode,wrapper);
}
2022-04-24 20:24:38 +00:00
// Test the rendering
expect(wrapper.innerHTML).toBe(wiki.getTiddlerText("ExpectedResult"));
});
});
function readMultipleTiddlersTiddler(title) {
var rawTiddlers = $tw.wiki.getTiddlerText(title).split("\n+\n");
2022-04-24 20:24:38 +00:00
var tiddlers = [];
$tw.utils.each(rawTiddlers,function(rawTiddler) {
var fields = Object.create(null),
split = rawTiddler.split(/\r?\n\r?\n/mg);
if(split.length >= 1) {
fields = $tw.utils.parseFields(split[0],fields);
}
if(split.length >= 2) {
fields.text = split.slice(1).join("\n\n");
}
tiddlers.push(fields);
});
return tiddlers;
}
2022-05-26 20:11:53 +00:00
function createWidgetNode(parser,wiki) {
return wiki.makeWidget(parser);
2022-04-24 20:24:38 +00:00
}
function parseText(text,wiki,options) {
2022-05-26 20:11:53 +00:00
return wiki.parseText("text/vnd.tiddlywiki",text,options);
2022-04-24 20:24:38 +00:00
}
function renderWidgetNode(widgetNode) {
$tw.fakeDocument.setSequenceNumber(0);
var wrapper = $tw.fakeDocument.createElement("div");
widgetNode.render(wrapper,null);
// console.log(require("util").inspect(wrapper,{depth: 8}));
return wrapper;
}
2022-04-26 12:05:44 +00:00
function refreshWidgetNode(widgetNode,wrapper) {
widgetNode.refresh(widgetNode.wiki.changedTiddlers,wrapper);
2022-04-24 20:24:38 +00:00
// console.log(require("util").inspect(wrapper,{depth: 8}));
}
});
})();