spec/models/attachy/extension/user/photos_spec.rb in attachy-0.1.0 vs spec/models/attachy/extension/user/photos_spec.rb in attachy-0.1.1

- old
+ new

@@ -15,26 +15,30 @@ expect(user.photos_files).to match_array [photo_1, photo_2] end end describe ':photos_files=' do - let!(:user) { create :user } - let!(:photo_1) { create :file, scope: :photo } - let!(:photo_2) { create :file, scope: :photo } + let!(:user) { create :user } context 'when given records' do - before { user.photos_files = [photo_1, photo_2] } + context 'already persisted' do + let!(:photo_1) { create :file, scope: :photos } + let!(:photo_2) { create :file, scope: :photos } - it 'is saved' do - expect(user.photos_files).to match_array [photo_1, photo_2] + before { user.photos_files = [photo_1, photo_2] } + + it 'references all' do + expect(user.photos_files).to match_array [photo_1, photo_2] + end end end context 'when given no records' do - before { user.photos_files = [photo_1, photo_2] } + let!(:photo_1) { create :file, scope: :photo, attachable: user } + let!(:photo_2) { create :file, scope: :photo, attachable: user } - it 'clears the existents' do + it 'clears all' do user.photos_files = [] expect(user.photos_files).to eq [] end end @@ -53,9 +57,117 @@ let!(:photo_1) { create :file, scope: :photos, attachable: user } let!(:photo_2) { create :file, scope: :photos, attachable: user } it 'returns all records' do expect(user.photos).to match_array [photo_1, photo_2] + end + end + end + + describe ':photos=' do + let!(:user) { create :user } + + context 'with persisted record' do + let!(:photo) { create :file, scope: :photos } + + context 'as a json' do + let!(:json) { photo.to_json } + + it 'does not creates a new one' do + user.photos = json + + expect { user.photos_files }.to_not change(Attachy::File, :count) + end + + it 'is referenced' do + user.photos = json + + expect(user.photos_files).to eq [photo] + end + end + + context 'as an array of json' do + let!(:json) { [photo].to_json } + + it 'does not creates a new one' do + user.photos = json + + expect { user.photos_files }.to_not change(Attachy::File, :count) + end + + it 'is referenced' do + user.photos = json + + expect(user.photos_files).to eq [photo] + end + end + end + + context 'with no persisted record' do + let!(:photo) do + build(:file, + format: :jpg, + height: 600, + public_id: :public_id, + scope: :photo, + width: 800 + ) + end + + context 'as a json' do + let!(:json) { photo.to_json } + + it 'is saved and referenced' do + user.photos = json + + expect(user.photos_files.size).to eq 1 + + file = user.photos_files[0] + + expect(file.format).to eq 'jpg' + expect(file.height).to eq 600 + expect(file.public_id).to eq 'public_id' + expect(file.scope).to eq 'photos' + expect(file.width).to eq 800 + end + end + + context 'as an array of json' do + let!(:json) { [photo].to_json } + + it 'is saved and referenced' do + user.photos = json + + expect(user.photos_files.size).to eq 1 + + file = user.photos_files[0] + + expect(file.format).to eq 'jpg' + expect(file.height).to eq 600 + expect(file.public_id).to eq 'public_id' + expect(file.scope).to eq 'photos' + expect(file.width).to eq 800 + end + end + end + + context 'with an empty string' do + let!(:photo) { create :file, scope: :photos, attachable: user } + + it 'does nothing' do + user.photos = '' + + expect(user.photos_files).to eq [photo] + end + end + + context 'with an empty array as string' do + let!(:photo) { create :file, scope: :photo, attachable: user } + + it 'clears the records' do + user.photos = '[]' + + expect(user.photos_files).to eq [] end end end describe ':photo?' do