Skip to content

Commit

Permalink
feat(WEB): convert last.fm usernames to lowercase
Browse files Browse the repository at this point in the history
  • Loading branch information
niall-byrne committed Mar 15, 2024
1 parent 0c1e472 commit 7367885
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,59 +146,70 @@ describe("UserNameFormContainer", () => {
const mockAction = {
setSubmitting: jest.fn(),
} as never as FormikHelpers<LastFMUserSearchInterface>;
const mockFormContent = { username: "validUsername" };

describe("when the user is logged in", () => {
beforeEach(() => {
mockAuthHook.status = "authenticated";
mockAuthHook.user = mockUserProfile;
arrangeHandleSubmit();
});

describe("when submitted with a username", () => {
beforeEach(() => handleSubmit(mockFormContent, mockAction));

it("should NOT call setSubmitting", () => {
expect(mockAction.setSubmitting).toHaveBeenCalledTimes(0);
});

it("should redirect to the expected route", () => {
const query = new URLSearchParams(mockFormContent);
expect(mockRouterHook.push).toHaveBeenCalledTimes(1);
expect(mockRouterHook.push).toHaveBeenCalledWith(
`${mockRoute}?${query.toString()}`
);
});
});
});

describe("when the user is not logged in", () => {
beforeEach(() => {
mockAuthHook.status = "unauthenticated";
mockAuthHook.user = null;
arrangeHandleSubmit();
});

describe("when submitted with a username", () => {
beforeEach(() => handleSubmit(mockFormContent, mockAction));

it("should call setSubmitting as expected", () => {
expect(mockAction.setSubmitting).toHaveBeenCalledTimes(1);
expect(mockAction.setSubmitting).toHaveBeenCalledWith(false);
describe.each(["camelCaseUserName", "snake-case-user-name"])(
"with a valid username (%s)",

(mockUserName) => {
const mockFormContent = { username: mockUserName };

describe("when the user is logged in", () => {
beforeEach(() => {
mockAuthHook.status = "authenticated";
mockAuthHook.user = mockUserProfile;
arrangeHandleSubmit();
});

describe("when submitted with a username", () => {
beforeEach(() => handleSubmit(mockFormContent, mockAction));

it("should NOT call setSubmitting", () => {
expect(mockAction.setSubmitting).toHaveBeenCalledTimes(0);
});

it("should redirect to the expected route", () => {
const caseConvertedMockFormContent = {
username: mockFormContent.username.toLowerCase(),
};
const query = new URLSearchParams(caseConvertedMockFormContent);

expect(mockRouterHook.push).toHaveBeenCalledTimes(1);
expect(mockRouterHook.push).toHaveBeenCalledWith(
`${mockRoute}?${query.toString()}`
);
});
});
});

it("should generate an error", () => {
expect(mockFormHook.error.open).toHaveBeenCalledTimes(1);
expect(mockFormHook.error.open).toHaveBeenCalledWith(
"session",
_t(lastfmTranslations.search.errors.session.notLoggedIn)
);
describe("when the user is not logged in", () => {
beforeEach(() => {
mockAuthHook.status = "unauthenticated";
mockAuthHook.user = null;
arrangeHandleSubmit();
});

describe("when submitted with a username", () => {
beforeEach(() => handleSubmit(mockFormContent, mockAction));

it("should call setSubmitting as expected", () => {
expect(mockAction.setSubmitting).toHaveBeenCalledTimes(1);
expect(mockAction.setSubmitting).toHaveBeenCalledWith(false);
});

it("should generate an error", () => {
expect(mockFormHook.error.open).toHaveBeenCalledTimes(1);
expect(mockFormHook.error.open).toHaveBeenCalledWith(
"session",
_t(lastfmTranslations.search.errors.session.notLoggedIn)
);
});

it("should NOT redirect", () => {
expect(mockRouterHook.push).toHaveBeenCalledTimes(0);
});
});
});

it("should NOT redirect", () => {
expect(mockRouterHook.push).toHaveBeenCalledTimes(0);
});
});
});
}
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default function UserNameFormContainer({
return;
}
const params = {
[fields.username]: values.username,
[fields.username]: values.username.toLowerCase(),
};
const query = new URLSearchParams(params);
router.push(`${route}?${query.toString()}`);
Expand Down

0 comments on commit 7367885

Please sign in to comment.