|
2 | 2 |
|
3 | 3 | describe FastJsonapi::ObjectSerializer do |
4 | 4 | include_context 'movie class' |
| 5 | + include_context 'group class' |
5 | 6 |
|
6 | 7 | context 'when testing instance methods of object serializer' do |
7 | 8 | it 'returns correct hash when serializable_hash is called' do |
|
12 | 13 | serializable_hash = MovieSerializer.new([movie, movie], options).serializable_hash |
13 | 14 |
|
14 | 15 | expect(serializable_hash[:data].length).to eq 2 |
15 | | - expect(serializable_hash[:data][0][:relationships].length).to eq 3 |
| 16 | + expect(serializable_hash[:data][0][:relationships].length).to eq 4 |
16 | 17 | expect(serializable_hash[:data][0][:attributes].length).to eq 2 |
17 | 18 |
|
18 | 19 | expect(serializable_hash[:meta]).to be_instance_of(Hash) |
|
126 | 127 | end |
127 | 128 | end |
128 | 129 |
|
| 130 | + context 'nested includes' do |
| 131 | + it 'has_many to belongs_to: returns correct nested includes when serializable_hash is called' do |
| 132 | + # 3 actors, 3 agencies |
| 133 | + include_object_total = 6 |
| 134 | + |
| 135 | + options = {} |
| 136 | + options[:include] = [:actors, :'actors.agency'] |
| 137 | + serializable_hash = MovieSerializer.new([movie], options).serializable_hash |
| 138 | + |
| 139 | + expect(serializable_hash[:included]).to be_instance_of(Array) |
| 140 | + expect(serializable_hash[:included].length).to eq include_object_total |
| 141 | + (0..include_object_total-1).each do |include| |
| 142 | + expect(serializable_hash[:included][include]).to be_instance_of(Hash) |
| 143 | + end |
| 144 | + |
| 145 | + options[:include] = [:'actors.agency'] |
| 146 | + serializable_hash = MovieSerializer.new([movie], options).serializable_hash |
| 147 | + |
| 148 | + expect(serializable_hash[:included]).to be_instance_of(Array) |
| 149 | + expect(serializable_hash[:included].length).to eq include_object_total |
| 150 | + (0..include_object_total-1).each do |include| |
| 151 | + expect(serializable_hash[:included][include]).to be_instance_of(Hash) |
| 152 | + end |
| 153 | + end |
| 154 | + |
| 155 | + it '`has_many` to `belongs_to` to `belongs_to` - returns correct nested includes when serializable_hash is called' do |
| 156 | + # 3 actors, 3 agencies, 1 state |
| 157 | + include_object_total = 7 |
| 158 | + |
| 159 | + options = {} |
| 160 | + options[:include] = [:actors, :'actors.agency', :'actors.agency.state'] |
| 161 | + serializable_hash = MovieSerializer.new([movie], options).serializable_hash |
| 162 | + |
| 163 | + expect(serializable_hash[:included]).to be_instance_of(Array) |
| 164 | + expect(serializable_hash[:included].length).to eq include_object_total |
| 165 | + |
| 166 | + actors_serialized = serializable_hash[:included].find_all { |included| included[:type] == :actor }.map { |included| included[:id].to_i } |
| 167 | + agencies_serialized = serializable_hash[:included].find_all { |included| included[:type] == :agency }.map { |included| included[:id].to_i } |
| 168 | + states_serialized = serializable_hash[:included].find_all { |included| included[:type] == :state }.map { |included| included[:id].to_i } |
| 169 | + |
| 170 | + movie.actors.each do |actor| |
| 171 | + expect(actors_serialized).to include(actor.id) |
| 172 | + end |
| 173 | + |
| 174 | + agencies = movie.actors.map(&:agency).uniq |
| 175 | + agencies.each do |agency| |
| 176 | + expect(agencies_serialized).to include(agency.id) |
| 177 | + end |
| 178 | + |
| 179 | + states = agencies.map(&:state).uniq |
| 180 | + states.each do |state| |
| 181 | + expect(states_serialized).to include(state.id) |
| 182 | + end |
| 183 | + end |
| 184 | + it 'has_many => has_one returns correct nested includes when serializable_hash is called' do |
| 185 | + options = {} |
| 186 | + options[:include] = [:movies, :'movies.advertising_campaign'] |
| 187 | + serializable_hash = MovieTypeSerializer.new([movie_type], options).serializable_hash |
| 188 | + |
| 189 | + movies_serialized = serializable_hash[:included].find_all { |included| included[:type] == :movie }.map { |included| included[:id].to_i } |
| 190 | + advertising_campaigns_serialized = serializable_hash[:included].find_all { |included| included[:type] == :advertising_campaign }.map { |included| included[:id].to_i } |
| 191 | + |
| 192 | + movies = movie_type.movies |
| 193 | + movies.each do |movie| |
| 194 | + expect(movies_serialized).to include(movie.id) |
| 195 | + end |
| 196 | + |
| 197 | + advertising_campaigns = movies.map(&:advertising_campaign) |
| 198 | + advertising_campaigns.each do |advertising_campaign| |
| 199 | + expect(advertising_campaigns_serialized).to include(advertising_campaign.id) |
| 200 | + end |
| 201 | + end |
| 202 | + it 'polymorphic' do |
| 203 | + options = {} |
| 204 | + options[:include] = [:groupees] |
| 205 | + serializable_hash = GroupSerializer.new([group], options).serializable_hash |
| 206 | + end |
| 207 | + end |
| 208 | + |
129 | 209 | context 'when testing included do block of object serializer' do |
130 | 210 | it 'should set default_type based on serializer class name' do |
131 | 211 | class BlahSerializer |
|
0 commit comments