Skip to content

Commit

Permalink
✅ - test: add tests for user format lib
Browse files Browse the repository at this point in the history
  • Loading branch information
svenvandescheur committed Jan 16, 2025
1 parent a03ab84 commit f12dd06
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 3 deletions.
147 changes: 147 additions & 0 deletions frontend/src/lib/format/user.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { User } from "../api/auth";
import { formatUser } from "./user";

describe("formatUser", () => {
test("should return an empty string if the user is null or undefined", () => {
// @ts-expect-error - testing untyped behavior.
expect(formatUser(null)).toBe("");
// @ts-expect-error - testing untyped behavior.
expect(formatUser(undefined)).toBe("");
});

test("should return the username if firstName and lastName are missing", () => {
const user: User = {
username: "johndoe",
firstName: "",
lastName: "",
pk: 0,
email: "",
role: {
canStartDestruction: false,
canReviewDestruction: false,
canCoReviewDestruction: false,
canReviewFinalList: false,
},
};
expect(formatUser(user)).toBe("johndoe");
});

test("should return the first name and username if lastName is missing", () => {
const user: User = {
username: "johndoe",
firstName: "John",
lastName: "",
pk: 0,
email: "",
role: {
canStartDestruction: false,
canReviewDestruction: false,
canCoReviewDestruction: false,
canReviewFinalList: false,
},
};
expect(formatUser(user)).toBe("John (johndoe)");
});

test("should return the last name and username if firstName is missing", () => {
const user: User = {
username: "johndoe",
firstName: "",
lastName: "Doe",
pk: 0,
email: "",
role: {
canStartDestruction: false,
canReviewDestruction: false,
canCoReviewDestruction: false,
canReviewFinalList: false,
},
};
expect(formatUser(user)).toBe("Doe (johndoe)");
});

test("should return full name and username when both firstName and lastName are present", () => {
const user: User = {
username: "johndoe",
firstName: "John",
lastName: "Doe",
pk: 0,
email: "",
role: {
canStartDestruction: false,
canReviewDestruction: false,
canCoReviewDestruction: false,
canReviewFinalList: false,
},
};
expect(formatUser(user)).toBe("John Doe (johndoe)");
});

test("should return full name without username when showUsername is false", () => {
const user: User = {
username: "johndoe",
firstName: "John",
lastName: "Doe",
pk: 0,
email: "",
role: {
canStartDestruction: false,
canReviewDestruction: false,
canCoReviewDestruction: false,
canReviewFinalList: false,
},
};
expect(formatUser(user, { showUsername: false })).toBe("John Doe");
});

test("should return first name without username when showUsername is false and lastName is missing", () => {
const user: User = {
username: "johndoe",
firstName: "John",
lastName: "",
pk: 0,
email: "",
role: {
canStartDestruction: false,
canReviewDestruction: false,
canCoReviewDestruction: false,
canReviewFinalList: false,
},
};
expect(formatUser(user, { showUsername: false })).toBe("John");
});

test("should return last name without username when showUsername is false and firstName is missing", () => {
const user: User = {
username: "johndoe",
firstName: "",
lastName: "Doe",
pk: 0,
email: "",
role: {
canStartDestruction: false,
canReviewDestruction: false,
canCoReviewDestruction: false,
canReviewFinalList: false,
},
};
expect(formatUser(user, { showUsername: false })).toBe("Doe");
});

test("should handle trimming properly when fields contain extra spaces", () => {
const user: User = {
username: "johndoe",
firstName: " John ",
lastName: " Doe ",
pk: 0,
email: "",
role: {
canStartDestruction: false,
canReviewDestruction: false,
canCoReviewDestruction: false,
canReviewFinalList: false,
},
};
expect(formatUser(user)).toBe("John Doe (johndoe)");
});
});
9 changes: 6 additions & 3 deletions frontend/src/lib/format/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ export function formatUser(user: User, { showUsername = true } = {}) {

if (!user.firstName && !user.lastName) return user.username;

if (user.firstName) displayName = displayName.concat(user.firstName, " ");
if (user.lastName) displayName = displayName.concat(user.lastName, " ");
if (showUsername) displayName = displayName.concat(`(${user.username})`);
if (user.firstName)
displayName = displayName.concat(user.firstName.trim(), " ");
if (user.lastName)
displayName = displayName.concat(user.lastName.trim(), " ");
if (showUsername)
displayName = displayName.concat(`(${user.username.trim()})`);

return displayName.trim();
}

0 comments on commit f12dd06

Please sign in to comment.