-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathconditional_fields_with_key_format_spec.rb
168 lines (131 loc) · 4.04 KB
/
conditional_fields_with_key_format_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
require 'spec_helper'
describe JSONAPI::Serializable::Resource do
let(:klass) do
Class.new(JSONAPI::Serializable::Resource) do
type 'foo'
id { 'bar' }
end
end
let(:object) { User.new }
let(:resource) do
klass.new(object: object, conditional: conditional)
end
context 'when the attribute is conditional and keys are formatted' do
before do
klass.class_eval do
extend JSONAPI::Serializable::Resource::KeyFormat
key_format -> (k) { k.to_s.upcase }
extend JSONAPI::Serializable::Resource::ConditionalFields
end
end
subject { resource.as_jsonapi[:attributes] }
context 'via :if' do
before do
klass.class_eval do
attribute :name, if: proc { @conditional } do
'foo'
end
end
end
context 'and the clause is true' do
let(:conditional) { true }
it { is_expected.to eq(NAME: 'foo') }
end
context 'and the clause is false' do
let(:conditional) { false }
it { is_expected.to be_nil }
end
end
context 'via :unless' do
before do
klass.class_eval do
attribute :name, unless: proc { @conditional } do
'foo'
end
end
end
context 'and the clause is true' do
let(:conditional) { true }
it { is_expected.to be_nil }
end
context 'and the clause is false' do
let(:conditional) { false }
it { is_expected.to eq(NAME: 'foo') }
end
end
end
context 'when relationship is conditional and keys are formatted' do
before do
klass.class_eval do
extend JSONAPI::Serializable::Resource::KeyFormat
key_format -> (k) { k.to_s.upcase }
extend JSONAPI::Serializable::Resource::ConditionalFields
relationship :posts, if: -> { false }
end
end
subject { resource.as_jsonapi[:relationships] }
context 'and the clause is false' do
let(:conditional) { false }
it { is_expected.to be_nil }
end
end
context 'when a link is conditional and keys are formatted' do
before do
klass.class_eval do
extend JSONAPI::Serializable::Resource::KeyFormat
key_format -> (k) { k.to_s.upcase }
extend JSONAPI::Serializable::Resource::ConditionalFields
link :self, if: proc { @conditional } do
'https://example.com/users/42'
end
end
end
subject { resource.as_jsonapi[:links] }
context 'and the clause is true' do
let(:conditional) { true }
it { is_expected.to eq(self: 'https://example.com/users/42') }
end
context 'and the clause is false' do
let(:conditional) { false }
it { is_expected.to be_nil }
end
end
context 'when inheriting and keys are formatted' do
before do
klass.class_eval do
extend JSONAPI::Serializable::Resource::KeyFormat
key_format -> (k) { k.to_s.upcase }
extend JSONAPI::Serializable::Resource::ConditionalFields
relationship :posts, if: -> { false }
end
end
let(:subclass) { Class.new(klass) }
let(:resource) { subclass.new(object: object) }
subject { resource.as_jsonapi[:relationships] }
context 'and the clause is false' do
let(:conditional) { false }
it { is_expected.to be_nil }
end
end
context 'when a field and a link have the same name and keys are formatted' do
before do
klass.class_eval do
extend JSONAPI::Serializable::Resource::KeyFormat
key_format -> (k) { k.to_s.upcase }
extend JSONAPI::Serializable::Resource::ConditionalFields
attribute :name, if: proc { @conditional } do
'attribute'
end
link :name, unless: proc { @conditional } do
'link'
end
end
end
let(:conditional) { true }
subject { resource.as_jsonapi }
it "doesn't override previously registered condition" do
expect(subject[:attributes]).to eq(NAME: 'attribute')
expect(subject).not_to have_key(:links)
end
end
end