|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Griesoft.AspNetCore.ReCaptcha.TagHelpers; |
| 5 | +using Microsoft.AspNetCore.Razor.TagHelpers; |
| 6 | +using NUnit.Framework; |
| 7 | + |
| 8 | +namespace ReCaptcha.Tests.TagHelpers |
| 9 | +{ |
| 10 | + [TestFixture] |
| 11 | + public class CallbackScriptTagHelperComponentTests |
| 12 | + { |
| 13 | + private TagHelperOutput _tagHelperOutput; |
| 14 | + |
| 15 | + [SetUp] |
| 16 | + public void Initialize() |
| 17 | + { |
| 18 | + _tagHelperOutput = new TagHelperOutput("body", |
| 19 | + new TagHelperAttributeList(), (useCachedResult, htmlEncoder) => |
| 20 | + { |
| 21 | + var tagHelperContent = new DefaultTagHelperContent(); |
| 22 | + tagHelperContent.SetContent(string.Empty); |
| 23 | + return Task.FromResult<TagHelperContent>(tagHelperContent); |
| 24 | + }); |
| 25 | + } |
| 26 | + |
| 27 | + [Test] |
| 28 | + public void Construction_IsSuccessful() |
| 29 | + { |
| 30 | + // Arrange |
| 31 | + var formId = "formId"; |
| 32 | + |
| 33 | + // Act |
| 34 | + var instance = new CallbackScriptTagHelperComponent(formId); |
| 35 | + |
| 36 | + // Assert |
| 37 | + Assert.NotNull(instance); |
| 38 | + } |
| 39 | + |
| 40 | + [Test] |
| 41 | + public void Constructor_ShouldThrow_WhenFormIdNull() |
| 42 | + { |
| 43 | + // Arrange |
| 44 | + |
| 45 | + |
| 46 | + // Act |
| 47 | + |
| 48 | + |
| 49 | + // Assert |
| 50 | + Assert.Throws<ArgumentNullException>(() => new CallbackScriptTagHelperComponent(null)); |
| 51 | + } |
| 52 | + |
| 53 | + [Test] |
| 54 | + public void CallbackScript_ShouldReturn_ExpectedValue() |
| 55 | + { |
| 56 | + // Arrange |
| 57 | + var formId = "formId"; |
| 58 | + var expectedResult = $"<script>function submit{formId}(token){{document.getElementById('{formId}').submit();}}</script>"; |
| 59 | + |
| 60 | + // Act |
| 61 | + var result = CallbackScriptTagHelperComponent.CallbackScript(formId); |
| 62 | + |
| 63 | + // Assert |
| 64 | + Assert.AreEqual(expectedResult, result); |
| 65 | + } |
| 66 | + |
| 67 | + [Test] |
| 68 | + public void Process_ShouldAppendScript_WhenTagIsBody() |
| 69 | + { |
| 70 | + // Arrange |
| 71 | + var formId = "formId"; |
| 72 | + var comp = new CallbackScriptTagHelperComponent(formId); |
| 73 | + var context = new TagHelperContext("body", new TagHelperAttributeList(), |
| 74 | + new Dictionary<object, object>(), |
| 75 | + Guid.NewGuid().ToString("N")); |
| 76 | + |
| 77 | + // Act |
| 78 | + comp.Process(context, _tagHelperOutput); |
| 79 | + |
| 80 | + // Assert |
| 81 | + Assert.IsTrue(_tagHelperOutput.PostContent.GetContent().Contains(CallbackScriptTagHelperComponent.CallbackScript(formId))); |
| 82 | + } |
| 83 | + |
| 84 | + [Test] |
| 85 | + public void Process_ShouldSkip_WhenTagIsNotBody() |
| 86 | + { |
| 87 | + // Arrange |
| 88 | + var formId = "formId"; |
| 89 | + var comp = new CallbackScriptTagHelperComponent(formId); |
| 90 | + var context = new TagHelperContext("head", new TagHelperAttributeList(), |
| 91 | + new Dictionary<object, object>(), |
| 92 | + Guid.NewGuid().ToString("N")); |
| 93 | + |
| 94 | + // Act |
| 95 | + comp.Process(context, _tagHelperOutput); |
| 96 | + |
| 97 | + // Assert |
| 98 | + Assert.IsTrue(_tagHelperOutput.PostContent.IsEmptyOrWhiteSpace); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments