@@ -309,4 +309,36 @@ class BlahSerializer
309309 expect ( serializable_hash [ :included ] [ 0 ] [ :links ] [ :self ] ) . to eq url
310310 end
311311 end
312+
313+ context 'when optional attributes are determined by record data' do
314+ it 'returns optional attribute when attribute is included' do
315+ movie . release_year = 2001
316+ json = MovieOptionalRecordDataSerializer . new ( movie ) . serialized_json
317+ serializable_hash = JSON . parse ( json )
318+ expect ( serializable_hash [ 'data' ] [ 'attributes' ] [ 'release_year' ] ) . to eq movie . release_year
319+ end
320+
321+ it "doesn't return optional attribute when attribute is not included" do
322+ movie . release_year = 1970
323+ json = MovieOptionalRecordDataSerializer . new ( movie ) . serialized_json
324+ serializable_hash = JSON . parse ( json )
325+ expect ( serializable_hash [ 'data' ] [ 'attributes' ] . has_key? ( 'release_year' ) ) . to be_falsey
326+ end
327+ end
328+
329+ context 'when optional attributes are determined by params data' do
330+ it 'returns optional attribute when attribute is included' do
331+ movie . director = 'steven spielberg'
332+ json = MovieOptionalParamsDataSerializer . new ( movie , { params : { admin : true } } ) . serialized_json
333+ serializable_hash = JSON . parse ( json )
334+ expect ( serializable_hash [ 'data' ] [ 'attributes' ] [ 'director' ] ) . to eq 'steven spielberg'
335+ end
336+
337+ it "doesn't return optional attribute when attribute is not included" do
338+ movie . director = 'steven spielberg'
339+ json = MovieOptionalParamsDataSerializer . new ( movie , { params : { admin : false } } ) . serialized_json
340+ serializable_hash = JSON . parse ( json )
341+ expect ( serializable_hash [ 'data' ] [ 'attributes' ] . has_key? ( 'director' ) ) . to be_falsey
342+ end
343+ end
312344end
0 commit comments