/
NGonTests.cs
138 lines (113 loc) · 4.52 KB
/
NGonTests.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
using System;
using System.Dynamic;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Moq;
using NUnit.Framework;
namespace NGon.Tests
{
[TestFixture]
public class NGonTests
{
private ControllerBase _controller;
private HtmlHelper _helper;
private const string StartScriptTagFormat = @"<script type=""text/javascript"">window.{0}={{}};";
private const string EndScriptTag = @"</script>";
[SetUp]
public void Setup()
{
var controllerContext = new ControllerContext(new Mock<HttpContextBase>().Object, new RouteData(), new Mock<ControllerBase>().Object);
controllerContext.Controller.ViewData = new ViewDataDictionary { { "NGon", new ExpandoObject() } };
var viewContext = new ViewContext(controllerContext, new Mock<IView>().Object, controllerContext.Controller.ViewData, new TempDataDictionary(), new Mock<TextWriter>().Object);
var mockViewDataContainer = new Mock<IViewDataContainer>();
mockViewDataContainer.Setup(v => v.ViewData).Returns(controllerContext.Controller.ViewData);
_controller = controllerContext.Controller;
_helper = new HtmlHelper(viewContext, mockViewDataContainer.Object);
}
[Test]
public void SimpleStringPropertyTest()
{
//arrange
_controller.ViewBag.NGon.Foo = "100";
//act
var result = _helper.IncludeNGon();
//assert
var expected = String.Concat(String.Format(StartScriptTagFormat, "ngon"), @"ngon.Foo=""100"";", EndScriptTag);
var actual = result.ToString();
Assert.AreEqual(expected, actual);
}
[Test]
public void SimpleIntPropertyTest()
{
//arrange
_controller.ViewBag.NGon.Foo = 100;
//act
var result = _helper.IncludeNGon();
//assert
var expected = String.Concat(String.Format(StartScriptTagFormat, "ngon"), @"ngon.Foo=100;", EndScriptTag);
var actual = result.ToString();
Assert.AreEqual(expected, actual);
}
[Test]
public void PocoObjectJsonTest()
{
var person = new { FirstName = "John", LastName = "Doe", Age = 45 };
//arrange
_controller.ViewBag.NGon.Person = person;
//act
var result = _helper.IncludeNGon();
//assert
var expected = String.Concat(String.Format(StartScriptTagFormat, "ngon"), @"ngon.Person={""FirstName"":""John"",""LastName"":""Doe"",""Age"":45};", EndScriptTag);
var actual = result.ToString();
Assert.AreEqual(expected, actual);
}
[Test]
public void NamespaceTest()
{
const string @namespace = "test";
//arrange
_controller.ViewBag.NGon.Foo = 100;
//act
var result = _helper.IncludeNGon(@namespace);
//assert
var expected = String.Concat(String.Format(StartScriptTagFormat, @namespace), @namespace, @".Foo=100;", EndScriptTag);
var actual = result.ToString();
Assert.AreEqual(expected, actual);
}
[Test]
public void MissingNGonInViewBagThrowsException()
{
//arrange
_controller.ViewData.Clear();
//act
//assert
Assert.Throws<InvalidOperationException>(() => _helper.IncludeNGon());
}
[Test]
public void OutputScriptTagFalseOmitsScriptTag()
{
//arrange
_controller.ViewBag.NGon.Foo = 100;
//act
var result = _helper.IncludeNGon(outputScriptTag: false);
//assert
var expected = "window.ngon={};ngon.Foo=100;";
var actual = result.ToString();
Assert.AreEqual(expected, actual);
}
[Test]
public void ExternalJsFileTrueOutputsExternalJSReference()
{
//arrange
_controller.ViewBag.NGon.Foo = 100;
//act
var result = _helper.IncludeNGon(useExternalJSFile: true);
//assert
var expected = @"<script src=""ngon.js"" type=""text/javascript""></script>";
var actual = result.ToString();
Assert.AreEqual(expected, actual);
}
}
}