-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathsideload_spec.rb
177 lines (149 loc) · 5.37 KB
/
sideload_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
169
170
171
172
173
174
175
176
177
require 'spec_helper'
RSpec.describe JsonapiCompliable::Sideload do
let(:opts) { {} }
let(:instance) { described_class.new(:foo, **opts) }
describe '.new' do
it 'assigns a resource class' do
expect(instance.resource_class < JsonapiCompliable::Resource).to eq(true)
expect(instance.resource_class.object_id).to_not eq(JsonapiCompliable::Resource)
end
it "extends the resource with the adapter's sideloading module" do
mod = Module.new do
def foo
'bar'
end
end
adapter = JsonapiCompliable::Adapters::Abstract.new
allow(adapter).to receive(:sideloading_module) { mod }
resource = Class.new(JsonapiCompliable::Resource)
opts[:resource] = resource
allow(resource).to receive(:config) { { adapter: adapter } }
expect(instance.foo).to eq('bar')
end
context 'when passed :resource' do
let(:resource_class) { Class.new(JsonapiCompliable::Resource) }
before do
opts[:resource] = resource_class
end
it 'assigns an instance of that)resource' do
expect(instance.resource_class).to eq(resource_class)
end
end
end
describe '#resolve' do
context 'when polymorphic' do
let(:opts) { { polymorphic: true } }
let(:query_hash) { JsonapiCompliable::Query.default_hash }
let(:query) { double(zero_results?: false, to_hash: { foo: query_hash }) }
let(:parents) { [{ id: 1, type: 'foo' }, { id: 2, type: 'bar' }] }
let(:foo_resource) do
Class.new(JsonapiCompliable::Resource) do
use_adapter JsonapiCompliable::Adapters::Null
end
end
before do
instance.group_by :type
instance.allow_sideload 'foo', resource: foo_resource do
scope { |parents| [{ parent_id: 1 }] }
assign do |parents, children|
parents.each do |parent|
parent[:child] = children.find { |c| c[:parent_id] == parent[:id] }
end
end
end
end
it 'groups parents, then resolves that group' do
instance.resolve(parents, query, instance.name)
expect(parents.first[:child]).to eq({ parent_id: 1 })
end
end
context 'when not polymorphic' do
let(:parents) { [{ id: 1 }] }
let(:query) { double }
let(:results) { [{ parent_id: 1 }] }
let(:base_scope) { double }
let(:scope_proc) { ->(parents) { base_scope } }
let(:scope) do
scope = double
allow(scope).to receive(:resolve).and_yield(results)
scope
end
before do
instance.scope { |parents| base_scope }
instance.assign do |parents, children|
parents.each do |parent|
parent[:child] = children.find { |c| c[:parent_id] == parent[:id] }
end
end
allow(JsonapiCompliable::Scope).to receive(:new)
.and_return(scope)
end
it 'scopes via configured proc' do
expect(scope).to receive(:resolve) { results }
expect(JsonapiCompliable::Scope).to receive(:new)
.with(base_scope, anything, query, default_paginate: false, namespace: :foo)
.and_return(scope)
instance.resolve(parents, query, instance.name)
end
it 'assigns results to parents' do
instance.resolve(parents, query, instance.name)
expect(parents.first[:child]).to eq({ parent_id: 1 })
end
context 'when passed namespace' do
it 'passes namespace to scope builder' do
expect(JsonapiCompliable::Scope).to receive(:new)
.with(base_scope, anything, query, default_paginate: false, namespace: :bar)
.and_return(scope)
instance.resolve(parents, query, :bar)
end
end
end
end
describe '#allow_sideload' do
it 'assigns a new sideload' do
instance.allow_sideload :bar
expect(instance.sideloads[:bar]).to be_a(JsonapiCompliable::Sideload)
end
it 'evaluates the given block in the context of the new sideload' do
instance.allow_sideload :bar do
instance_variable_set(:@foo, 'foo')
end
expect(instance.sideloads[:bar].instance_variable_get(:@foo))
.to eq('foo')
end
context 'when polymorphic' do
before do
opts[:polymorphic] = true
end
it 'adds a new sideload to polymorphic groups' do
instance.allow_sideload :bar
groups = instance.instance_variable_get(:@polymorphic_groups)
expect(groups[:bar]).to be_a(JsonapiCompliable::Sideload)
end
it 'does not add to sideloads' do
instance.allow_sideload :bar
expect(instance.sideloads).to be_empty
end
end
end
describe '#associate' do
before do
instance.instance_variable_set(:@type, :has_many)
end
it 'delegates to adapter' do
expect(instance.resource_class.config[:adapter])
.to receive(:associate).with('parent', 'child', :foo, :has_many)
instance.associate('parent', 'child')
end
context 'when a polymorphic child' do
before do
instance.instance_variable_set(:@parent, double(name: :parent_name))
end
it 'passes parent name as association name' do
expect(instance.resource_class.config[:adapter])
.to receive(:associate).with('parent', 'child', :parent_name, :has_many)
instance.associate('parent', 'child')
end
end
end
end