-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Web; | ||
using GraphQL.Language.AST; | ||
using GraphQL.Types; | ||
|
||
namespace Our.Umbraco.GraphQL.Adapters.Types | ||
{ | ||
public class HtmlGraphType : ScalarGraphType | ||
{ | ||
public HtmlGraphType() | ||
{ | ||
Name = "HTML"; | ||
Description = "A string containing HTML code."; | ||
} | ||
|
||
public override object ParseLiteral(IValue value) | ||
{ | ||
if (value is StringValue stringValue) | ||
return ParseValue(stringValue.Value); | ||
return null; | ||
} | ||
|
||
public override object ParseValue(object value) | ||
{ | ||
if(value is string stringValue) | ||
return new HtmlString(stringValue); | ||
|
||
return null; | ||
} | ||
|
||
public override object Serialize(object value) | ||
{ | ||
if (value is IHtmlString htmlString) | ||
return htmlString.ToString(); | ||
|
||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
test/Our.Umbraco.GraphQL.Tests/Adapters/Types/HtmlGraphTypeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.Web; | ||
using FluentAssertions; | ||
using GraphQL.Language.AST; | ||
using Our.Umbraco.GraphQL.Adapters.Types; | ||
using Our.Umbraco.GraphQL.Types; | ||
using Xunit; | ||
|
||
namespace Our.Umbraco.GraphQL.Tests.Adapters.Types | ||
{ | ||
public class HtmlGraphTypeTests | ||
{ | ||
[Fact] | ||
public void Serialize_WithHtmlString_ReturnsValueToString() | ||
{ | ||
var idGraphType = new HtmlGraphType(); | ||
var value = "<div>Some HTML</div>"; | ||
var htmlString = new HtmlString(value); | ||
|
||
var serialized = idGraphType.Serialize(htmlString); | ||
|
||
serialized.Should().BeOfType<string>().Which.Should().Be(value); | ||
} | ||
|
||
[Fact] | ||
public void Serialize_WithNull_ReturnsNull() | ||
{ | ||
var idGraphType = new HtmlGraphType(); | ||
|
||
var serialized = idGraphType.Serialize(null); | ||
|
||
serialized.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void ParseValue_WithValue_ReturnsHtmlString() | ||
{ | ||
var idGraphType = new HtmlGraphType(); | ||
var value = "<div>Some HTML</div>"; | ||
|
||
var parsed = idGraphType.ParseValue(value); | ||
|
||
parsed.Should().BeAssignableTo<IHtmlString>().Which.ToString().Should().Be(value); | ||
} | ||
|
||
[Fact] | ||
public void ParseValue_WithNull_ReturnsNull() | ||
{ | ||
var idGraphType = new HtmlGraphType(); | ||
|
||
var parsed = idGraphType.ParseValue(null); | ||
|
||
parsed.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void ParseLiteral_WithStringValue_ReturnsHtmlString() | ||
{ | ||
var idGraphType = new HtmlGraphType(); | ||
var value = "<div>Some HTML</div>"; | ||
|
||
var parsed = idGraphType.ParseLiteral(new StringValue(value)); | ||
|
||
parsed.Should().BeAssignableTo<IHtmlString>().Which.ToString().Should().Be(value); | ||
} | ||
|
||
[Fact] | ||
public void ParseLiteral_WithNull_ReturnsNull() | ||
{ | ||
var idGraphType = new HtmlGraphType(); | ||
|
||
var parsed = idGraphType.ParseLiteral(null); | ||
|
||
parsed.Should().BeNull(); | ||
} | ||
} | ||
} |