Permalink
Browse files

added init tests with manual mocks

  • Loading branch information...
ehelin committed Aug 26, 2018
1 parent e23013f commit d6cc762ab824555072506752bc15b238d3ff0e88
@@ -9,34 +9,57 @@ namespace TestJavaScriptV2
{
public class BaseTest
{
protected bool AssertTrueResult(TestScript script, string test)
{
var rawResult = script.RunTest(test);
var result = JsonConvert.DeserializeObject(rawResult);
bool loaded = Convert.ToBoolean(result);
return loaded;
}
protected object TestNonErrorGetResult(TestScript script, string test)
{
var result = JsonConvert.DeserializeObject(script.RunTest(test));
var result = JsonConvert.DeserializeObject(script.RunTest(test));
return result;
}
protected bool TestErrorThrow(TestScript script, string test, string error = null, string errorType = null)
protected bool TestErrorThrow(
TestScript script,
string test,
string error = null,
string errorType = null,
string errorLocation = null
)
{
try
try
{
script.RunTest(test);
}
catch (Exception ex)
{
if (!string.IsNullOrEmpty(error))
// TODO - simplify logic
if (!string.IsNullOrEmpty(error) && ex.Message.IndexOf(error) != -1
&& string.IsNullOrEmpty(errorType) && string.IsNullOrEmpty(errorType))
{
if (ex.Message.IndexOf(error) != -1
&& ex.Message.IndexOf(errorType) != -1)
{
return true;
}
else
{
return false;
}
return true;
}
else if (!string.IsNullOrEmpty(error) && ex.Message.IndexOf(error) != -1
&& !string.IsNullOrEmpty(errorType) && ex.Message.IndexOf(errorType) != -1
&& string.IsNullOrEmpty(errorLocation))
{
return true;
}
else if (!string.IsNullOrEmpty(error) && ex.Message.IndexOf(error) != -1
&& !string.IsNullOrEmpty(errorType) && ex.Message.IndexOf(errorType) != -1
&& !string.IsNullOrEmpty(errorLocation) && ex.Message.IndexOf(errorLocation) != -1)
{
return true;
}
else
else if (string.IsNullOrEmpty(error)
|| string.IsNullOrEmpty(errorType)
|| string.IsNullOrEmpty(errorLocation))
{
return true;
}
@@ -3,18 +3,109 @@
using JSTest;
using JSTest.ScriptElements;
using JSTest.ScriptLibraries;
using Newtonsoft.Json;
namespace TestJavaScriptV2
{
[TestClass]
public class InitTests : BaseTest
{
[TestMethod]
public void Test_Global_Load_HappyPath()
{
var script = SetUpHappyPathScript();
string contentDivId = "csHtmlContentDiv";
Assert.IsTrue(AssertTrueResult(script, @"return Load('" + contentDivId + "');"));
}
[TestMethod]
public void Test_Global_Init_HappyPath()
{
var script = SetUpHappyPathScript();
Assert.IsTrue(AssertTrueResult(script, @"return Init();"));
}
[TestMethod]
public void Test_Global_Load_ThrowsContentDivIsNullError()
{
var script = CreateJsTestScript(@"..\..\..\..\TgimbaNetCoreWeb\wwwroot\Scripts\HtmlVanillaJs\Global.js");
script = AddOtherHappyPathUsedJsFiles(script);
script.AppendBlock(@"
var document = {
getElementById: function(id) {
return null;
}
};
");
string contentDivId = "csHtmlContentDiv";
Assert.IsTrue(TestErrorThrow(script, @"Load('" + contentDivId + "');", contentDivId, "null", "Global.js"));
script = null;
}
[TestMethod]
public void Test_Global_Load_ThrowsContentDivIsUndefinedError()
{
var script = CreateJsTestScript(@"..\..\..\..\TgimbaNetCoreWeb\wwwroot\Scripts\HtmlVanillaJs\Global.js");
script = AddOtherHappyPathUsedJsFiles(script);
script.AppendBlock(@"
var document = {
getElementById: function(id) {
return undefined;
}
};
");
string contentDivId = "csHtmlContentDiv";
Assert.IsTrue(TestErrorThrow(script, @"Load('" + contentDivId + "');", contentDivId, "undefined", "Global.js"));
script = null;
}
[TestMethod]
public void TestSandbox_DummyTestMethodReturns1()
{
var script = CreateJsTestScript(@"..\..\..\..\TgimbaNetCoreWeb\wwwroot\Scripts\HtmlVanillaJs\Global.js");
script.RunTest(@"assert.equal(1, DummyTestMethodReturns1());");
}
private TestScript SetUpHappyPathScript()
{
var script = CreateJsTestScript(@"..\..\..\..\TgimbaNetCoreWeb\wwwroot\Scripts\HtmlVanillaJs\Global.js");
script = AddOtherHappyPathUsedJsFiles(script);
script.AppendBlock(GetHappyPathMock());
return script;
}
private TestScript AddOtherHappyPathUsedJsFiles(TestScript script)
{
script.AppendFile(@"..\..\..\..\TgimbaNetCoreWeb\wwwroot\Scripts\HtmlVanillaJs\Utilities.js");
script.AppendFile(@"..\..\..\..\TgimbaNetCoreWeb\wwwroot\Scripts\HtmlVanillaJs\ErrorHandler.js");
return script;
}
private string GetHappyPathMock()
{
string globalJsMock = (@"
var document = {
getElementById: function(id) {
return '<div id=\'csHtmlContentDiv\'></div>';
}
};
function ServerCall(subUrl, htmlCtrl) {
if (subUrl === '/Markup/HtmlVanillaJs/main.html'
&& htmlCtrl === '<div id=\'csHtmlContentDiv\'></div>') {
return true;
} else {
return false;
}
}
");
return globalJsMock;
}
}
}
@@ -1,13 +1,13 @@
function Init() {
Load();
return Load("csHtmlContentDiv");
}
function Load() {
var content = document.getElementById("csHtmlContentDiv");
isNullUndefined(content, 'Global.js', "csHtmlContentDiv");
function Load(contentDiv) {
var content = document.getElementById(contentDiv);
ServerCall('/Markup/HtmlVanillaJs/main.html', content);
isNullUndefined(content, 'Global.js', contentDiv);
return ServerCall('/Markup/HtmlVanillaJs/main.html', content);
}
function DummyTestMethodReturns1() {

0 comments on commit d6cc762

Please sign in to comment.