Silk-Tests.st 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. Smalltalk createPackage: 'Silk-Tests'!
  2. (Smalltalk packageAt: 'Silk-Tests' ifAbsent: [ self error: 'Package not created: Silk-Tests' ]) imports: {'amber/jquery/Wrappers-JQuery'}!
  3. DOMiteTest subclass: #SilkInheritedTest
  4. slots: {}
  5. package: 'Silk-Tests'!
  6. !SilkInheritedTest methodsFor: 'fixture'!
  7. testedClass
  8. ^ Silk
  9. ! !
  10. TestCase subclass: #SilkTest
  11. slots: {#fixtureDiv}
  12. package: 'Silk-Tests'!
  13. !SilkTest methodsFor: 'fixture'!
  14. assertBodyEndsWith: aString
  15. | sanitizedBody sanitizedAssertion |
  16. sanitizedBody := document body innerHTML replace: '\s*' with: ''.
  17. sanitizedAssertion := aString replace: '\s*' with: ''.
  18. self assert: sanitizedBody size >= sanitizedAssertion size.
  19. self
  20. assert: (sanitizedBody last: sanitizedAssertion size)
  21. equals: sanitizedAssertion
  22. !
  23. assertBodyEndsWithOneOf: aStringArray
  24. | sanitizedBody err |
  25. sanitizedBody := document body innerHTML replace: '\s*' with: ''.
  26. aStringArray do: [ :aString |
  27. | sanitizedAssertion |
  28. sanitizedAssertion := aString replace: '\s*' with: ''.
  29. [ self
  30. assert: sanitizedBody size >= sanitizedAssertion size;
  31. assert: (sanitizedBody last: sanitizedAssertion size)
  32. equals: sanitizedAssertion. ^ self ] on: Error do: [ :e | err := e ]].
  33. err ifNotNil: [ err signal ]
  34. !
  35. setUp
  36. fixtureDiv := document createElement: 'div'.
  37. document body appendChild: fixtureDiv.
  38. fixtureDiv setAttribute: 'id' to: 'fixture'.
  39. fixtureDiv innerHTML: 'sentinel'
  40. !
  41. tearDown
  42. | lastChild |
  43. lastChild := document body lastChild.
  44. self assert: lastChild equals: fixtureDiv.
  45. document body removeChild: lastChild
  46. ! !
  47. !SilkTest methodsFor: 'testing'!
  48. testInsertTable
  49. | d tbl |
  50. d := 'html body div#fixture' asSilk.
  51. tbl := d TABLE.
  52. tbl TR
  53. TD: 'A';
  54. TD: 'B';
  55. TD: 'C'.
  56. tbl TR
  57. TD: 'D';
  58. TD: 'E';
  59. TD: 'F'.
  60. self assertBodyEndsWith: '>sentinel<table><tr><td>A</td><td>B</td><td>C</td></tr><tr><td>D</td><td>E</td><td>F</td></tr></table></div>'
  61. !
  62. testInsertTable2
  63. | d tbl |
  64. d := 'html body div#fixture' asSilk.
  65. tbl := d TABLE.
  66. tbl TR: {
  67. Silk TD: 'A'.
  68. Silk TD: 'B'.
  69. Silk TD: 'C'};
  70. TR: {
  71. Silk TD: 'D'.
  72. Silk TD: 'E'.
  73. Silk TD: 'F'}.
  74. self assertBodyEndsWith: '>sentinel<table><tr><td>A</td><td>B</td><td>C</td></tr><tr><td>D</td><td>E</td><td>F</td></tr></table></div>'
  75. !
  76. testNestedDIVsWithAttributes
  77. "demonstrates how DIVs are nested and given attributes"
  78. | s |
  79. s := '#fixture' asSilk.
  80. s := s DIV << ('id' -> 'container') << ('class' -> 'mySilkContainerClass').
  81. s DIV << ('id' -> 'contentarea') << 'here comes the content'.
  82. s := s DIV << ('id' -> 'toolbar') << ('class' -> 'myToolbarClass').
  83. (s BUTTON: 'do something') on: 'click' bind: [Transcript show: 'button pressed'].
  84. self assertBodyEndsWithOneOf: #(
  85. '>sentinel<div class="mySilkContainerClass" id="container"><div id="contentarea">here comes the content</div><div class="myToolbarClass" id="toolbar"><button>do something</button></div></div></div>'
  86. '>sentinel<div id="container" class="mySilkContainerClass"><div id="contentarea">here comes the content</div><div id="toolbar" class="myToolbarClass"><button>do something</button></div></div></div>'
  87. )
  88. !
  89. testOnClickEvent
  90. "#on:bind"
  91. | s para |
  92. s := '#fixture' asSilk.
  93. para := s P: 'DOM'.
  94. self timeout: 100.
  95. (self async: [para on: 'click' bind:
  96. ["Test successful" self finished].
  97. '#fixture p' asJQuery trigger: 'click'.
  98. ]) fork
  99. ! !