/
association_spec.rb
64 lines (55 loc) · 2.07 KB
/
association_spec.rb
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
require 'spec_helper'
RSpec.describe RailsAdmin::Config::Fields::Association do
describe '#pretty_value' do
let(:player) { FactoryBot.create(:player, name: '<br />', team: FactoryBot.create(:team)) }
let(:field) { RailsAdmin.config('Team').fields.detect { |f| f.name == :players } }
let(:view) { ActionView::Base.empty }
subject { field.with(object: player.team, view: view).pretty_value }
context 'when the link is disabled' do
let(:view) { ActionView::Base.empty.tap { |d| allow(d).to receive(:action).and_return(nil) } }
it 'does not expose non-HTML-escaped string' do
is_expected.to be_html_safe
is_expected.to eq '<br />'
end
end
context 'when the value is empty' do
let(:team) { FactoryBot.build :team }
subject { field.with(object: team, view: view).pretty_value }
it "returns '-' to show emptiness" do
is_expected.to eq '-'
end
end
end
describe '#removable?', active_record: true do
context 'with non-nullable foreign key' do
let(:field) { RailsAdmin.config('FieldTest').fields.detect { |f| f.name == :nested_field_tests } }
it 'is false' do
expect(field.removable?).to be false
end
end
context 'with nullable foreign key' do
let(:field) { RailsAdmin.config('Team').fields.detect { |f| f.name == :players } }
it 'is true' do
expect(field.removable?).to be true
end
end
context 'with polymorphic has_many' do
let(:field) { RailsAdmin.config('Player').fields.detect { |f| f.name == :comments } }
it 'does not break' do
expect(field.removable?).to be true
end
end
context 'with has_many through' do
before do
class TeamWithHasManyThrough < Team
has_many :drafts
has_many :draft_players, through: :drafts, source: :player
end
end
let(:field) { RailsAdmin.config('TeamWithHasManyThrough').fields.detect { |f| f.name == :draft_players } }
it 'does not break' do
expect(field.removable?).to be true
end
end
end
end