use blitz_script::ScriptDocument;
fn value(doc: &mut ScriptDocument, expression: &str) -> serde_json::Value {
doc.eval_json(expression).unwrap_or(serde_json::Value::Null)
}
fn page() -> ScriptDocument {
ScriptDocument::from_html(
"<html><body><div id='host'></div></body></html>",
blitz_dom::DocumentConfig::default(),
)
}
#[test]
fn implementation_is_an_object() {
let mut doc = page();
doc.execute_scripts();
assert_eq!(value(&mut doc, "typeof document.implementation"), "object");
}
#[test]
fn create_html_document_is_callable() {
let mut doc = page();
doc.execute_scripts();
assert_eq!(
value(
&mut doc,
"typeof document.implementation.createHTMLDocument"
),
"function"
);
}
#[test]
fn the_new_document_has_its_own_body() {
let mut doc = page();
doc.execute_scripts();
assert_eq!(
value(
&mut doc,
"(function () {\
var d = document.implementation.createHTMLDocument('');\
return d.body ? 'present' : 'missing';\
})()"
),
"present"
);
assert_eq!(
value(
&mut doc,
"(function () {\
var d = document.implementation.createHTMLDocument('');\
return d.body === document.body;\
})()"
),
false,
"the new document handed back the live page's body"
);
}
#[test]
fn the_title_argument_is_used() {
let mut doc = page();
doc.execute_scripts();
assert_eq!(
value(
&mut doc,
"document.implementation.createHTMLDocument('hello').title"
),
"hello"
);
}
#[test]
fn the_jquery_feature_detect_answers_two() {
let mut doc = page();
doc.execute_scripts();
assert_eq!(
value(
&mut doc,
"(function () {\
var b = document.implementation.createHTMLDocument('').body;\
b.innerHTML = '<form></form><form></form>';\
return b.childNodes.length;\
})()"
),
2
);
}
#[test]
fn the_live_document_is_untouched() {
let mut doc = page();
doc.execute_scripts();
assert_eq!(
value(
&mut doc,
"(function () {\
var before = document.body.childNodes.length;\
var b = document.implementation.createHTMLDocument('').body;\
b.innerHTML = '<form></form><form></form>';\
return document.body.childNodes.length === before;\
})()"
),
true,
"creating a document changed the live page"
);
}