Skip to content

Commit

Permalink
Merge pull request #3104 from metabrainz/LB-1663
Browse files Browse the repository at this point in the history
LB-1663: Dissapearing "following" list on other users' profiles
  • Loading branch information
anshg1214 authored Jan 7, 2025
2 parents aed06c4 + 6ffe9c2 commit 143d865
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
8 changes: 7 additions & 1 deletion frontend/js/src/user/components/follow/UserSocialNetwork.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ export default class UserSocialNetwork extends React.Component<
user: ListenBrainzUser,
action: "follow" | "unfollow"
) => {
const { currentUser } = this.context;
const { user: profileUser } = this.props;
const { followingList } = this.state;
const newFollowingList = [...followingList];
const index = newFollowingList.findIndex(
Expand All @@ -260,7 +262,11 @@ export default class UserSocialNetwork extends React.Component<
if (action === "follow" && index === -1) {
newFollowingList.push(user.name);
}
if (action === "unfollow" && index !== -1) {
if (
action === "unfollow" &&
index !== -1 &&
profileUser.name === currentUser?.name
) {
newFollowingList.splice(index, 1);
}
this.setState({ followingList: newFollowingList });
Expand Down
52 changes: 42 additions & 10 deletions frontend/js/tests/user/follow/UserSocialNetwork.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const similarUsers = [
},
];

const followingFollowers = ["bob", "fnord"];
const followingFollowers = ["jack", "fnord"];

describe("<UserSocialNetwork />", () => {
afterEach(() => {
Expand Down Expand Up @@ -138,7 +138,7 @@ describe("<UserSocialNetwork />", () => {
];
expect(instance.state.similarUsersList).toEqual(similarUsersInState);

const expectedFollowingFollowersState = ["bob", "fnord"];
const expectedFollowingFollowersState = ["jack", "fnord"];
expect(instance.state.followerList).toEqual(
expectedFollowingFollowersState
);
Expand Down Expand Up @@ -166,14 +166,44 @@ describe("<UserSocialNetwork />", () => {
});

// initial state after first fetch
expect(instance.state.followingList).toEqual(["bob", "fnord"]);
expect(instance.state.followingList).toEqual(["jack", "fnord"]);
await act(async () => {
instance.updateFollowingList({ name: "Baldur" }, "follow");
});
expect(instance.state.followingList).toEqual(["bob", "fnord", "Baldur"]);
expect(instance.state.followingList).toEqual(["jack", "fnord", "Baldur"]);
});

it("updates the state when called with action unfollow", async () => {
const wrapper = mount(
<GlobalAppContext.Provider value={globalContext}>
<ReactQueryWrapper>
<BrowserRouter>
<UserSocialNetwork
user={{
id: 1,
name: "iliekcomputers",
}}
/>
</BrowserRouter>
</ReactQueryWrapper>
</GlobalAppContext.Provider>
);
const instance = wrapper
.find(UserSocialNetwork)
.instance() as UserSocialNetwork;
await act(async () => {
await instance.componentDidMount();
});

// initial state after first fetch
expect(instance.state.followingList).toEqual(["jack", "fnord"]);
await act(async () => {
instance.updateFollowingList({ name: "fnord" }, "unfollow");
});
expect(instance.state.followingList).toEqual(["jack"]);
});

it("does nothing, when called with action unfollow when it's not your own account", async () => {
const wrapper = mount(
<GlobalAppContext.Provider value={globalContext}>
<ReactQueryWrapper>
Expand All @@ -191,11 +221,13 @@ describe("<UserSocialNetwork />", () => {
});

// initial state after first fetch
expect(instance.state.followingList).toEqual(["bob", "fnord"]);
expect(instance.state.followingList).toEqual(["jack", "fnord"]);
await act(async () => {
instance.updateFollowingList({ name: "fnord" }, "unfollow");
});
expect(instance.state.followingList).toEqual(["bob"]);

// Since it's not your own account, it should not be removed from the following list
expect(instance.state.followingList).toEqual(["jack", "fnord"]);
});

it("only allows adding a user once", async () => {
Expand All @@ -217,13 +249,13 @@ describe("<UserSocialNetwork />", () => {
await act(async () => {
instance.updateFollowingList({ name: "Baldur" }, "follow");
});
expect(instance.state.followingList).toEqual(["bob", "fnord", "Baldur"]);
expect(instance.state.followingList).toEqual(["jack", "fnord", "Baldur"]);

// Ensure we can't add a user twice
await act(async () => {
instance.updateFollowingList({ name: "Baldur" }, "follow");
});
expect(instance.state.followingList).toEqual(["bob", "fnord", "Baldur"]);
expect(instance.state.followingList).toEqual(["jack", "fnord", "Baldur"]);
});

it("does nothing when trying to unfollow a user that is not followed", async () => {
Expand All @@ -243,11 +275,11 @@ describe("<UserSocialNetwork />", () => {
await instance.componentDidMount();
});

expect(instance.state.followingList).toEqual(["bob", "fnord"]);
expect(instance.state.followingList).toEqual(["jack", "fnord"]);
await act(async () => {
instance.updateFollowingList({ name: "Baldur" }, "unfollow");
});
expect(instance.state.followingList).toEqual(["bob", "fnord"]);
expect(instance.state.followingList).toEqual(["jack", "fnord"]);
});
});

Expand Down

0 comments on commit 143d865

Please sign in to comment.