-
Notifications
You must be signed in to change notification settings - Fork 420
/
Copy pathobject_serializer_relationship_links_spec.rb
71 lines (58 loc) · 2.29 KB
/
object_serializer_relationship_links_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
require 'spec_helper'
describe FastJsonapi::ObjectSerializer do
include_context 'movie class'
context "params option" do
let(:hash) { serializer.serializable_hash }
context "generating links for a serializer relationship" do
let(:params) { { } }
let(:options_with_params) { { params: params } }
let(:relationship_url) { "http://movies.com/#{movie.id}/relationships/actors" }
let(:related_url) { "http://movies.com/movies/#{movie.name.parameterize}/actors/" }
before(:context) do
class MovieSerializer
has_many :actors, lazy_load_data: false, links: {
self: :actors_relationship_url,
related: -> (object, params = {}) {
"#{params.has_key?(:secure) ? "https" : "http"}://movies.com/movies/#{object.name.parameterize}/actors/"
}
}
end
end
context "with a single record" do
let(:serializer) { MovieSerializer.new(movie, options_with_params) }
let(:links) { hash[:data][:relationships][:actors][:links] }
it "handles relationship links that call a method" do
expect(links).to be_present
expect(links[:self]).to eq(relationship_url)
end
it "handles relationship links that call a proc" do
expect(links).to be_present
expect(links[:related]).to eq(related_url)
end
context "with serializer params" do
let(:params) { { secure: true } }
let(:secure_related_url) { related_url.gsub("http", "https") }
it "passes the params to the link serializer correctly" do
expect(links).to be_present
expect(links[:related]).to eq(secure_related_url)
end
end
end
end
context "lazy loading relationship data" do
before(:context) do
class LazyLoadingMovieSerializer < MovieSerializer
has_many :actors, lazy_load_data: true, links: {
related: :actors_relationship_url
}
end
end
let(:serializer) { LazyLoadingMovieSerializer.new(movie) }
let(:actor_hash) { hash[:data][:relationships][:actors] }
it "does not include the :data key" do
expect(actor_hash).to be_present
expect(actor_hash).not_to have_key(:data)
end
end
end
end