forked from andrewdavey/cassette
/
ExternalScriptBundle.Render.cs
153 lines (122 loc) · 6.18 KB
/
ExternalScriptBundle.Render.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using Cassette.BundleProcessing;
using Cassette.Configuration;
using Moq;
using Should;
using Xunit;
namespace Cassette.Scripts
{
public class ExternalScriptBundleRender_Tests
{
public ExternalScriptBundleRender_Tests()
{
settings = new CassetteSettings("")
{
IsDebuggingEnabled = false,
UrlGenerator = Mock.Of<IUrlGenerator>()
};
fallbackRenderer = new Mock<IBundleHtmlRenderer<ScriptBundle>>();
}
readonly CassetteSettings settings;
readonly Mock<IBundleHtmlRenderer<ScriptBundle>> fallbackRenderer;
[Fact]
public void WhenRenderExternalScriptBundle_ThenHtmlIsScriptElement()
{
var bundle = new ExternalScriptBundle("http://test.com/") { Processor = Mock.Of<IBundleProcessor<ScriptBundle>>() };
var html = Render(bundle);
html.ShouldEqual("<script src=\"http://test.com/\" type=\"text/javascript\"></script>");
}
[Fact]
public void WhenRenderExternalScriptBundleWithCondition_ThenHtmlIsScriptElementWithConditional()
{
var bundle = new ExternalScriptBundle("http://test.com/") { Condition = "CONDITION", Processor = Mock.Of<IBundleProcessor<ScriptBundle>>() };
var html = Render(bundle);
html.ShouldEqual(
"<!--[if CONDITION]>" + Environment.NewLine +
"<script src=\"http://test.com/\" type=\"text/javascript\"></script>" + Environment.NewLine +
"<![endif]-->");
}
[Fact]
public void WhenRenderExternalScriptBundleWithNotIECondition_ThenHtmlIsScriptElementWithConditionalButLeavesScriptVisibleToAllBrowsers()
{
var bundle = new ExternalScriptBundle("http://test.com/") { Condition = "(gt IE 9)|!(IE)", Processor = Mock.Of<IBundleProcessor<ScriptBundle>>() };
var fallbackRenderer = new Mock<IBundleHtmlRenderer<ScriptBundle>>();
var html = bundle.Render(bundle);
html.ShouldEqual(
"<!--[if " + bundle.Condition + "]><!-->" + Environment.NewLine +
"<script src=\"http://test.com/\" type=\"text/javascript\"></script>" + Environment.NewLine +
"<!-- <![endif]-->");
}
[Fact]
public void WhenRenderExternalScriptBundleWithHtmlAttributes_ThenHtmlIsScriptElementWithExtraAttributes()
{
var bundle = new ExternalScriptBundle("http://test.com/") { Processor = Mock.Of<IBundleProcessor<ScriptBundle>>() };
bundle.HtmlAttributes["class"] = "foo";
var html = Render(bundle);
html.ShouldEqual("<script src=\"http://test.com/\" type=\"text/javascript\" class=\"foo\"></script>");
}
[Fact]
public void WhenRenderExternalScriptBundleWithLocalAssetsAndIsDebugMode_ThenFallbackRendererUsed()
{
var bundle = new ExternalScriptBundle("http://test.com/", "test") { Processor = Mock.Of<IBundleProcessor<ScriptBundle>>() };
bundle.Assets.Add(new StubAsset());
fallbackRenderer.Setup(r => r.Render(bundle))
.Returns("FALLBACK");
settings.IsDebuggingEnabled = true;
var html = Render(bundle);
html.ShouldEqual("FALLBACK");
}
[Fact]
public void WhenRenderExternalScriptBundleWithFallbackAsset_ThenHtmlContainsFallbackScript()
{
var bundle = new ExternalScriptBundle("http://test.com/", "test", "CONDITION") { Processor = Mock.Of<IBundleProcessor<ScriptBundle>>() };
bundle.Assets.Add(new StubAsset());
fallbackRenderer.Setup(r => r.Render(bundle))
.Returns("FALLBACK");
var html = Render(bundle);
html.ShouldEqual(
"<script src=\"http://test.com/\" type=\"text/javascript\"></script>" + Environment.NewLine +
"<script type=\"text/javascript\">" + Environment.NewLine +
"if(CONDITION){" + Environment.NewLine +
"document.write(unescape('FALLBACK'));" + Environment.NewLine +
"}" + Environment.NewLine +
"</script>"
);
}
[Fact]
public void WhenRenderExternalScriptBundleWithFallbackAsset_ThenHtmlEscapesFallbackScriptTags()
{
var bundle = new ExternalScriptBundle("http://test.com/", "test", "CONDITION") { Processor = Mock.Of<IBundleProcessor<ScriptBundle>>() };
bundle.Assets.Add(new StubAsset());
fallbackRenderer.Setup(r => r.Render(bundle))
.Returns("<script></script>");
var html = Render(bundle);
html.ShouldContain("%3Cscript%3E%3C/script%3E");
}
[Fact]
public void GivenExternalScriptBundleWithFallbackAssetsAndDebugMode_WhenRender_ThenOnlyOutputFallbackScripts()
{
settings.IsDebuggingEnabled = true;
var bundle = new ExternalScriptBundle("http://test.com/", "test", "CONDITION") { Processor = Mock.Of<IBundleProcessor<ScriptBundle>>() };
bundle.Assets.Add(new StubAsset());
fallbackRenderer.Setup(r => r.Render(bundle))
.Returns("<script></script>");
var html = Render(bundle);
html.ShouldEqual("<script></script>");
}
[Fact]
public void WhenRenderExternalScriptBundleWithNoLocalAssetsAndIsDebugMode_ThenNormalScriptElementIsReturned()
{
var bundle = new ExternalScriptBundle("http://test.com/", "test") { Processor = Mock.Of<IBundleProcessor<ScriptBundle>>() };
settings.IsDebuggingEnabled = true;
var html = Render(bundle);
html.ShouldEqual("<script src=\"http://test.com/\" type=\"text/javascript\"></script>");
}
string Render(ExternalScriptBundle bundle)
{
bundle.Process(settings);
bundle.FallbackRenderer = fallbackRenderer.Object;
return bundle.Render();
}
}
}