Skip to content

Commit 10ff45a

Browse files
committed
DataContext={resource}: test for DataContext=value inside
1 parent 4e4f950 commit 10ff45a

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

src/Tests/ControlTests/ResourceDataContextTests.cs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
using DotVVM.Framework.ViewModel;
1111
using Microsoft.VisualStudio.TestTools.UnitTesting;
1212
using DotVVM.Framework.Testing;
13-
using System.Security.Claims;
13+
using DotVVM.Framework.Compilation.Styles;
1414

1515
namespace DotVVM.Framework.Tests.ControlTests
1616
{
1717
[TestClass]
1818
public class ResourceDataContextTests
1919
{
2020
static readonly ControlTestHelper cth = new ControlTestHelper(config: config => {
21+
_ = Controls.Repeater.RenderAsNamedTemplateProperty;
22+
config.Styles.Register<Repeater>().SetProperty(r => r.RenderAsNamedTemplate, false, StyleOverrideOptions.Ignore);
2123
});
2224
OutputChecker check = new OutputChecker("testoutputs");
2325

@@ -82,8 +84,6 @@ public async Task Repeater()
8284
<span class=name data-id={resource: Id}>{{resource: Name}}</span>
8385
8486
<span>{{value: _parent.CommandData}}</span>
85-
86-
<dot:Button Click={command: _root.TestMethod(Name)} />
8787
</dot:Repeater>
8888
8989
<!-- with wrapper tag -->
@@ -100,6 +100,17 @@ public async Task Repeater()
100100
);
101101

102102
check.CheckString(r.FormattedHtml, fileExtension: "html");
103+
}
104+
105+
[TestMethod]
106+
public async Task RepeaterWithCommand()
107+
{
108+
var r = await cth.RunPage(typeof(TestViewModel), @"
109+
<dot:Repeater DataSource={resource: Customers.Items}>
110+
<dot:Button Click={command: _root.TestMethod(Name)} />
111+
</dot:Repeater>
112+
"
113+
);
103114

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

122+
[TestMethod]
123+
public async Task DataContextRevert()
124+
{
125+
// revert client-side data context by DataContext={value: _root...}
126+
var r = await cth.RunPage(typeof(TestViewModel), @"
127+
128+
<dot:Repeater DataSource={resource: Customers.Items} RenderWrapperTag=false>
129+
<span class=name data-id={resource: Id}>{{resource: Name}}</span>
130+
131+
<dot:Repeater DataSource={value: _root.FewStrings}>
132+
{{value: _root.StringPrefix + _this}}
133+
</dot:Repeater>
134+
</dot:Repeater>
135+
"
136+
);
137+
138+
check.CheckString(r.FormattedHtml, fileExtension: "html");
139+
}
140+
111141
public class TestViewModel: DotvvmViewModelBase
112142
{
113143
public string NullableString { get; } = null;
@@ -127,6 +157,10 @@ public class TestViewModel: DotvvmViewModelBase
127157
}
128158
};
129159

160+
public List<string> FewStrings { get; set; } = new List<string>() { "Hi!", "Ahoj!" };
161+
162+
public string StringPrefix { get; set; } = "...";
163+
130164
public UploadedFilesCollection Files { get; set; } = new UploadedFilesCollection();
131165

132166
public record CustomerData(
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<html>
2+
<head></head>
3+
<body>
4+
<span class="name" data-id="1">One</span>
5+
<div data-bind="foreach: { data: FewStrings }">
6+
<!-- ko text: ($parent.StringPrefix() ?? "") + $data -->
7+
<!-- /ko -->
8+
</div>
9+
<span class="name" data-id="2">Two</span>
10+
<div data-bind="foreach: { data: FewStrings }">
11+
<!-- ko text: ($parent.StringPrefix() ?? "") + $data -->
12+
<!-- /ko -->
13+
</div>
14+
</body>
15+
</html>

src/Tests/ControlTests/testoutputs/ResourceDataContextTests.Repeater.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
<!-- without wrapper tag -->
66
<span class="name" data-id="1">One</span>
77
<span data-bind="text: CommandData"></span>
8-
<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="">
98
-------------------
109
<span class="name" data-id="2">Two</span>
1110
<span data-bind="text: CommandData"></span>
12-
<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="">
1311

1412
<!-- with wrapper tag -->
1513
<div>

0 commit comments

Comments
 (0)