add tests for widget getVariableInfo.params

This commit is contained in:
pmario 2024-05-05 18:06:57 +02:00
parent 10c4196a18
commit b621c75342
1 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,95 @@
/*\
title: test-widget-getVariableInfo.js
type: application/javascript
tags: [[$:/tags/test-spec]]
Tests the wikitext rendering pipeline end-to-end. We also need tests that individually test parsers, rendertreenodes etc., but this gets us started.
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
describe("Widget module", function() {
var widget = require("$:/core/modules/widgets/widget.js");
function createWidgetNode(parseTreeNode,wiki) {
return new widget.widget(parseTreeNode,{
wiki: wiki,
document: $tw.fakeDocument
});
}
function parseText(text,wiki,options) {
var parser = wiki.parseText("text/vnd.tiddlywiki",text,options);
return parser ? {type: "widget", children: parser.tree} : undefined;
}
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;
}
function refreshWidgetNode(widgetNode,wrapper,changes) {
var changedTiddlers = {};
if(changes) {
$tw.utils.each(changes,function(title) {
changedTiddlers[title] = true;
});
}
widgetNode.refresh(changedTiddlers,wrapper,null);
// console.log(require("util").inspect(wrapper,{depth: 8}));
}
it("should make sure that getVariableInfo returns all expected parameters", function() {
var wiki = new $tw.Wiki();
wiki.addTiddlers([
{title: "A", text: "\\define macro(a:a) aaa"},
{title: "B", text: "\\function fn(f:f) fff\n\\function x() [<fn>]"},
{title: "C", text: "\\procedure proc(p:p) ppp"},
{title: "D", text: "\\widget $my.widget(w:w) www"}
]);
var text = "\\import A B C D\n\n<$let abc=def>";
var widgetNode = createWidgetNode(parseText(text,wiki),wiki);
// Render the widget node to the DOM
renderWidgetNode(widgetNode);
var childNode = widgetNode;
while(childNode.children.length > 0) {
childNode = childNode.children[0];
}
expect(childNode.getVariableInfo("macro",{allowSelfAssigned:true}).params).toEqual([{name:"a",value:"a"}]);
// function params
expect(childNode.getVariableInfo("fn", {allowSelfAssigned:true}).params).toEqual([{name:"f",value:"f"}]);
// functions have a text and a value
expect(childNode.getVariableInfo("x", {allowSelfAssigned:true}).text).toBe("fff");
expect(childNode.getVariableInfo("x", {allowSelfAssigned:true}).srcVariable.value).toBe("[<fn>]");
// procedures and widgets failed prior to v5.3.4
expect(childNode.getVariableInfo("proc", {allowSelfAssigned:true}).params).toEqual([{name:"p",default:"p"}]);
expect(childNode.getVariableInfo("$my.widget", {allowSelfAssigned:true}).params).toEqual([{name:"w",default:"w"}]);
// no params expected
expect(childNode.getVariableInfo("abc", {allowSelfAssigned:true})).toEqual({text:"def"});
// debugger; Find code in browser
// Find values to be compated to
// console.log("macro", childNode.getVariableInfo("macro",{allowSelfAssigned:true}));
// console.log("function", childNode.getVariableInfo("fn",{allowSelfAssigned:true}));
// console.log("function x", childNode.getVariableInfo("x",{allowSelfAssigned:true}));
// console.log("procedure", childNode.getVariableInfo("proc",{allowSelfAssigned:true}));
// console.log("widget", childNode.getVariableInfo("$my.widget",{allowSelfAssigned:true}));
// console.log("let", childNode.getVariableInfo("abc",{allowSelfAssigned:true}));
});
});
})();