File: assert_select_jquery_test.rb

package info (click to toggle)
ruby-jquery-rails 4.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,712 kB
  • sloc: javascript: 20,590; ruby: 184; sh: 49; makefile: 4
file content (91 lines) | stat: -rw-r--r-- 2,967 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
require 'ostruct'
require_relative 'test_helper'
require_relative '../lib/jquery/assert_select'

class AssertSelectJQueryTest < ActiveSupport::TestCase
  include Rails::Dom::Testing::Assertions::SelectorAssertions
  attr_reader :response

  JAVASCRIPT_TEST_OUTPUT = <<-JS
    $("#card").show("blind", 1000);
    $("#id").html('<div><p>something</p></div>');
    $('#card').html('<div><p>something else</p></div>');
    jQuery("#id").replaceWith("<div><p>something</p></div>");
    $("<div><p>something</p></div>").appendTo("#id");
    $("<div><p>something else</p></div>").appendTo("#id");
    jQuery("<div><p>something</p></div>").prependTo("#id");
    $('#id').remove();
    jQuery("#id").hide();
    $("[data-placeholder~=name]").remove();
    $("#cart tr:not(.total_line) > *").remove();
    $("[href|=\"val\"][href$=\"val\"][href^=\"val\"]").remove();
    $("tr + td, li").remove();

    // without semicolon
    $("#browser_cart").hide("blind", 1000)

    $('#item').html('<div><span>\\`Total\\`: \\$12.34</span></div>');
  JS

  setup do
    @response = OpenStruct.new(content_type: 'text/javascript', body: JAVASCRIPT_TEST_OUTPUT)
  end

  def test_target_as_receiver
    assert_nothing_raised do
      assert_select_jquery :show, :blind, '#card'
      assert_select_jquery :hide, :blind, '#browser_cart'
      assert_select_jquery :html, '#id' do
        assert_select 'p', 'something'
      end
      assert_select_jquery :replaceWith, '#id' do
        assert_select 'p', 'something'
      end
      assert_select_jquery :remove, "[data-placeholder~=name]"
      assert_select_jquery :remove, "#cart tr:not(.total_line) > *"
      assert_select_jquery :remove, "[href|=\"val\"][href$=\"val\"][href^=\"val\"]"
      assert_select_jquery :remove, "tr + td, li"

      assert_select_jquery :html, '#item' do
        assert_select 'span', '`Total`: $12.34'
      end
    end

    assert_raise Minitest::Assertion, "No JQuery call matches [:show, :some_wrong]" do
      assert_select_jquery :show, :some_wrong
    end

    assert_raise Minitest::Assertion, "<something else> was expected but was <something>" do
      assert_select_jquery :html, '#id' do
        assert_select 'p', 'something else'
      end
    end
  end

  def test_target_as_argument
    assert_nothing_raised do
      assert_select_jquery :appendTo, '#id' do
        assert_select 'p', 'something'
        assert_select 'p', 'something else'
      end
      assert_select_jquery :prependTo, '#id' do
        assert_select 'p', 'something'
      end
    end

    assert_raise Minitest::Assertion, 'No JQuery call matches [:prependTo, "#wrong_id"]' do
      assert_select_jquery :prependTo, '#wrong_id'
    end
  end

  def test_argumentless
    assert_nothing_raised do
      assert_select_jquery :remove
      assert_select_jquery :hide
    end

    assert_raise Minitest::Assertion, 'No JQuery call matches [:wrong_function]' do
      assert_select_jquery :wrong_function
    end
  end
end