Source code
Revision control
Copy as Markdown
Other Tools
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<head>
<title>script.aculo.us Unit test file</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="../../lib/prototype.js" type="text/javascript"></script>
<script src="../../src/scriptaculous.js" type="text/javascript"></script>
<script src="../../src/unittest.js" type="text/javascript"></script>
<link rel="stylesheet" href="../test.css" type="text/css" />
</head>
<body>
<h1>script.aculo.us Unit test file</h1>
<p>
Tests for Ajax.InPlaceEditor in controls.js
</p>
<!-- Log output -->
<div id="testlog"> </div>
<h1 id="tobeedited">To be edited</h1>
<a id="tobeeditedEditControl" href="#">edit</a>
<div id="tobeeditedMultiLine">First line<br/>Second line<br/>Third line</div>
<!-- Tests follow -->
<script type="text/javascript" language="javascript" charset="utf-8">
// <![CDATA[
new Test.Unit.Runner({
setup: function() { with(this) {
inPlaceEditor = new Ajax.InPlaceEditor($('tobeedited'), '_ajax_inplaceeditor_result.html', {
externalControl: $('tobeeditedEditControl'),
ajaxOptions: {method: 'get'} //override so we can use a static for the result
});
inPlaceEditorMultiLine = new Ajax.InPlaceEditor($('tobeeditedMultiLine'), '_ajax_inplaceeditor_result.html', {
ajaxOptions: {method: 'get'} //override so we can use a static for the result
});
}},
teardown: function() { with(this) {
inPlaceEditor.dispose();
}},
testDisposesProperly: function() { with(this) {
assertEqual("rgba(0, 0, 0, 0)", Element.getStyle('tobeedited','background-color'));
inPlaceEditor.dispose();
assertEqual("rgba(0, 0, 0, 0)", Element.getStyle('tobeedited','background-color'));
assertVisible($('tobeedited'));
Event.simulateMouse('tobeedited','click');
assertVisible($('tobeedited'));
}},
testUsesTextAreaWhenMoreThanOneRows: function() { with(this) {
inPlaceEditor.options.rows = 5;
inPlaceEditor.enterEditMode();
assertEqual("TEXTAREA", document.forms[0].firstChild.tagName);
assertEqual("BR", document.forms[0].childNodes[1].tagName);
}},
testCanSpecifyAllTextsThroughOptions: function() { with(this) {
// swedish translation ;-)
inPlaceEditor.options.okText = "spara";
inPlaceEditor.options.cancelText = "avbryt";
inPlaceEditor.options.savingText = "Sparar...";
inPlaceEditor.enterEditMode();
assertEqual("spara", document.forms[0].lastChild.previousSibling.value);
assertEqual("avbryt", document.forms[0].lastChild.innerHTML);
inPlaceEditor.showSaving();
assertEqual("Sparar...", $('tobeedited').innerHTML);
}},
testCanSpecifyFormIdThroughOptions: function() { with(this) {
inPlaceEditor.enterEditMode();
// default form id
assertEqual("tobeedited-inplaceeditor", document.forms[0].id);
inPlaceEditor.leaveEditMode();
inPlaceEditor.options.formId = "myFormId";
inPlaceEditor.enterEditMode();
assertEqual("myFormId", document.forms[0].id);
}},
testCantEditWhileSaving: function() { with(this) {
inPlaceEditor.onLoading();
Event.simulateMouse('tobeedited','mouseover');
assertEqual("rgba(0, 0, 0, 0)", Element.getStyle('tobeedited','background-color'));
Event.simulateMouse('tobeedited','click');
assertVisible($('tobeedited'));
}},
testCallbackFunctionGetsCalled: function() { with(this) {
called = false;
inPlaceEditor.options.callback = function(form) {
called = true;
}
Event.simulateMouse('tobeedited','click');
Event.simulateMouse(document.forms[0].childNodes[1],'click');
assert(called, "callback was not called");
}},
testCanUseExternalElementToGoIntoEditMode: function() { with(this) {
Event.simulateMouse('tobeeditedEditControl','click');
assertNotNull(document.forms[0], "external control didn't work");
// doesn't work if you click it again while in edit mode
Event.simulateMouse('tobeeditedEditControl','click');
assertNull(document.forms[1], "external control created two forms");
assertNotVisible($('tobeeditedEditControl'));
Event.simulateMouse(document.forms[0].childNodes[2],'click');
assertVisible($('tobeeditedEditControl'));
}},
testHasLineBreaksDetectsHTMLLineBreaks: function() { with(this) {
assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("Line 1<br/>Line 2"));
assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("Line 1<br>Line 2"));
assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("<p>Line 1</p>Line 2"));
assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("Line 1<BR/>Line 2"));
assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("Line 1<BR>Line 2"));
assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("<P>Line 1</P>Line 2"));
assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("<P>Line 1<P>Line 2"));
assert(!inPlaceEditorMultiLine.hasHTMLLineBreaks("One line"));
}},
testConvertsHTMLLineBreaksIntoNewLines: function() { with(this) {
assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("Line 1<br/>Line 2"));
assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("Line 1<br>Line 2"));
// <p> not supported (too hard)
//assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("<p>Line 1</p>Line 2"));
assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("Line 1<BR/>Line 2"));
assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("Line 1<BR>Line 2"));
// <p> not supported (too hard)
//assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("<P>Line 1</P>Line 2"));
// unclosed <P>s not supported yet
//assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("<P>Line 1<P>Line 2"));
// <p> not supported (too hard)
//assertEqual("Line 1\nLine 2\nLine 3\nLine 4", inPlaceEditorMultiLine.convertHTMLLineBreaks("<P>Line 1</P>Line 2<br>Line 3<p>Line 4</P>"));
assertEqual("One line", inPlaceEditorMultiLine.convertHTMLLineBreaks("One line"));
}},
testConvertsParagraphsAndBRsIntoLineBreaks: function() { with(this) {
inPlaceEditorMultiLine.enterEditMode();
assertEqual("TEXTAREA", document.forms[0].firstChild.tagName);
assertEqual("First line\nSecond line\nThird line", document.forms[0].firstChild.value)
// doesn't automatically determine size yet
//assertEqual(3, document.forms[0].firstChild.rows);
}}
});
// ]]>
</script>
</body>
</html>