drag-spec.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { d3, initChart } from './c3-helper'
  2. describe('drag behavior', function() {
  3. 'use strict'
  4. var chart, shapes, $$, totalshapes
  5. var args = {
  6. data: {
  7. x: 'x',
  8. columns: [
  9. ['x', 10, 30, 45, 50, 70, 100],
  10. ['data1', 30, 200, 100, 400, 150, 250],
  11. ['data2', 20, 180, 240, 100, 190]
  12. ],
  13. selection: {
  14. enabled: true,
  15. grouped: true,
  16. multiple: true,
  17. draggable: true
  18. }
  19. }
  20. }
  21. beforeAll(function(done) {
  22. chart = initChart(chart, args, done)
  23. $$ = chart.internal
  24. shapes = $$.main
  25. .selectAll('.' + $$.CLASS.shapes)
  26. .selectAll('.' + $$.CLASS.shape)
  27. totalshapes = shapes.size()
  28. })
  29. it('should contain 15 shapes', function() {
  30. expect(totalshapes).toBe(15)
  31. })
  32. it('should have no selected shapes', function() {
  33. var selected = shapes
  34. .filter(function() {
  35. return d3.select(this).classed($$.CLASS.SELECTED)
  36. })
  37. .size()
  38. expect(selected).toBe(0)
  39. })
  40. describe('Trigger drag events', function() {
  41. beforeAll(function() {
  42. var s = chart.internal.eventRect,
  43. coords1 = [
  44. (s.attr('width') - s.attr('x')) / 3,
  45. (s.attr('height') - s.attr('y')) / 3
  46. ],
  47. coords2 = [
  48. (2 * (s.attr('width') - s.attr('x'))) / 3,
  49. (2 * (s.attr('height') - s.attr('y'))) / 3
  50. ]
  51. $$.dragstart(coords1)
  52. $$.drag(coords2)
  53. $$.dragend()
  54. })
  55. it('should select 6 shapes', function() {
  56. var selected = shapes
  57. .filter(function() {
  58. return d3.select(this).classed($$.CLASS.SELECTED)
  59. })
  60. .size()
  61. expect(selected).toBe(6)
  62. })
  63. it('should select 9 unselected shapes', function() {
  64. var unselected = shapes
  65. .filter(function() {
  66. return !d3.select(this).classed($$.CLASS.SELECTED)
  67. })
  68. .size()
  69. expect(unselected).toBe(9)
  70. })
  71. describe('Selected api', function() {
  72. it('should return 6 selected shapes', function() {
  73. var selected = chart.selected()
  74. expect(selected.length).toBe(6)
  75. })
  76. it('should return 3 selected shapes with targetId = data1', function() {
  77. var selected = chart.selected('data1')
  78. expect(selected.length).toBe(3)
  79. })
  80. })
  81. })
  82. })