Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking to see if item is "clickable" in HyperSpec

I have a Hyperstack component like this:

      render do
        LI(class: class_names, title: conflicts || '',
           data: { toggle: :tooltip, placement: 'auto left', container: :body }) do
          DIV(class: 'row paper-row') do
            DIV(class: 'col-sm-12 text-left') do
              P(class: 'medium-weight', id: 'tp_envelopes') { params.envelope.name }
            end
          end
        end.on(:click) { select_envelope }
      end

I want the on(:click) handler to be conditional if the conflicts method returns a non-nil value.

I also wonder how I can test this in hyperspec/capaybara ... i.e. how do I check to see if an html element responds to a :click event?

like image 622
Mitch VanDuyn Avatar asked Apr 11 '19 13:04

Mitch VanDuyn


3 Answers

You could turn off/on the click handler conditionally by using

end.tap { |item| item.on(:click) { select_envelope } unless conflicts }

to replace

end.on(:click) { select_envelope }
like image 164
Todd J Russell Avatar answered Oct 20 '22 16:10

Todd J Russell


end.on(:click) { select_envelope }

You could replace (:click) with:

(!conflicts && :click)

This works because if conflicts is not nil, this would result in a nil click handler that doesn't do anything.

like image 2
Matt Saginario Avatar answered Oct 20 '22 17:10

Matt Saginario


I'm not sure that checking the element for a click event would be reliable due to event bubbling.

I would write the test to click the element, and then expect whatever logic is in the click event to not happen.

conflicts = true

element.click() # trigger select_envelope

expect(selected_envelope).to be_nil # no envelope should be selected due to conflicts
like image 1
iamprich Avatar answered Oct 20 '22 15:10

iamprich