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 62 63 64 65 66 67 68
|
# frozen_string_literal: true
RSpec.shared_examples 'list_preferences_for user' do |list_factory, list_id_attribute|
subject { create(list_factory) } # rubocop:disable Rails/SaveBang
let_it_be(:user) { create(:user) }
describe '#preferences_for' do
context 'when user is nil' do
it 'returns not persisted preferences' do
preferences = subject.preferences_for(nil)
expect(preferences).not_to be_persisted
expect(preferences[list_id_attribute]).to eq(subject.id)
expect(preferences.user_id).to be_nil
end
end
context 'when a user preference already exists' do
before do
subject.update_preferences_for(user, collapsed: true)
end
it 'loads preference for user' do
preferences = subject.preferences_for(user)
expect(preferences).to be_persisted
expect(preferences.collapsed).to eq(true)
end
end
context 'when preferences for user does not exist' do
it 'returns not persisted preferences' do
preferences = subject.preferences_for(user)
expect(preferences).not_to be_persisted
expect(preferences.user_id).to eq(user.id)
expect(preferences.public_send(list_id_attribute)).to eq(subject.id)
end
end
end
describe '#update_preferences_for' do
context 'when user is present' do
context 'when there are no preferences for user' do
it 'creates new user preferences' do
expect { subject.update_preferences_for(user, collapsed: true) }.to change { subject.preferences.count }.by(1)
expect(subject.preferences_for(user).collapsed).to eq(true)
end
end
context 'when there are preferences for user' do
it 'updates user preferences' do
subject.update_preferences_for(user, collapsed: false)
expect { subject.update_preferences_for(user, collapsed: true) }.not_to change { subject.preferences.count }
expect(subject.preferences_for(user).collapsed).to eq(true)
end
end
context 'when user is nil' do
it 'does not create user preferences' do
expect { subject.update_preferences_for(nil, collapsed: true) }.not_to change { subject.preferences.count }
end
end
end
end
end
|