grid.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. casper.test.begin('grid', 73, function (test) {
  2. casper
  3. .start('examples/grid/index.html')
  4. .then(function () {
  5. // headers
  6. test.assertElementCount('th', 2)
  7. test.assertElementCount('th.active', 0)
  8. test.assertSelectorHasText('th:nth-child(1)', 'Name')
  9. test.assertSelectorHasText('th:nth-child(2)', 'Power')
  10. assertTable(test, ['name', 'power'], [
  11. { name: 'Chuck Norris', power: Infinity },
  12. { name: 'Bruce Lee', power: 9000 },
  13. { name: 'Jackie Chan', power: 7000 },
  14. { name: 'Jet Li', power: 8000 }
  15. ])
  16. })
  17. // test sorting
  18. .thenClick('th:nth-child(1)', function () {
  19. test.assertElementCount('th.active:nth-child(1)', 1)
  20. test.assertElementCount('th.active:nth-child(2)', 0)
  21. test.assertElementCount('th:nth-child(1) .arrow.dsc', 1)
  22. test.assertElementCount('th:nth-child(2) .arrow.dsc', 0)
  23. assertTable(test, ['name', 'power'], [
  24. { name: 'Jet Li', power: 8000 },
  25. { name: 'Jackie Chan', power: 7000 },
  26. { name: 'Chuck Norris', power: Infinity },
  27. { name: 'Bruce Lee', power: 9000 }
  28. ])
  29. })
  30. .thenClick('th:nth-child(2)', function () {
  31. test.assertElementCount('th.active:nth-child(1)', 0)
  32. test.assertElementCount('th.active:nth-child(2)', 1)
  33. test.assertElementCount('th:nth-child(1) .arrow.dsc', 1)
  34. test.assertElementCount('th:nth-child(2) .arrow.dsc', 1)
  35. assertTable(test, ['name', 'power'], [
  36. { name: 'Chuck Norris', power: Infinity },
  37. { name: 'Bruce Lee', power: 9000 },
  38. { name: 'Jet Li', power: 8000 },
  39. { name: 'Jackie Chan', power: 7000 }
  40. ])
  41. })
  42. .thenClick('th:nth-child(2)', function () {
  43. test.assertElementCount('th.active:nth-child(1)', 0)
  44. test.assertElementCount('th.active:nth-child(2)', 1)
  45. test.assertElementCount('th:nth-child(1) .arrow.dsc', 1)
  46. test.assertElementCount('th:nth-child(2) .arrow.asc', 1)
  47. assertTable(test, ['name', 'power'], [
  48. { name: 'Jackie Chan', power: 7000 },
  49. { name: 'Jet Li', power: 8000 },
  50. { name: 'Bruce Lee', power: 9000 },
  51. { name: 'Chuck Norris', power: Infinity }
  52. ])
  53. })
  54. .thenClick('th:nth-child(1)', function () {
  55. test.assertElementCount('th.active:nth-child(1)', 1)
  56. test.assertElementCount('th.active:nth-child(2)', 0)
  57. test.assertElementCount('th:nth-child(1) .arrow.asc', 1)
  58. test.assertElementCount('th:nth-child(2) .arrow.asc', 1)
  59. assertTable(test, ['name', 'power'], [
  60. { name: 'Bruce Lee', power: 9000 },
  61. { name: 'Chuck Norris', power: Infinity },
  62. { name: 'Jackie Chan', power: 7000 },
  63. { name: 'Jet Li', power: 8000 }
  64. ])
  65. })
  66. // test search
  67. .then(function () {
  68. this.fill('#search', {
  69. query: 'j'
  70. })
  71. })
  72. .then(function () {
  73. assertTable(test, ['name', 'power'], [
  74. { name: 'Jackie Chan', power: 7000 },
  75. { name: 'Jet Li', power: 8000 }
  76. ])
  77. })
  78. .then(function () {
  79. this.fill('#search', {
  80. query: 'infinity'
  81. })
  82. })
  83. .then(function () {
  84. assertTable(test, ['name', 'power'], [
  85. { name: 'Chuck Norris', power: Infinity }
  86. ])
  87. })
  88. // run
  89. .run(function () {
  90. test.done()
  91. })
  92. /**
  93. * Helper to assert the table data is rendered correctly.
  94. *
  95. * @param {CasperTester} test
  96. * @param {Array} columns
  97. * @param {Array} data
  98. */
  99. function assertTable (test, columns, data) {
  100. test.assertElementCount('td', data.length * columns.length)
  101. for (var i = 0; i < data.length; i++) {
  102. for (var j = 0; j < columns.length; j++) {
  103. test.assertSelectorHasText(
  104. 'tr:nth-child(' + (i + 1) + ') td:nth-child(' + (j + 1) + ')',
  105. data[i][columns[j]]
  106. )
  107. }
  108. }
  109. }
  110. })