forked from ThoughtWorksInc/Binding.scala
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 changed file
with
35 additions
and
12 deletions.
There are no files selected for viewing
47 changes: 35 additions & 12 deletions
47
dom/src/test/scala/com/thoughtworks/binding/ComponentModel.scala
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 |
---|---|---|
@@ -1,38 +1,61 @@ | ||
package com.thoughtworks.binding | ||
|
||
import com.thoughtworks.binding.Binding.Var | ||
import com.thoughtworks.binding.dom.Runtime.TagsAndTags2 | ||
import org.scalatest.{FreeSpec, Matchers} | ||
import org.scalajs.dom.html.Div | ||
import scalatags.JsDom | ||
|
||
/** | ||
* @author Leonid Turnaev <[email protected]> | ||
*/ | ||
class ComponentModel extends FreeSpec with Matchers { | ||
|
||
"@dom method component" in { | ||
@dom def dialog(id: String): Binding[Div] = <div id={id} class="dialog"/> | ||
@dom def dialog(id: String, caption: Binding[String]): Binding[Div] = <div id={id} class="dialog" data:dialog-caption={caption.bind}/> | ||
|
||
@dom val html = <div>{dialog("message").bind}</div> | ||
val caption = Var("Caption") | ||
@dom val html = <div>{dialog("message", caption).bind}</div> | ||
html.watch() | ||
|
||
assert(html.value.outerHTML == """<div><div class="dialog" id="message"/></div>""") | ||
assert(html.value.outerHTML == """<div><div dialog-caption="Caption" class="dialog" id="message"/></div>""") | ||
caption.value = "New caption" | ||
assert(html.value.outerHTML == """<div><div dialog-caption="New caption" class="dialog" id="message"/></div>""") | ||
} | ||
|
||
"user defined tag component" in { | ||
implicit final class UserTags(x: TagsAndTags2.type) { | ||
object dialog { | ||
def render: Div = { | ||
val div = x.div.render | ||
div.className = "dialog" | ||
div | ||
} | ||
import scala.language.implicitConversions | ||
|
||
class Dialog(val div: Div) { | ||
def caption: String = { | ||
div.getAttribute("dialog-caption") | ||
} | ||
def caption_=(caption: String): Unit = { | ||
div.setAttribute("dialog-caption", caption) | ||
} | ||
} | ||
|
||
object Dialog { | ||
implicit final def toDiv(tag: Dialog): Div = tag.div | ||
|
||
def render: Dialog = { | ||
val div = JsDom.tags.div.render | ||
div.className = "dialog" | ||
new Dialog(div) | ||
} | ||
} | ||
|
||
@dom val html = <div><dialog id="message"/></div> | ||
implicit final class UserTags(x: TagsAndTags2.type) { | ||
val dialog = Dialog | ||
} | ||
|
||
val caption = Var("Caption") | ||
@dom val html = <div><dialog id="message" caption={caption.bind}/></div> | ||
html.watch() | ||
|
||
assert(html.value.outerHTML == """<div><div class="dialog" id="message"/></div>""") | ||
assert(html.value.outerHTML == """<div><div class="dialog" dialog-caption="Caption" id="message"/></div>""") | ||
caption.value = "New caption" | ||
assert(html.value.outerHTML == """<div><div class="dialog" dialog-caption="New caption" id="message"/></div>""") | ||
} | ||
|
||
} |