Module: Decidim::FrontEndPointerTestHelpers

Defined in:
lib/decidim/dev/test/rspec_support/helpers.rb

Instance Method Summary collapse

Instance Method Details

#drag(selector, mode: "mouse", direction: nil, amount: 0) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/decidim/dev/test/rspec_support/helpers.rb', line 91

def drag(selector, mode: "mouse", direction: nil, amount: 0)
  move =
    case direction
    when "left"
      "x -= #{amount}"
    when "right"
      "x += #{amount}"
    when "top"
      "y -= #{amount}"
    when "bottom"
      "y += #{amount}"
    end

  events =
    if mode == "touch"
      <<~JS
        var evStart = new Event("touchstart");
        evStart.touches = [{ pageX: rect.x, pageY: rect.y }];
        var evMove = new Event("touchmove");
        evMove.touches = [{ pageX: x, pageY: y }];

        element.dispatchEvent(evStart);
        document.dispatchEvent(evMove);
        document.dispatchEvent(new Event("touchend"));
      JS
    else
      <<~JS
        element.dispatchEvent(new MouseEvent("mousedown", { clientX: rect.x, clientY: rect.y }));
        document.dispatchEvent(new MouseEvent("mousemove", { clientX: x, clientY: y }));
        document.dispatchEvent(new MouseEvent("mouseup"));
      JS
    end

  page.execute_script(
    <<~JS
      var element = document.querySelector("#{selector}");
      var rect = element.getBoundingClientRect();

      var x = rect.x;
      var y = rect.y;
      #{move};

      #{events}
    JS
  )
end