-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from kitcc-org/102-rewrite-login-test
ログインページのテストを書き直した
- Loading branch information
Showing
8 changed files
with
460 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const redirect = vi | ||
.fn() | ||
.mockImplementation((url: string, init?: number | ResponseInit) => { | ||
return null; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,123 @@ | ||
import type * as remixruncloudflare from '@remix-run/cloudflare'; | ||
import { ActionFunctionArgs, LoaderFunctionArgs } from '@remix-run/cloudflare'; | ||
import { createRemixStub } from '@remix-run/testing'; | ||
import { screen } from '@testing-library/react'; | ||
import LoginPage, { action, loader } from '~/routes/auth.login'; | ||
import { customRender } from '../helpers/wrapper'; | ||
import { redirect } from '../mocks/@remix-run/cloudflare'; | ||
|
||
vi.mock('@remix-run/cloudflare', async (importOriginal) => { | ||
const actual = await importOriginal<typeof remixruncloudflare>(); | ||
return { | ||
...actual, | ||
redirect: (url: string, init?: number | ResponseInit) => { | ||
return redirect(url, init); | ||
}, | ||
}; | ||
}); | ||
|
||
const LoginPageStub = createRemixStub([ | ||
{ | ||
path: '/login', | ||
Component: LoginPage, | ||
async loader({ request }) { | ||
return await loader({ request } as LoaderFunctionArgs); | ||
}, | ||
async action({ request }) { | ||
return await action({ request } as ActionFunctionArgs); | ||
}, | ||
}, | ||
]); | ||
|
||
describe('Login page', () => { | ||
it('should login successfully', async () => { | ||
const { user } = customRender( | ||
<LoginPageStub initialEntries={['/login']} />, | ||
); | ||
|
||
// メールアドレスを入力 | ||
const emailForm = await screen.findByLabelText('メールアドレス'); | ||
await user.type(emailForm, 'user@example.com'); | ||
expect(emailForm).toHaveValue('user@example.com'); | ||
|
||
// パスワードを入力 | ||
const passwordForm = await screen.findByLabelText('パスワード'); | ||
await user.type(passwordForm, 'passw0rd'); | ||
expect(passwordForm).toHaveValue('passw0rd'); | ||
|
||
// ログインボタンをクリック | ||
// ログイン成功の通知が表示される | ||
// prettier-ignore | ||
const submitButton = await screen.findByRole('button', { name: 'ログイン' }); | ||
await user.click(submitButton); | ||
|
||
// マイページへリダイレクトされる | ||
// prettier-ignore | ||
expect(redirect).toHaveBeenCalledWith( | ||
'/home/mypage', | ||
{ | ||
headers: { | ||
'Set-Cookie': expect.any(String), | ||
}, | ||
} | ||
); | ||
}); | ||
|
||
it('should fail to pass when email is invalid', async () => { | ||
const { user } = customRender( | ||
<LoginPageStub initialEntries={['/login']} />, | ||
); | ||
|
||
// メールアドレスを入力 | ||
const emailForm = await screen.findByLabelText('メールアドレス'); | ||
await user.type(emailForm, 'user@invalid'); | ||
|
||
// ログインボタンをクリック | ||
// prettier-ignore | ||
const submitButton = await screen.findByRole('button', { name: 'ログイン' }); | ||
await user.click(submitButton); | ||
|
||
// エラーメッセージが表示される | ||
const message = await screen.findByText('有効でないメールアドレスです'); | ||
expect(message).toBeInTheDocument(); | ||
}); | ||
|
||
it('should fail to pass when password length is less than 8', async () => { | ||
const { user } = customRender( | ||
<LoginPageStub initialEntries={['/login']} />, | ||
); | ||
|
||
// パスワードを入力 | ||
const passwordForm = await screen.findByLabelText('パスワード'); | ||
await user.type(passwordForm, 'hoge'); | ||
|
||
// ログインボタンをクリック | ||
// prettier-ignore | ||
const submitButton = await screen.findByRole('button', { name: 'ログイン' }); | ||
await user.click(submitButton); | ||
|
||
// エラーメッセージが表示される | ||
// prettier-ignore | ||
const message = await screen.findByText('パスワードは8文字以上で入力してください'); | ||
expect(message).toBeInTheDocument(); | ||
}); | ||
|
||
it('should fail to pass when password is not alphanumeric', async () => { | ||
const { user } = customRender( | ||
<LoginPageStub initialEntries={['/login']} />, | ||
); | ||
|
||
// パスワードを入力 | ||
const passwordForm = await screen.findByLabelText('パスワード'); | ||
await user.type(passwordForm, 'password'); | ||
|
||
// ログインボタンをクリック | ||
// prettier-ignore | ||
const submitButton = await screen.findByRole('button', { name: 'ログイン' }); | ||
await user.click(submitButton); | ||
|
||
// エラーメッセージが表示される | ||
// prettier-ignore | ||
const message = await screen.findByText('パスワードにはアルファベットと数字を含めてください'); | ||
expect(message).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,41 @@ | ||
/// <reference types="vitest" /> | ||
import { | ||
vitePlugin as remix, | ||
cloudflareDevProxyVitePlugin as remixCloudflareDevProxy, | ||
} from "@remix-run/dev"; | ||
import { defineConfig } from "vite"; | ||
import tsconfigPaths from "vite-tsconfig-paths"; | ||
vitePlugin as remix, | ||
cloudflareDevProxyVitePlugin as remixCloudflareDevProxy, | ||
} from '@remix-run/dev'; | ||
import { defineConfig } from 'vite'; | ||
import tsconfigPaths from 'vite-tsconfig-paths'; | ||
|
||
export default defineConfig({ | ||
plugins: [ | ||
remixCloudflareDevProxy(), | ||
!process.env.VITEST | ||
? remix({ | ||
future: { | ||
v3_fetcherPersist: true, | ||
v3_relativeSplatPath: true, | ||
v3_throwAbortReason: true, | ||
}, | ||
}) | ||
: null, | ||
tsconfigPaths(), | ||
], | ||
server: { | ||
proxy: {}, | ||
https: { | ||
key: "./certs/key.pem", | ||
cert: "./certs/cert.pem", | ||
}, | ||
}, | ||
test: { | ||
alias: { | ||
"~/*": "./app/*", | ||
}, | ||
env: { | ||
NODE_TLS_REJECT_UNAUTHORIZED: "0", | ||
}, | ||
environment: "happy-dom", | ||
globals: true, | ||
setupFiles: ["./test/setup.ts"], | ||
}, | ||
plugins: [ | ||
remixCloudflareDevProxy(), | ||
!process.env.VITEST | ||
? remix({ | ||
future: { | ||
v3_fetcherPersist: true, | ||
v3_relativeSplatPath: true, | ||
v3_throwAbortReason: true, | ||
}, | ||
}) | ||
: null, | ||
tsconfigPaths(), | ||
], | ||
server: { | ||
proxy: {}, | ||
https: { | ||
key: './certs/key.pem', | ||
cert: './certs/cert.pem', | ||
}, | ||
}, | ||
test: { | ||
alias: { | ||
'~/*': './app/*', | ||
}, | ||
env: { | ||
NODE_TLS_REJECT_UNAUTHORIZED: '0', | ||
}, | ||
environment: 'happy-dom', | ||
globals: true, | ||
setupFiles: ['./test/setup.ts'], | ||
}, | ||
}); |