1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-02-03 04:39:10 +00:00

Don't have data widget rendering its own JSON

Making the data widget render its raw JSON (introduced in 683ec33004) was a bad idea as it messes up the innerwiki use of the data widget. Instead we use the testcase widget with a special template to render the raw JSON of the payload of a testcase, thus giving us a way to test the data widget
This commit is contained in:
jeremy@jermolene.com 2023-05-04 08:57:43 +01:00
parent c6f15d8e8c
commit d9b6384884
8 changed files with 27 additions and 19 deletions

View File

@ -31,12 +31,7 @@ DataWidget.prototype.render = function(parent,nextSibling) {
this.parentDomNode = parent;
this.computeAttributes();
this.execute();
var domNode = this.document.createTextNode("");
parent.insertBefore(domNode,nextSibling);
this.renderChildren(parent,nextSibling);
// Children must have been rendered before we can read the data values
domNode.textContent = JSON.stringify(this.readDataTiddlerValues());
this.domNodes.push(domNode);
};
/*

View File

@ -57,11 +57,14 @@ TestCaseWidget.prototype.render = function(parent,nextSibling) {
}
loadTiddler("$:/core");
loadTiddler("$:/plugins/tiddlywiki/codemirror");
// Load the test case template
// loadTiddler(this.testcaseTemplate);
// Load tiddlers from child data widgets
var tiddlers = [];
this.findChildrenDataWidgets(this.contentRoot.children,"data",function(widget) {
Array.prototype.push.apply(tiddlers,widget.readDataTiddlerValues());
});
var jsonPayload = JSON.stringify(tiddlers);
this.testcaseWiki.addTiddlers(tiddlers);
// Unpack plugin tiddlers
this.testcaseWiki.readPluginInfo();
@ -69,15 +72,9 @@ TestCaseWidget.prototype.render = function(parent,nextSibling) {
this.testcaseWiki.unpackPluginTiddlers();
this.testcaseWiki.addIndexersToWiki();
// Generate a `transclusion` variable that depends on the values of the payload tiddlers so that the template can easily make unique state tiddlers
this.setVariable("transclusion",$tw.utils.hashString(this.testcaseWiki.getTiddlersAsJson("[all[tiddlers]]")));
// Generate a `testcaseInfo` variable that contains information about the subwiki in JSON format
var testcaseInfoData = {
tiddlers: {} // Hashmap of tiddler titles mapped to array of field names
};
this.testcaseWiki.each(function(tiddler,title) {
testcaseInfoData.tiddlers[title] = Object.keys(tiddler.fields);
});
this.setVariable("testcaseInfo",JSON.stringify(testcaseInfoData));
this.setVariable("transclusion",$tw.utils.hashString(jsonPayload));
// Generate a `payloadTiddlers` variable that contains the payload in JSON format
this.setVariable("payloadTiddlers",jsonPayload);
// Render the page root template of the subwiki
var rootWidget = this.testcaseWiki.makeTranscludeWidget(this.testcaseTemplate,{document: this.document, parseAsInline: false, parentWidget: this});
rootWidget.render(parent,nextSibling);

View File

@ -0,0 +1,4 @@
title: $:/core/ui/testcases/RawJSONTemplate
\whitespace trim
<$text text=<<payloadTiddlers>>/>

View File

@ -9,7 +9,9 @@ text: Importing a compound payload tiddler and adding custom fields
title: Output
\whitespace trim
<$testcase template="$:/core/ui/testcases/RawJSONTemplate">
<$data $compound-tiddler="Compound" custom="Alpha"/>
</$testcase>
+
title: Compound
type: text/vnd.tiddlywiki-multiple
@ -22,4 +24,4 @@ This is a payload tiddler from a compound tiddler
+
title: ExpectedResult
<p>[{"title":"Payload Tiddler","tags":"Alpha Beta Gamma","text":"This is a payload tiddler from a compound tiddler","custom":"Alpha"}]</p>
<p><div>[{"title":"Payload Tiddler","tags":"Alpha Beta Gamma","text":"This is a payload tiddler from a compound tiddler","custom":"Alpha"}]</div></p>

View File

@ -9,7 +9,9 @@ text: Importing a payload filter and adding custom fields
title: Output
\whitespace trim
<$testcase template="$:/core/ui/testcases/RawJSONTemplate">
<$data $filter="[tag[Definitions]]" custom="Alpha"/>
</$testcase>
+
title: HelloThere
tags: Definitions
@ -23,4 +25,4 @@ This is the tiddler AnotherDefinition
+
title: ExpectedResult
<p>[{"title":"AnotherDefinition","tags":"Definitions","text":"This is the tiddler AnotherDefinition","custom":"Alpha"},{"title":"HelloThere","tags":"Definitions","text":"This is the tiddler HelloThere","custom":"Alpha"}]</p>
<p><div>[{"title":"AnotherDefinition","tags":"Definitions","text":"This is the tiddler AnotherDefinition","custom":"Alpha"},{"title":"HelloThere","tags":"Definitions","text":"This is the tiddler HelloThere","custom":"Alpha"}]</div></p>

View File

@ -9,7 +9,9 @@ text: Importing a payload tiddler and adding custom fields
title: Output
\whitespace trim
<$testcase template="$:/core/ui/testcases/RawJSONTemplate">
<$data $tiddler="HelloThere" custom="Alpha"/>
</$testcase>
+
title: HelloThere
tags: Definitions
@ -18,4 +20,4 @@ This is the tiddler HelloThere
+
title: ExpectedResult
<p>[{"title":"HelloThere","tags":"Definitions","text":"This is the tiddler HelloThere","custom":"Alpha"}]</p>
<p><div>[{"title":"HelloThere","tags":"Definitions","text":"This is the tiddler HelloThere","custom":"Alpha"}]</div></p>

View File

@ -9,8 +9,10 @@ text: Standalone data widget to create individual tiddlers
title: Output
\whitespace trim
<$testcase template="$:/core/ui/testcases/RawJSONTemplate">
<$data title="Epsilon" text="Theta"/>
</$testcase>
+
title: ExpectedResult
<p>[{"title":"Epsilon","text":"Theta"}]</p>
<p><div>[{"title":"Epsilon","text":"Theta"}]</div></p>

View File

@ -24,7 +24,11 @@ describe("Wiki-based tests", function() {
var tiddler = $tw.wiki.getTiddler(title);
it(tiddler.fields.title + ": " + tiddler.fields.description, function() {
// Add our tiddlers
var wiki = new $tw.Wiki();
var wiki = new $tw.Wiki(),
coreTiddler = $tw.wiki.getTiddler("$:/core");
if(coreTiddler) {
wiki.addTiddler(coreTiddler);
}
wiki.addTiddlers(readMultipleTiddlersTiddler(title));
// Complain if we don't have the ouput and expected results
if(!wiki.tiddlerExists("Output")) {