-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
has_one_spec.rb
138 lines (119 loc) · 3.32 KB
/
has_one_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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
require "administrate/field/has_one"
require "support/constant_helpers"
require "rails_helper"
require "administrate/resource_resolver"
require "administrate/page"
require "administrate/page/form"
describe Administrate::Field::HasOne do
describe "#nested_form" do
it "returns a form" do
resource = build(:product)
value = double
field = Administrate::Field::HasOne.new(
:product_meta_tag,
value,
:show,
resource: resource,
)
form = field.nested_form
expect(form).to be_present
end
end
describe "#nested_show" do
it "returns a Show" do
product = create(:product)
product_meta_tag = double
field = Administrate::Field::HasOne.new(
:product_meta_tag,
product_meta_tag,
:show,
resource: product,
)
show = field.nested_show
expect(show).to be_a(Administrate::Page::Show)
end
end
describe ".permitted_attribute" do
context "with custom class_name" do
before do
allow(Administrate.deprecator).to receive(:warn)
end
it "returns attributes from correct dashboard" do
field = Administrate::Field::Deferred.new(
Administrate::Field::HasOne,
class_name: :product_meta_tag,
)
field_name = "product_meta_tag"
attributes = field.permitted_attribute(
field_name,
resource_class: Product,
)
expect(attributes[:"#{field_name}_attributes"]).
to eq(%i(meta_title meta_description id))
end
it "triggers a deprecation warning" do
field = Administrate::Field::Deferred.new(
Administrate::Field::HasOne,
class_name: :product_meta_tag,
)
field_name = "product_meta_tag"
field.permitted_attribute(
field_name,
resource_class: Product,
)
expect(Administrate.deprecator).to have_received(:warn).
with(/:class_name is deprecated/)
end
end
end
describe "#to_partial_path" do
it "returns a partial based on the page being rendered" do
resource = double
page = :show
value = double
field = Administrate::Field::HasOne.new(
:product_meta_tag,
value,
page,
resource: resource,
)
path = field.to_partial_path
expect(path).to eq("/fields/has_one/#{page}")
end
end
describe "#linkable?" do
context "when data is persisted" do
it "shows it" do
product_meta_tag = create(:product_meta_tag)
field = described_class.new(
:product_meta_tag,
product_meta_tag,
:show,
)
expect(field).to be_linkable
end
end
context "when data isn't persisted" do
it "doesn't shows it" do
product_meta_tag = build(:product_meta_tag)
field = described_class.new(
:product_meta_tag,
product_meta_tag,
:show,
)
expect(field).to_not be_linkable
end
end
context "when data doesn't respond to `persisted?`" do
it "doesn't show it" do
product_meta_tag = double(:product_meta_tag)
field = described_class.new(
:product_meta_tag,
product_meta_tag,
:show,
)
expect(field).not_to be_linkable
end
end
end
end