File: capybara_spec.rb

package info (click to toggle)
ruby-capybara 3.36.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,380 kB
  • sloc: ruby: 23,399; javascript: 748; makefile: 11
file content (112 lines) | stat: -rw-r--r-- 3,284 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Capybara do
  describe 'default_max_wait_time' do
    before { @previous_default_time = described_class.default_max_wait_time }

    after { described_class.default_max_wait_time = @previous_default_time } # rubocop:disable RSpec/InstanceVariable

    it 'should be changeable' do
      expect(described_class.default_max_wait_time).not_to eq(5)
      described_class.default_max_wait_time = 5
      expect(described_class.default_max_wait_time).to eq(5)
    end
  end

  describe '.register_driver' do
    it 'should add a new driver' do
      described_class.register_driver :schmoo do |app|
        Capybara::RackTest::Driver.new(app)
      end
      session = Capybara::Session.new(:schmoo, TestApp)
      session.visit('/')
      expect(session.body).to include('Hello world!')
    end
  end

  describe '.register_server' do
    it 'should add a new server' do
      described_class.register_server :blob do |_app, _port, _host|
        # do nothing
      end

      expect(described_class.servers[:blob]).to be_truthy
    end
  end

  describe '.server' do
    after do
      described_class.server = :default
    end

    it 'should default to a proc that calls run_default_server' do
      mock_app = Object.new
      allow(described_class).to receive(:run_default_server).and_return(true)
      described_class.server.call(mock_app, 8000)
      expect(described_class).to have_received(:run_default_server).with(mock_app, 8000)
    end

    it 'should return a custom server proc' do
      server = ->(_app, _port) {}
      described_class.register_server :custom, &server
      described_class.server = :custom
      expect(described_class.server).to eq(server)
    end

    it 'should have :webrick registered' do
      expect(described_class.servers[:webrick]).not_to be_nil
    end

    it 'should have :puma registered' do
      expect(described_class.servers[:puma]).not_to be_nil
    end
  end

  describe 'server=' do
    after do
      described_class.server = :default
    end

    it 'accepts a proc' do
      server = ->(_app, _port) {}
      described_class.server = server
      expect(described_class.server).to eq server
    end
  end

  describe 'app_host' do
    after do
      described_class.app_host = nil
    end

    it 'should warn if not a valid URL' do
      expect { described_class.app_host = 'www.example.com' }.to raise_error(ArgumentError, /Capybara\.app_host should be set to a url/)
    end

    it 'should not warn if a valid URL' do
      expect { described_class.app_host = 'http://www.example.com' }.not_to raise_error
    end

    it 'should not warn if nil' do
      expect { described_class.app_host = nil }.not_to raise_error
    end
  end

  describe 'default_host' do
    around do |test|
      old_default = described_class.default_host
      test.run
      described_class.default_host = old_default
    end

    it 'should raise if not a valid URL' do
      expect { described_class.default_host = 'www.example.com' }.to raise_error(ArgumentError, /Capybara\.default_host should be set to a url/)
    end

    it 'should not warn if a valid URL' do
      expect { described_class.default_host = 'http://www.example.com' }.not_to raise_error
    end
  end
end