-
Notifications
You must be signed in to change notification settings - Fork 129
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
8 changed files
with
246 additions
and
21 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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,163 @@ | ||
import * as r from "./runtime"; | ||
import S from "s-js"; | ||
|
||
describe("r.hydration", () => { | ||
const container = document.createElement("div"), | ||
_tmpl$ = r.template(`<span><!--#--><!--/--> John</span>`), | ||
_tmpl$2 = r.template(`<div>First</div>`), | ||
_tmpl$3 = r.template(`<div>Last</div>`); | ||
|
||
it("hydrates simple text", () => { | ||
S.root(() => { | ||
r.startSSR(); | ||
const leadingExpr = (function() { | ||
const _el$ = r.getNextElement(_tmpl$), | ||
_el$2 = _el$.firstChild, | ||
_el$3 = _el$2.nextSibling; | ||
r.insert(_el$, "Hi", _el$3); | ||
return _el$; | ||
})(); | ||
r.insert(container, leadingExpr); | ||
}); | ||
expect(container.innerHTML).toBe(`<span _hk="0"><!--#-->Hi<!--/--> John</span>`); | ||
// gather refs | ||
const el1 = container.firstChild, | ||
el2 = el1.firstChild, | ||
el3 = el2.nextSibling, | ||
el4 = el3.nextSibling; | ||
|
||
S.root(() => { | ||
r.hydration(() => { | ||
const leadingExpr = (function() { | ||
const _el$ = r.getNextElement(_tmpl$), | ||
_el$2 = _el$.firstChild, | ||
[_el$3, _co$] = r.getNextMarker(_el$2.nextSibling); | ||
r.hydration(() => r.insert(_el$, "Hi", _el$3, _co$), _el$); | ||
return _el$; | ||
})(); | ||
r.insert(container, leadingExpr, undefined, [...container.childNodes]); | ||
}, container); | ||
}); | ||
expect(container.innerHTML).toBe(`<span _hk="0"><!--#-->Hi<!--/--> John</span>`); | ||
expect(container.firstChild).toBe(el1); | ||
expect(el1.firstChild).toBe(el2); | ||
expect(el2.nextSibling).toBe(el3); | ||
expect(el3.nextSibling).toBe(el4); | ||
}); | ||
|
||
it("hydrates with an updated timestamp", () => { | ||
container.removeChild(container.firstChild); | ||
const time = Date.now(); | ||
S.root(() => { | ||
r.startSSR(); | ||
const leadingExpr = (function() { | ||
const _el$ = r.getNextElement(_tmpl$), | ||
_el$2 = _el$.firstChild, | ||
_el$3 = _el$2.nextSibling; | ||
r.insert(_el$, time, _el$3); | ||
return _el$; | ||
})(); | ||
r.insert(container, leadingExpr); | ||
}); | ||
expect(container.innerHTML).toBe(`<span _hk="0"><!--#-->${time}<!--/--> John</span>`); | ||
// gather refs | ||
const el1 = container.firstChild, | ||
el2 = el1.firstChild, | ||
el3 = el2.nextSibling, | ||
el4 = el3.nextSibling; | ||
|
||
const updatedTime = Date.now(); | ||
S.root(() => { | ||
r.hydration(() => { | ||
const leadingExpr = (function() { | ||
const _el$ = r.getNextElement(_tmpl$), | ||
_el$2 = _el$.firstChild, | ||
[_el$3, _co$] = r.getNextMarker(_el$2.nextSibling); | ||
r.hydration(() => r.insert(_el$, updatedTime, _el$3, _co$), _el$); | ||
return _el$; | ||
})(); | ||
r.insert(container, leadingExpr, undefined, [...container.childNodes]); | ||
}, container); | ||
}); | ||
expect(container.innerHTML).toBe(`<span _hk="0"><!--#-->${updatedTime}<!--/--> John</span>`); | ||
expect(container.firstChild).toBe(el1); | ||
expect(el1.firstChild).toBe(el2); | ||
expect(el2.nextSibling).toBe(el3); | ||
expect(el3.nextSibling).toBe(el4); | ||
}); | ||
|
||
it("hydrates fragments", () => { | ||
container.removeChild(container.firstChild); | ||
r.startSSR(); | ||
S.root(() => { | ||
const multiExpression = [r.getNextElement(_tmpl$2), 'middle', r.getNextElement(_tmpl$3)]; | ||
r.insert(container, multiExpression); | ||
}); | ||
expect(container.innerHTML).toBe(`<div _hk="0">First</div>middle<div _hk="1">Last</div>`); | ||
// gather refs | ||
const el1 = container.firstChild, | ||
el2 = el1.nextSibling, | ||
el3 = el2.nextSibling; | ||
|
||
S.root(() => { | ||
r.hydration(() => { | ||
const multiExpression = [r.getNextElement(_tmpl$2), 'middle', r.getNextElement(_tmpl$3)]; | ||
r.insert(container, multiExpression, undefined, [...container.childNodes]); | ||
}, container); | ||
}); | ||
expect(container.innerHTML).toBe(`<div _hk="0">First</div>middle<div _hk="1">Last</div>`); | ||
expect(container.firstChild).toBe(el1); | ||
expect(el1.nextSibling).toEqual(el2); | ||
expect(el1.nextSibling.nextSibling).toBe(el3); | ||
}); | ||
|
||
it("hydrates fragments with dynamic", () => { | ||
while (container.firstChild) container.removeChild(container.firstChild); | ||
r.startSSR(); | ||
S.root(() => { | ||
const multiExpression = [r.getNextElement(_tmpl$2), () => 'middle', r.getNextElement(_tmpl$3)]; | ||
r.insert(container, multiExpression); | ||
}); | ||
expect(container.innerHTML).toBe(`<div _hk="0">First</div>middle<div _hk="1">Last</div>`); | ||
// gather refs | ||
const el1 = container.firstChild, | ||
el2 = el1.nextSibling, | ||
el3 = el2.nextSibling; | ||
|
||
S.root(() => { | ||
r.hydration(() => { | ||
const multiExpression = [r.getNextElement(_tmpl$2), () => 'middle', r.getNextElement(_tmpl$3)]; | ||
r.insert(container, multiExpression, undefined, [...container.childNodes]); | ||
}, container); | ||
}); | ||
expect(container.innerHTML).toBe(`<div _hk="0">First</div>middle<div _hk="1">Last</div>`); | ||
expect(container.firstChild).toBe(el1); | ||
expect(el1.nextSibling).toEqual(el2); | ||
expect(el1.nextSibling.nextSibling).toBe(el3); | ||
}); | ||
|
||
it("hydrates fragments with dynamic template", () => { | ||
while (container.firstChild) container.removeChild(container.firstChild); | ||
r.startSSR(); | ||
S.root(() => { | ||
const multiExpression = [r.getNextElement(_tmpl$2), () => r.getNextElement(_tmpl$2), r.getNextElement(_tmpl$3)]; | ||
r.insert(container, multiExpression); | ||
}); | ||
expect(container.innerHTML).toBe(`<div _hk="0">First</div><div _hk="2">First</div><div _hk="1">Last</div>`); | ||
// gather refs | ||
const el1 = container.firstChild, | ||
el2 = el1.nextSibling, | ||
el3 = el2.nextSibling; | ||
|
||
S.root(() => { | ||
r.hydration(() => { | ||
const multiExpression = [r.getNextElement(_tmpl$2), () => r.getNextElement(_tmpl$2), r.getNextElement(_tmpl$3)]; | ||
r.insert(container, multiExpression, undefined, [...container.childNodes]); | ||
}, container); | ||
}); | ||
expect(container.innerHTML).toBe(`<div _hk="0">First</div><div _hk="2">First</div><div _hk="1">Last</div>`); | ||
expect(container.firstChild).toBe(el1); | ||
expect(el1.nextSibling).toBe(el2); | ||
expect(el1.nextSibling.nextSibling).toBe(el3); | ||
}); | ||
}); |
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