Skip to content

Commit

Permalink
DataContext={resource}: test for DataContext=value inside
Browse files Browse the repository at this point in the history
  • Loading branch information
exyi committed May 22, 2022
1 parent 76fd0f2 commit bcdd52d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
42 changes: 37 additions & 5 deletions src/Tests/ControlTests/ResourceDataContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
using DotVVM.Framework.ViewModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DotVVM.Framework.Testing;
using System.Security.Claims;
using DotVVM.Framework.Compilation.Styles;

namespace DotVVM.Framework.Tests.ControlTests
{
[TestClass]
public class ResourceDataContextTests
{
static readonly ControlTestHelper cth = new ControlTestHelper(config: config => {
_ = Controls.Repeater.RenderAsNamedTemplateProperty;
config.Styles.Register<Repeater>().SetProperty(r => r.RenderAsNamedTemplate, false, StyleOverrideOptions.Ignore);
});
OutputChecker check = new OutputChecker("testoutputs");

Expand Down Expand Up @@ -82,8 +84,6 @@ public async Task Repeater()
<span class=name data-id={resource: Id}>{{resource: Name}}</span>
<span>{{value: _parent.CommandData}}</span>
<dot:Button Click={command: _root.TestMethod(Name)} />
</dot:Repeater>
<!-- with wrapper tag -->
Expand All @@ -95,13 +95,22 @@ public async Task Repeater()
<span class=name data-id={resource: Id}>{{resource: Name}}</span>
<span>{{value: _parent.CommandData}}</span>
<dot:Button Click={command: _root.TestMethod(Name)} />
</dot:Repeater>
"
);

check.CheckString(r.FormattedHtml, fileExtension: "html");
}

[TestMethod]
public async Task RepeaterWithCommand()
{
var r = await cth.RunPage(typeof(TestViewModel), @"
<dot:Repeater DataSource={resource: Customers.Items}>
<dot:Button Click={command: _root.TestMethod(Name)} />
</dot:Repeater>
"
);

await r.RunCommand("_root.TestMethod(Name)", x => x is TestViewModel.CustomerData { Id: 1 });
Assert.AreEqual((string)r.ViewModel.CommandData, "One");
Expand All @@ -110,6 +119,25 @@ public async Task Repeater()
Assert.AreEqual((string)r.ViewModel.CommandData, "Two");
}

[TestMethod]
public async Task DataContextRevert()
{
// revert client-side data context by DataContext={value: _root...}
var r = await cth.RunPage(typeof(TestViewModel), @"
<dot:Repeater DataSource={resource: Customers.Items} RenderWrapperTag=false>
<span class=name data-id={resource: Id}>{{resource: Name}}</span>
<dot:Repeater DataSource={value: _root.FewStrings}>
{{value: _root.StringPrefix + _this}}
</dot:Repeater>
</dot:Repeater>
"
);

check.CheckString(r.FormattedHtml, fileExtension: "html");
}

public class TestViewModel: DotvvmViewModelBase
{
public string NullableString { get; } = null;
Expand All @@ -129,6 +157,10 @@ public class TestViewModel: DotvvmViewModelBase
}
};

public List<string> FewStrings { get; set; } = new List<string>() { "Hi!", "Ahoj!" };

public string StringPrefix { get; set; } = "...";

public UploadedFilesCollection Files { get; set; } = new UploadedFilesCollection();

public record CustomerData(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<head></head>
<body>
<span class="name" data-id="1">One</span>
<div data-bind="foreach: { data: FewStrings }">
<!-- ko text: ($parent.StringPrefix() ?? "") + $data -->
<!-- /ko -->
</div>
<span class="name" data-id="2">Two</span>
<div data-bind="foreach: { data: FewStrings }">
<!-- ko text: ($parent.StringPrefix() ?? "") + $data -->
<!-- /ko -->
</div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@
<!-- without wrapper tag -->
<span class="name" data-id="1">One</span>
<span data-bind="text: CommandData"></span>
<input onclick="dotvvm.postBack(this,[&quot;Customers.Items/[0]&quot;],&quot;LTd5tu7TstgZN8jr&quot;,&quot;&quot;,null,[],[],undefined).catch(dotvvm.log.logPostBackScriptError);event.stopPropagation();return false;" type="button" value="">
-------------------
<span class="name" data-id="2">Two</span>
<span data-bind="text: CommandData"></span>
<input onclick="dotvvm.postBack(this,[&quot;Customers.Items/[1]&quot;],&quot;LTd5tu7TstgZN8jr&quot;,&quot;&quot;,null,[],[],undefined).catch(dotvvm.log.logPostBackScriptError);event.stopPropagation();return false;" type="button" value="">

<!-- with wrapper tag -->
<div>
<span class="name" data-id="1">One</span>
<span data-bind="text: CommandData"></span>
<input onclick="dotvvm.postBack(this,[&quot;Customers/[0]&quot;],&quot;VoomVIgRFCWNqBr4&quot;,&quot;&quot;,null,[],[],undefined).catch(dotvvm.log.logPostBackScriptError);event.stopPropagation();return false;" type="button" value="">
-------------------
<span class="name" data-id="2">Two</span>
<span data-bind="text: CommandData"></span>
<input onclick="dotvvm.postBack(this,[&quot;Customers/[1]&quot;],&quot;VoomVIgRFCWNqBr4&quot;,&quot;&quot;,null,[],[],undefined).catch(dotvvm.log.logPostBackScriptError);event.stopPropagation();return false;" type="button" value="">
</div>
</body>
</html>

0 comments on commit bcdd52d

Please sign in to comment.