tests
This commit is contained in:
parent
cd8d08c0a5
commit
b6233a2a96
1 changed files with 142 additions and 1 deletions
|
|
@ -55,10 +55,12 @@ var re = regexp.MustCompile(`\s+`)
|
||||||
func compareIgnoreSpaces(t *testing.T, actual, expected string) {
|
func compareIgnoreSpaces(t *testing.T, actual, expected string) {
|
||||||
expected = strings.ReplaceAll(expected, "\n", "")
|
expected = strings.ReplaceAll(expected, "\n", "")
|
||||||
expected = strings.ReplaceAll(expected, "\t", "")
|
expected = strings.ReplaceAll(expected, "\t", "")
|
||||||
expected = re.ReplaceAllString(expected, " ")
|
|
||||||
actual = strings.ReplaceAll(actual, "\n", "")
|
actual = strings.ReplaceAll(actual, "\n", "")
|
||||||
actual = strings.ReplaceAll(actual, "\t", "")
|
actual = strings.ReplaceAll(actual, "\t", "")
|
||||||
actual = re.ReplaceAllString(actual, " ")
|
actual = re.ReplaceAllString(actual, " ")
|
||||||
|
spaceRegex := regexp.MustCompile(`\s+`)
|
||||||
|
actual = strings.TrimSpace(spaceRegex.ReplaceAllString(actual, ""))
|
||||||
|
expected = strings.TrimSpace(spaceRegex.ReplaceAllString(expected, ""))
|
||||||
assert.Equal(t, expected, actual)
|
assert.Equal(t, expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -226,3 +228,142 @@ func TestInjectScriptIfNotExist(t *testing.T) {
|
||||||
}
|
}
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEvalCommands(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
div := Div(Id("test"))
|
||||||
|
result := Render(EvalCommands(div,
|
||||||
|
SetText("hello"),
|
||||||
|
EvalJs(`
|
||||||
|
alert('test')
|
||||||
|
`),
|
||||||
|
SetClassOnParent("myclass"),
|
||||||
|
SetClassOnSibling("div", "myclass"),
|
||||||
|
))
|
||||||
|
|
||||||
|
evalId := ""
|
||||||
|
for _, child := range div.children {
|
||||||
|
switch child.(type) {
|
||||||
|
case *AttributeR:
|
||||||
|
attr := child.(*AttributeR)
|
||||||
|
if attr.Name == "data-eval-commands-id" {
|
||||||
|
evalId = attr.Value
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//language=JavaScript
|
||||||
|
compareIgnoreSpaces(t, result, fmt.Sprintf(`
|
||||||
|
let element = document.querySelector("[data-eval-commands-id='%s']");
|
||||||
|
if(!element) {return;}
|
||||||
|
self = element;
|
||||||
|
self.innerText = 'hello'
|
||||||
|
alert('test')
|
||||||
|
if(!self.parentElement) { return; }
|
||||||
|
element = self.parentElement;
|
||||||
|
element.classList.add('myclass')
|
||||||
|
if(!self.parentElement) { return; }
|
||||||
|
let siblings = self.parentElement.querySelectorAll('div');
|
||||||
|
siblings.forEach(function(element) {
|
||||||
|
element.classList.add('myclass')
|
||||||
|
});
|
||||||
|
`, evalId))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToggleText(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
result := Render(ToggleText("hello", "world"))
|
||||||
|
//language=JavaScript
|
||||||
|
compareIgnoreSpaces(t, result, fmt.Sprintf(`
|
||||||
|
if(self.innerText === "hello") {
|
||||||
|
self.innerText = "world";
|
||||||
|
} else {
|
||||||
|
self.innerText = "hello";
|
||||||
|
}
|
||||||
|
`))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToggleTextOnSibling(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
result := Render(ToggleTextOnSibling("div", "hello", "world"))
|
||||||
|
//language=JavaScript
|
||||||
|
compareIgnoreSpaces(t, result, fmt.Sprintf(`
|
||||||
|
if(!self.parentElement) { return; }
|
||||||
|
let siblings = self.parentElement.querySelectorAll('div');
|
||||||
|
siblings.forEach(function(element){
|
||||||
|
if(element.innerText === "hello"){
|
||||||
|
element.innerText= "world";
|
||||||
|
} else {
|
||||||
|
element.innerText= "hello";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
`))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToggleTextOnChildren(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
result := Render(ToggleTextOnChildren("div", "hello", "world"))
|
||||||
|
//language=JavaScript
|
||||||
|
compareIgnoreSpaces(t, result, fmt.Sprintf(`
|
||||||
|
let children = self.querySelectorAll('div');
|
||||||
|
children.forEach(function(element) {
|
||||||
|
if(element.innerText === "hello") {
|
||||||
|
element.innerText = "world";
|
||||||
|
} else {
|
||||||
|
element.innerText = "hello";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
`))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToggleTextOnParent(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
result := Render(ToggleTextOnParent("hello", "world"))
|
||||||
|
//language=JavaScript
|
||||||
|
compareIgnoreSpaces(t, result, fmt.Sprintf(`
|
||||||
|
if(!self.parentElement) { return; }
|
||||||
|
let element = self.parentElement;
|
||||||
|
|
||||||
|
if(element.innerText === "hello") {
|
||||||
|
element.innerText = "world";
|
||||||
|
} else {
|
||||||
|
element.innerText = "hello";
|
||||||
|
}
|
||||||
|
`))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToggleClassOnChildren(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
result := Render(ToggleClassOnChildren("div", "hidden"))
|
||||||
|
//language=JavaScript
|
||||||
|
compareIgnoreSpaces(t, result, fmt.Sprintf(`
|
||||||
|
let children = self.querySelectorAll('div');
|
||||||
|
children.forEach(function(element) {
|
||||||
|
element.classList.toggle('hidden')
|
||||||
|
});
|
||||||
|
`))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToggleClassOnParent(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
result := Render(ToggleClassOnParent("hidden"))
|
||||||
|
//language=JavaScript
|
||||||
|
compareIgnoreSpaces(t, result, fmt.Sprintf(`
|
||||||
|
if(!self.parentElement) { return; }
|
||||||
|
let element = self.parentElement;
|
||||||
|
element.classList.toggle('hidden')
|
||||||
|
`))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToggleClassOnSibling(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
result := Render(ToggleClassOnSibling("div", "hidden"))
|
||||||
|
//language=JavaScript
|
||||||
|
compareIgnoreSpaces(t, result, fmt.Sprintf(`
|
||||||
|
if(!self.parentElement) { return; }
|
||||||
|
let siblings = self.parentElement.querySelectorAll('div');
|
||||||
|
siblings.forEach(function(element) {
|
||||||
|
element.classList.toggle('hidden')
|
||||||
|
});
|
||||||
|
`))
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue