-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterm_serializer_spec.rb
61 lines (51 loc) · 1.85 KB
/
term_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
55
56
57
58
59
60
61
require 'rails_helper'
describe TermSerializer do
before do
create_all(Term)
end
it 'serializes a term' do
term = create(:term)
expected_hash = {
data: {
id: term.id.to_s,
type: :term,
attributes: { value: term.value, ord: term.ord },
relationships: {
facet: {
data: { id: term.facet.id.to_s, type: :facet }
}
}
}
}
serializer = TermSerializer.new(term)
actual_hash = serializer.serializable_hash
expect(actual_hash).to deep_eq_hash(expected_hash)
end
it 'includes the parent if present' do
term = create(:term_silkscreen)
serializer = TermSerializer.new(term)
actual_hash = serializer.serializable_hash
expected_parent_hash = { data: { id: term.parent.id.to_s, type: :term } }
actual_parent_hash = actual_hash[:data][:relationships][:parent]
expect(actual_parent_hash).to deep_eq_hash(expected_parent_hash)
end
it 'includes the children if present' do
term = create(:term_stencil)
serializer = TermSerializer.new(term)
actual_hash = serializer.serializable_hash
expected_children_hash = { data: term.children.map { |t| { id: t.id.to_s, type: :term } } }
actual_children_hash = actual_hash[:data][:relationships][:children]
expect(actual_children_hash).to deep_eq_hash(expected_children_hash)
end
it 'can deep-include children' do
term = create(:term_stencil)
serializer = TermSerializer.new(term, { include: [:children] })
actual_hash = serializer.serializable_hash
expected_includes = term.children.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