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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Web.TestUtil;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.Mvc.Ajax.Test
{
public class AjaxOptionsTest
{
[Fact]
public void InsertionModeProperty()
{
// Arrange
AjaxOptions options = new AjaxOptions();
// Act & Assert
MemberHelper.TestEnumProperty(options, "InsertionMode", InsertionMode.Replace, false);
}
[Fact]
public void InsertionModePropertyExceptionText()
{
// Arrange
AjaxOptions options = new AjaxOptions();
// Act & Assert
Assert.ThrowsArgumentOutOfRange(
delegate { options.InsertionMode = (InsertionMode)4; },
"value",
@"Specified argument was out of the range of valid values.");
}
[Fact]
public void InsertionModeStringTests()
{
// Act & Assert
Assert.Equal(new AjaxOptions { InsertionMode = InsertionMode.Replace }.InsertionModeString, "Sys.Mvc.InsertionMode.replace");
Assert.Equal(new AjaxOptions { InsertionMode = InsertionMode.InsertAfter }.InsertionModeString, "Sys.Mvc.InsertionMode.insertAfter");
Assert.Equal(new AjaxOptions { InsertionMode = InsertionMode.InsertBefore }.InsertionModeString, "Sys.Mvc.InsertionMode.insertBefore");
}
[Fact]
public void InsertionModeUnobtrusiveTests()
{
// Act & Assert
Assert.Equal(new AjaxOptions { InsertionMode = InsertionMode.Replace }.InsertionModeUnobtrusive, "replace");
Assert.Equal(new AjaxOptions { InsertionMode = InsertionMode.InsertAfter }.InsertionModeUnobtrusive, "after");
Assert.Equal(new AjaxOptions { InsertionMode = InsertionMode.InsertBefore }.InsertionModeUnobtrusive, "before");
}
[Fact]
public void HttpMethodProperty()
{
// Arrange
AjaxOptions options = new AjaxOptions();
// Act & Assert
MemberHelper.TestStringProperty(options, "HttpMethod", String.Empty);
}
[Fact]
public void OnBeginProperty()
{
// Arrange
AjaxOptions options = new AjaxOptions();
// Act & Assert
MemberHelper.TestStringProperty(options, "OnBegin", String.Empty);
}
[Fact]
public void OnFailureProperty()
{
// Arrange
AjaxOptions options = new AjaxOptions();
// Act & Assert
MemberHelper.TestStringProperty(options, "OnFailure", String.Empty);
}
[Fact]
public void OnSuccessProperty()
{
// Arrange
AjaxOptions options = new AjaxOptions();
// Act & Assert
MemberHelper.TestStringProperty(options, "OnSuccess", String.Empty);
}
[Fact]
public void ToJavascriptStringWithEmptyOptions()
{
string s = (new AjaxOptions()).ToJavascriptString();
Assert.Equal("{ insertionMode: Sys.Mvc.InsertionMode.replace }", s);
}
[Fact]
public void ToJavascriptString()
{
// Arrange
AjaxOptions options = new AjaxOptions
{
InsertionMode = InsertionMode.InsertBefore,
Confirm = "confirm",
HttpMethod = "POST",
LoadingElementId = "loadingElement",
UpdateTargetId = "someId",
Url = "http://someurl.com",
OnBegin = "some_begin_function",
OnComplete = "some_complete_function",
OnFailure = "some_failure_function",
OnSuccess = "some_success_function",
};
// Act
string s = options.ToJavascriptString();
// Assert
Assert.Equal("{ insertionMode: Sys.Mvc.InsertionMode.insertBefore, " +
"confirm: 'confirm', " +
"httpMethod: 'POST', " +
"loadingElementId: 'loadingElement', " +
"updateTargetId: 'someId', " +
"url: 'http://someurl.com', " +
"onBegin: Function.createDelegate(this, some_begin_function), " +
"onComplete: Function.createDelegate(this, some_complete_function), " +
"onFailure: Function.createDelegate(this, some_failure_function), " +
"onSuccess: Function.createDelegate(this, some_success_function) }", s);
}
[Fact]
public void ToJavascriptStringEscapesQuotesCorrectly()
{
// Arrange
AjaxOptions options = new AjaxOptions
{
InsertionMode = InsertionMode.InsertBefore,
Confirm = @"""confirm""",
HttpMethod = "POST",
LoadingElementId = "loading'Element'",
UpdateTargetId = "someId",
Url = "http://someurl.com",
OnBegin = "some_begin_function",
OnComplete = "some_complete_function",
OnFailure = "some_failure_function",
OnSuccess = "some_success_function",
};
// Act
string s = options.ToJavascriptString();
// Assert
Assert.Equal("{ insertionMode: Sys.Mvc.InsertionMode.insertBefore, " +
@"confirm: '""confirm""', " +
"httpMethod: 'POST', " +
@"loadingElementId: 'loading\'Element\'', " +
"updateTargetId: 'someId', " +
"url: 'http://someurl.com', " +
"onBegin: Function.createDelegate(this, some_begin_function), " +
"onComplete: Function.createDelegate(this, some_complete_function), " +
"onFailure: Function.createDelegate(this, some_failure_function), " +
"onSuccess: Function.createDelegate(this, some_success_function) }", s);
}
[Fact]
public void ToJavascriptStringWithOnlyUpdateTargetId()
{
// Arrange
AjaxOptions options = new AjaxOptions { UpdateTargetId = "someId" };
// Act
string s = options.ToJavascriptString();
// Assert
Assert.Equal("{ insertionMode: Sys.Mvc.InsertionMode.replace, updateTargetId: 'someId' }", s);
}
[Fact]
public void ToJavascriptStringWithUpdateTargetIdAndExplicitInsertionMode()
{
// Arrange
AjaxOptions options = new AjaxOptions { InsertionMode = InsertionMode.InsertAfter, UpdateTargetId = "someId" };
// Act
string s = options.ToJavascriptString();
// Assert
Assert.Equal("{ insertionMode: Sys.Mvc.InsertionMode.insertAfter, updateTargetId: 'someId' }", s);
}
[Fact]
public void ToUnobtrusiveHtmlAttributesWithEmptyOptions()
{
// Arrange
AjaxOptions options = new AjaxOptions();
// Act
IDictionary<string, object> attributes = options.ToUnobtrusiveHtmlAttributes();
// Assert
Assert.Single(attributes);
Assert.Equal("true", attributes["data-ajax"]);
}
[Fact]
public void ToUnobtrusiveHtmlAttributes()
{
// Arrange
AjaxOptions options = new AjaxOptions
{
InsertionMode = InsertionMode.InsertBefore,
Confirm = "confirm",
HttpMethod = "POST",
LoadingElementId = "loadingElement",
LoadingElementDuration = 450,
UpdateTargetId = "someId",
Url = "http://someurl.com",
OnBegin = "some_begin_function",
OnComplete = "some_complete_function",
OnFailure = "some_failure_function",
OnSuccess = "some_success_function",
};
// Act
var attributes = options.ToUnobtrusiveHtmlAttributes();
// Assert
Assert.Equal(12, attributes.Count);
Assert.Equal("true", attributes["data-ajax"]);
Assert.Equal("confirm", attributes["data-ajax-confirm"]);
Assert.Equal("POST", attributes["data-ajax-method"]);
Assert.Equal("#loadingElement", attributes["data-ajax-loading"]);
Assert.Equal(450, attributes["data-ajax-loading-duration"]);
Assert.Equal("http://someurl.com", attributes["data-ajax-url"]);
Assert.Equal("#someId", attributes["data-ajax-update"]);
Assert.Equal("before", attributes["data-ajax-mode"]);
Assert.Equal("some_begin_function", attributes["data-ajax-begin"]);
Assert.Equal("some_complete_function", attributes["data-ajax-complete"]);
Assert.Equal("some_failure_function", attributes["data-ajax-failure"]);
Assert.Equal("some_success_function", attributes["data-ajax-success"]);
}
[Fact]
public void ToUnobtrusiveHtmlAttributesWithOnlyUpdateTargetId()
{
// Arrange
AjaxOptions options = new AjaxOptions { UpdateTargetId = "someId" };
// Act
var attributes = options.ToUnobtrusiveHtmlAttributes();
// Assert
Assert.Equal(3, attributes.Count);
Assert.Equal("true", attributes["data-ajax"]);
Assert.Equal("#someId", attributes["data-ajax-update"]);
Assert.Equal("replace", attributes["data-ajax-mode"]); // Only added when UpdateTargetId is set
}
[Fact]
public void ToUnobtrusiveHtmlAttributesWithUpdateTargetIdAndExplicitInsertionMode()
{
// Arrange
AjaxOptions options = new AjaxOptions
{
InsertionMode = InsertionMode.InsertAfter,
UpdateTargetId = "someId"
};
// Act
var attributes = options.ToUnobtrusiveHtmlAttributes();
// Assert
Assert.Equal(3, attributes.Count);
Assert.Equal("true", attributes["data-ajax"]);
Assert.Equal("#someId", attributes["data-ajax-update"]);
Assert.Equal("after", attributes["data-ajax-mode"]);
}
[Fact]
public void UpdateTargetIdProperty()
{
// Arrange
AjaxOptions options = new AjaxOptions();
// Act & Assert
MemberHelper.TestStringProperty(options, "UpdateTargetId", String.Empty);
}
}
}
|