Playing
26. Removing friends

Transcript

00:00
Before we move into our feature tests, let's finish off with the ability to remove a friend. So let's just get started. It can remove a friend and let's go ahead and create this out.
00:13
So we're going to need to add a friend first. So once again, let's just bring in two users. That'll be enough. And let's go ahead and add that friend from here.
00:23
So add friend and we'll add that friend. Now we don't necessarily need to accept the friend request before we delete it because we're just going to be removing that record regardless. So to decline a friend request, we're just going to delete this
00:40
and to remove an already accepted friend, we're just going to delete this as well. So let's just go ahead and accept this friend request anyway. So friend and accept friend and user. And then from that user, we're then going to say something like remove friend friend.
01:02
And we want to expect that the user friends has a count, have count of zero. And we also want to do the reverse. So we want to make sure that for the friend friends that also has a count of zero. Let's go ahead and run our tests.
01:24
Of course, this fails. We don't have a remove friend method. So let's go up to where we are adding a friend and let's just do this under accept. Let's create out a remove friend method.
01:42
And of course, we're going to accept a friend into there and basically just detach this record. So we're going to say this friends of mine. Now what that also does is not care about the accepted or unaccepted status because we're referencing the sort of base relationship here.
02:03
And we're just going to go ahead and detach that friend. Let's run this again and we get green. Great. So we could change this around or create another test to not accept the friend request first.
02:17
As you can see, though, if I get rid of accept friend, it still passes. So regardless of whether it's accepted or not, it just detaches that friend relationship completely. And they're no longer friends or the pending friend request is no longer there.
02:33
Okay. So over the last episodes, we've written enough here to cover all of our base functionality over in our user method. Now it's time to head over to the next few episodes and actually build out the UI.

Episode summary

In this episode, we dive into adding the ability for users to remove friends from their friend list. We start by setting up a feature test where we first add a friend (just to have a friend relationship to work with!), and then move on to removing that friend. Interestingly, we see that whether the friendship has been accepted or not, deleting the record works exactly the same way — it simply detaches that relationship between the two users.

We implement a new removeFriend method, making sure it wipes out both pending requests and existing friendships. After writing the test, we run it and ensure it passes, confirming the implementation behaves as expected. As a small experiment, we also test removing a non-accepted friend request, and it works perfectly!

Wrapping up, we realize that with this method, we've now completed all the core user-related functionality for managing friends. Up next, we'll move away from backend logic and start working on the UI to make our app more interactive.

Episode discussion

No comments, yet. Be the first!