Skip to content

Commit

Permalink
Button: unit test ClickArguments
Browse files Browse the repository at this point in the history
  • Loading branch information
exyi committed Jun 1, 2024
1 parent d94ca61 commit e71881a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Framework/Testing/BindingTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,25 @@ public StaticCommandBindingExpression StaticCommand(string expression, DataConte
});
}

/// <summary> Creates a <c>command</c> binding by parsing the specified expression. </summary>
/// <param name="contexts"> Hierarchy of data contexts. First element is <c>_root</c>, last element is <c>_this</c>. </param>
/// <param name="expectedType"> If specified, an implicit conversion into this type will be applied in the expression. </param>
public CommandBindingExpression Command(string expression, Type[] contexts, Type? expectedType = null) =>
Command(expression, CreateDataContext(contexts), expectedType);

/// <summary> Creates a <c>command</c> binding by parsing the specified expression. </summary>
/// <param name="expectedType"> If specified, an implicit conversion into this type will be applied in the expression. </param>
public CommandBindingExpression Command(string expression, DataContextStack context, Type? expectedType = null)
{
expectedType ??= typeof(Command);
return new CommandBindingExpression(BindingService, new object[] {
context,
new OriginalStringBindingProperty(expression),
BindingParserOptions.Value.AddImports(context.NamespaceImports).AddImports(Configuration.Markup.ImportedNamespaces),
new ExpectedTypeBindingProperty(expectedType)
});
}

/// <summary> Creates a value binding by parsing the specified expression. The expression will be implicitly converted to <typeparamref name="T"/> </summary>
/// <param name="contexts"> Hierarchy of data contexts. First element is <c>_root</c>, last element is <c>_this</c>. </param>
/// <param name="expectedType"> Convert the result to this type instead of converting it to <typeparamref name="T"/>. The type must be assignable to T. </param>
Expand Down
20 changes: 20 additions & 0 deletions src/Tests/Runtime/DotvvmControlRenderedHtmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using CheckTestOutput;
using DotVVM.Framework.Binding;
using DotVVM.Framework.Binding.Expressions;
using DotVVM.Framework.Compilation.ControlTree;
Expand All @@ -18,6 +20,8 @@ namespace DotVVM.Framework.Tests.Runtime
[TestClass]
public class DotvvmControlRenderedHtmlTests : DotvvmControlTestBase
{
static readonly BindingTestHelper bindingHelper = new BindingTestHelper();
readonly OutputChecker outputChecker = new OutputChecker("testoutputs");
[TestMethod]
public void GridViewTextColumn_RenderedHtmlTest_ServerRendering()
{
Expand Down Expand Up @@ -211,6 +215,22 @@ public void Literal_DateTimeToBrowserLocalTime_RenderOnServer()
});
}

[TestMethod]
public void Button_ClickArgumentsCommand()
{
var vm = new LiteralDateTimeViewModel();
var command = bindingHelper.Command("null", [ typeof(LiteralDateTimeViewModel) ], typeof(Func<DateTime, int, Task>));
var button = new Button("text", command) {
ClickArguments = new object[] {
bindingHelper.ValueBinding<DateTime>("DateTime", [ typeof(LiteralDateTimeViewModel) ]),
1
}
};

var html = InvokeLifecycleAndRender(button, CreateContext(vm));
outputChecker.CheckString(html, "Button_ClickArgumentsCommand", fileExtension: "html");
}


public class OrderedDataBindTextBox : TextBox
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<input type=button value=text onclick='dotvvm.postBack(this,[],"QWOVggzEvWARXRHR","",null,["validate-root"],[ko.contextFor(this).$data.DateTime,1],undefined).catch(dotvvm.log.logPostBackScriptError);event.stopPropagation();return false;' />

0 comments on commit e71881a

Please sign in to comment.