Skip to content

Commit ae23444

Browse files
committed
Refactor LoginPage tests
1 parent f748cc0 commit ae23444

8 files changed

+143
-178
lines changed

site/src/pages/LoginPage/LoginPage.test.tsx

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { fireEvent, screen } from "@testing-library/react";
22
import userEvent from "@testing-library/user-event";
33
import { rest } from "msw";
44
import { createMemoryRouter } from "react-router-dom";
5-
import { Language } from "./SignInForm/SignInForm";
5+
import { Language } from "./SignInForm";
66
import {
77
render,
88
renderWithRouter,
99
waitForLoaderToBeRemoved,
10-
} from "../../testHelpers/renderHelpers";
11-
import { server } from "../../testHelpers/server";
10+
} from "testHelpers/renderHelpers";
11+
import { server } from "testHelpers/server";
1212
import { LoginPage } from "./LoginPage";
1313
import * as TypesGen from "api/typesGenerated";
1414
import { i18n } from "i18n";
@@ -25,14 +25,6 @@ describe("LoginPage", () => {
2525
);
2626
});
2727

28-
it("renders the sign-in form", async () => {
29-
// When
30-
render(<LoginPage />);
31-
32-
// Then
33-
await screen.findByText(Language.passwordSignIn);
34-
});
35-
3628
it("shows an error message if SignIn fails", async () => {
3729
// Given
3830
const apiErrorMessage = "Something wrong happened";
@@ -59,28 +51,6 @@ describe("LoginPage", () => {
5951
expect(errorMessage).toBeDefined();
6052
});
6153

62-
it("shows github authentication when enabled", async () => {
63-
const authMethods: TypesGen.AuthMethods = {
64-
password: { enabled: true },
65-
github: { enabled: true },
66-
oidc: { enabled: true, signInText: "", iconUrl: "" },
67-
};
68-
69-
// Given
70-
server.use(
71-
rest.get("/api/v2/users/authmethods", async (req, res, ctx) => {
72-
return res(ctx.status(200), ctx.json(authMethods));
73-
}),
74-
);
75-
76-
// When
77-
render(<LoginPage />);
78-
79-
// Then
80-
expect(screen.queryByText(Language.passwordSignIn)).not.toBeInTheDocument();
81-
await screen.findByText(Language.githubSignIn);
82-
});
83-
8454
it("redirects to the setup page if there is no first user", async () => {
8555
// Given
8656
server.use(
Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,62 @@
11
import { action } from "@storybook/addon-actions";
2-
import { ComponentMeta, Story } from "@storybook/react";
32
import { MockAuthMethods } from "testHelpers/entities";
4-
import { LoginPageView, LoginPageViewProps } from "./LoginPageView";
3+
import { LoginPageView } from "./LoginPageView";
4+
import type { Meta, StoryObj } from "@storybook/react";
55

6-
export default {
6+
const meta: Meta<typeof LoginPageView> = {
77
title: "pages/LoginPageView",
88
component: LoginPageView,
9-
} as ComponentMeta<typeof LoginPageView>;
9+
};
1010

11-
const Template: Story<LoginPageViewProps> = (args) => (
12-
<LoginPageView {...args} />
13-
);
11+
export default meta;
12+
type Story = StoryObj<typeof LoginPageView>;
1413

15-
export const Example = Template.bind({});
16-
Example.args = {
17-
isLoading: false,
18-
onSignIn: action("onSignIn"),
19-
context: {
20-
data: {
21-
authMethods: MockAuthMethods,
22-
hasFirstUser: false,
14+
export const Example: Story = {
15+
args: {
16+
isLoading: false,
17+
onSignIn: action("onSignIn"),
18+
context: {
19+
data: {
20+
authMethods: MockAuthMethods,
21+
hasFirstUser: false,
22+
},
2323
},
2424
},
2525
};
2626

2727
const err = new Error("Username or email are wrong.");
2828

29-
export const AuthError = Template.bind({});
30-
AuthError.args = {
31-
isLoading: false,
32-
onSignIn: action("onSignIn"),
33-
context: {
34-
error: err,
35-
data: {
36-
authMethods: MockAuthMethods,
37-
hasFirstUser: false,
29+
export const AuthError: Story = {
30+
args: {
31+
isLoading: false,
32+
onSignIn: action("onSignIn"),
33+
context: {
34+
error: err,
35+
data: {
36+
authMethods: MockAuthMethods,
37+
hasFirstUser: false,
38+
},
3839
},
3940
},
4041
};
4142

42-
export const LoadingInitialData = Template.bind({});
43-
LoadingInitialData.args = {
44-
isLoading: true,
45-
onSignIn: action("onSignIn"),
46-
context: {},
43+
export const LoadingInitialData: Story = {
44+
args: {
45+
isLoading: true,
46+
onSignIn: action("onSignIn"),
47+
context: {},
48+
},
4749
};
4850

49-
export const SigningIn = Template.bind({});
50-
SigningIn.args = {
51-
isSigningIn: true,
52-
onSignIn: action("onSignIn"),
53-
context: {
54-
data: {
55-
authMethods: MockAuthMethods,
56-
hasFirstUser: false,
51+
export const SigningIn: Story = {
52+
args: {
53+
isSigningIn: true,
54+
onSignIn: action("onSignIn"),
55+
context: {
56+
data: {
57+
authMethods: MockAuthMethods,
58+
hasFirstUser: false,
59+
},
5760
},
5861
},
5962
};

site/src/pages/LoginPage/SignInForm/OAuthSignInForm.tsx renamed to site/src/pages/LoginPage/OAuthSignInForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import GitHubIcon from "@mui/icons-material/GitHub";
44
import KeyIcon from "@mui/icons-material/VpnKey";
55
import Box from "@mui/material/Box";
66
import { Language } from "./SignInForm";
7-
import { AuthMethods } from "../../../api/typesGenerated";
7+
import { AuthMethods } from "api/typesGenerated";
88
import { FC } from "react";
99
import { makeStyles } from "@mui/styles";
1010

site/src/pages/LoginPage/SignInForm/PasswordSignInForm.tsx renamed to site/src/pages/LoginPage/PasswordSignInForm.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Stack } from "../../../components/Stack/Stack";
1+
import { Stack } from "components/Stack/Stack";
22
import TextField from "@mui/material/TextField";
3-
import { getFormHelpers, onChangeTrimmed } from "../../../utils/formUtils";
4-
import { LoadingButton } from "../../../components/LoadingButton/LoadingButton";
3+
import { getFormHelpers, onChangeTrimmed } from "utils/formUtils";
4+
import { LoadingButton } from "components/LoadingButton/LoadingButton";
55
import { Language } from "./SignInForm";
66
import { FormikContextType, FormikTouched, useFormik } from "formik";
77
import * as Yup from "yup";
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { mockApiError } from "testHelpers/entities";
2+
import { SignInForm } from "./SignInForm";
3+
import type { Meta, StoryObj } from "@storybook/react";
4+
5+
const meta: Meta<typeof SignInForm> = {
6+
title: "components/SignInForm",
7+
component: SignInForm,
8+
args: {
9+
isSigningIn: false,
10+
},
11+
};
12+
13+
export default meta;
14+
type Story = StoryObj<typeof SignInForm>;
15+
16+
export const SignedOut: Story = {};
17+
18+
export const SigningIn: Story = {
19+
args: {
20+
isSigningIn: true,
21+
authMethods: {
22+
password: { enabled: true },
23+
github: { enabled: true },
24+
oidc: { enabled: false, signInText: "", iconUrl: "" },
25+
},
26+
},
27+
};
28+
29+
export const WithError: Story = {
30+
args: {
31+
error: mockApiError({
32+
message: "Email or password was invalid",
33+
validations: [
34+
{
35+
field: "password",
36+
detail: "Password is invalid.",
37+
},
38+
],
39+
}),
40+
initialTouched: {
41+
password: true,
42+
},
43+
},
44+
};
45+
46+
export const WithGithub: Story = {
47+
args: {
48+
authMethods: {
49+
password: { enabled: true },
50+
github: { enabled: true },
51+
oidc: { enabled: false, signInText: "", iconUrl: "" },
52+
},
53+
},
54+
};
55+
56+
export const WithOIDC: Story = {
57+
args: {
58+
authMethods: {
59+
password: { enabled: true },
60+
github: { enabled: false },
61+
oidc: { enabled: true, signInText: "", iconUrl: "" },
62+
},
63+
},
64+
};
65+
66+
export const WithOIDCWithoutPassword: Story = {
67+
args: {
68+
authMethods: {
69+
password: { enabled: false },
70+
github: { enabled: false },
71+
oidc: { enabled: true, signInText: "", iconUrl: "" },
72+
},
73+
},
74+
};
75+
76+
export const WithoutAny: Story = {
77+
args: {
78+
authMethods: {
79+
password: { enabled: false },
80+
github: { enabled: false },
81+
oidc: { enabled: false, signInText: "", iconUrl: "" },
82+
},
83+
},
84+
};
85+
86+
export const WithGithubAndOIDC: Story = {
87+
args: {
88+
authMethods: {
89+
password: { enabled: true },
90+
github: { enabled: true },
91+
oidc: { enabled: true, signInText: "", iconUrl: "" },
92+
},
93+
},
94+
};

site/src/pages/LoginPage/SignInForm/SignInForm.tsx renamed to site/src/pages/LoginPage/SignInForm.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { makeStyles } from "@mui/styles";
22
import { FormikTouched } from "formik";
33
import { FC, useState } from "react";
4-
import { AuthMethods } from "../../../api/typesGenerated";
4+
import { AuthMethods } from "api/typesGenerated";
55
import { useTranslation } from "react-i18next";
6-
import { Maybe } from "../../../components/Conditionals/Maybe";
6+
import { Maybe } from "components/Conditionals/Maybe";
77
import { PasswordSignInForm } from "./PasswordSignInForm";
88
import { OAuthSignInForm } from "./OAuthSignInForm";
99
import { BuiltInAuthFormValues } from "./SignInForm.types";

site/src/pages/LoginPage/SignInForm/SignInForm.stories.tsx

Lines changed: 0 additions & 102 deletions
This file was deleted.

0 commit comments

Comments
 (0)