Skip to content

Commit

Permalink
control tests: render template resources by default
Browse files Browse the repository at this point in the history
  • Loading branch information
exyi committed Dec 21, 2022
1 parent 6f4df9b commit 9b79772
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/Framework/Testing/ControlTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@ public ControlTestHelper(bool debug = true, Action<DotvvmConfiguration>? config
Dictionary<string, string>? markupFiles = null)
{
if (!fileLoader.MarkupFiles.TryAdd(fileName, markup))
throw new Exception($"File {fileName} already exists");
{
if (fileLoader.MarkupFiles[fileName] != markup)
throw new Exception($"File {fileName} already exists");
}

if (markupFiles is object) foreach (var markupFile in markupFiles)
{
if (!fileLoader.MarkupFiles.TryAdd(markupFile.Key, markupFile.Value))
throw new Exception($"File {markupFile.Value} already exists");
if (fileLoader.MarkupFiles[markupFile.Key] != markupFile.Value)
throw new Exception($"File {markupFile.Value} already exists");
}

return controlBuilderFactory.GetControlBuilder(fileName);
Expand Down Expand Up @@ -365,14 +369,19 @@ public class FakeBodyResourceLink : BodyResourceLinks
public string? CapturedHtml { get; private set; }
protected override void RenderControl(IHtmlWriter writer, IDotvvmRequestContext context)
{
var resourceManager = context.ResourceManager;
if (resourceManager.BodyRendered) return;
resourceManager.BodyRendered = true; // set the flag before the resources are rendered, so they can't add more resources to the list during the render

ResourcesRenderer.RenderResources(context.ResourceManager,
context.ResourceManager.GetNamedResourcesInOrder().Where(r => r.Resource is TemplateResource),
writer, context, ResourceRenderPosition.Body);

var str = new StringWriter();
var fakeWriter = new HtmlWriter(str, context);
base.RenderControl(fakeWriter, context);
this.CapturedHtml = str.ToString();

ResourcesRenderer.RenderResources(context.ResourceManager,
context.ResourceManager.GetNamedResourcesInOrder().Where(r => r.Resource is TemplateResource),
writer, context, ResourceRenderPosition.Body);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,12 @@
something
</div>
</div>

<!-- Resource gC64SHe4hRph/QhsVNKj4zUTHIm1GI1zo+Hn7WLuMeY= of type TemplateResource. -->
<template id="gC64SHe4hRph/QhsVNKj4zUTHIm1GI1zo+Hn7WLuMeY="><div class="magic-class contains-bazmek">
<span class="contains-bazmek">
bazmek
</span>
</div></template>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@
<div>
<div class="a">Something</div>
</div>

<!-- Resource cOvMmUhlJiLLj5tqZbMciSiRJeRRjviJeyXbpWBMycU= of type TemplateResource. -->
<template id="cOvMmUhlJiLLj5tqZbMciSiRJeRRjviJeyXbpWBMycU=">Separator</template>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
<div data-bind="dotvvm-js-component: { name: &quot;Bazmek&quot;, props: { lol: int }, view: &quot;p0&quot; }"></div>
<div data-bind="dotvvm-js-component: { name: &quot;Bazmek&quot;, commands: { cmd: (...args)=>(dotvvm.postBack($element,[],&quot;Rrba2KXEPUp2v+qm&quot;,&quot;&quot;,$context,[],args,undefined)) }, view: &quot;p0&quot; }"></div>
<div data-bind="dotvvm-js-component: { name: &quot;Bazmek&quot;, commands: { scmd: (...args)=>(dotvvm.applyPostbackHandlers((options) => options.viewModel.int(args[0]).int(),$element,[],args,$context)) }, view: &quot;p0&quot; }"></div>

<!-- Resource nd32PDzj7nMLlQAzyEgX84ylX61EVYZpjwVI25SuM74= of type TemplateResource. -->
<template id="nd32PDzj7nMLlQAzyEgX84ylX61EVYZpjwVI25SuM74="><h1>
Ahoj lidi
</h1></template>
</body>
</html>

0 comments on commit 9b79772

Please sign in to comment.