Created
May 16, 2016 11:18
-
-
Save mjaneczek/adc1bf4ecfc5f3c3a67a5fb855030c41 to your computer and use it in GitHub Desktop.
Interactor + tests example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TeamInteractor | |
def initialize(user:) | |
@user = user | |
end | |
def create(team_params:) | |
@user.teams.create team_params | |
end | |
def invite(team_id:, user_id:) | |
TeamMember.create team_id: team_id, user_id: user_id, status: :pending | |
end | |
def invitations | |
@user.team_members.where(status: :pending) | |
end | |
def accept(invitation_id:) | |
TeamMember.find_by(id: invitation_id).update status: :accepted | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe TeamInteractor do | |
let!(:current_user) { RegisterInteractor.new(user_params: { | |
email: 'test@example.com', password: '123456789', password_confirmation: '123456789' | |
}).call } | |
let!(:second_user) { RegisterInteractor.new(user_params: { | |
email: 'test2@example.com', password: '123456789', password_confirmation: '123456789' | |
}).call } | |
let!(:example_team) { | |
TeamInteractor.new(user: current_user).create team_params: { name: 'Example team', description: 'Example description' } | |
} | |
it 'creates an invitation' do | |
interactor = described_class.new user: current_user | |
invitation = interactor.invite team_id: example_team.id, user_id: second_user.id | |
expect(invitation.user).to eq(second_user) | |
expect(invitation.team).to eq(example_team) | |
end | |
context 'with pending invitation' do | |
before(:each) do | |
interactor = described_class.new user: current_user | |
invitation = interactor.invite team_id: example_team.id, user_id: second_user.id | |
end | |
it 'returns invitations' do | |
interactor = described_class.new user: second_user | |
invitations = interactor.invitations | |
expect(invitations.count).to eq(1) | |
expect(invitations.first.team).to eq(example_team) | |
end | |
it 'accepts an invitation' do | |
interactor = described_class.new user: second_user | |
invitation = interactor.invitations.first | |
expect(interactor.accept invitation_id: invitation.id).to eq(true) | |
end | |
it 'removes an invitation from list after accept' do | |
interactor = described_class.new user: second_user | |
invitation = interactor.invitations.first | |
interactor.accept invitation_id: invitation.id | |
expect(interactor.invitations.count).to eq(0) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment