-
Notifications
You must be signed in to change notification settings - Fork 0
/
facet_serializer_spec.rb
54 lines (47 loc) · 1.71 KB
/
facet_serializer_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
require 'rails_helper'
describe FacetSerializer do
before do
# creating the terms creates the facets by side effect
create_all(Term)
end
it 'serializes a facet' do
facet = create(:facet)
expected_hash = {
data: {
id: facet.id.to_s,
type: :facet,
attributes: { name: facet.name, allow_multiple: facet.allow_multiple },
relationships: {
terms: {
data: facet.terms.map { |t| { id: t.id.to_s, type: :term } }
}
}
}
}
serializer = FacetSerializer.new(facet)
actual_hash = serializer.serializable_hash
expect(actual_hash).to deep_eq_hash(expected_hash)
end
it 'sorts included terms by value' do
facet = create(:facet_medium)
serializer = FacetSerializer.new(facet, { include: [:terms] })
actual_hash = serializer.serializable_hash
expected_includes = facet.terms.order(:value).map { |t| TermSerializer.new(t).serializable_hash[:data] }
actual_includes = actual_hash[:included]
expect(actual_includes.size).to eq(expected_includes.size)
expected_includes.each_with_index do |incl, i|
expect(actual_includes[i]).to deep_eq_hash(incl)
end
end
it 'sorts included terms by ord if present' do
facet = create(:facet_decade)
serializer = FacetSerializer.new(facet, { include: [:terms] })
actual_hash = serializer.serializable_hash
expected_includes = facet.terms.order(:ord, :value).map { |t| TermSerializer.new(t).serializable_hash[:data] }
actual_includes = actual_hash[:included]
expect(actual_includes.size).to eq(expected_includes.size)
expected_includes.each_with_index do |incl, i|
expect(actual_includes[i]).to deep_eq_hash(incl)
end
end
end