Fakultas Ilmu Komputer UI

Commit 3b531226 authored by Lia Yuliana's avatar Lia Yuliana
Browse files

[CHORES] improve test for Register

parent 03a5a8ad
Pipeline #72611 passed with stages
in 11 minutes and 3 seconds
......@@ -8,9 +8,15 @@ import BASE_URL from '../../api/config'
import userEvent from '@testing-library/user-event'
const server = setupServer(
rest.post('http://localhost:8000/api/v1/auth/register/', (req, res, ctx) => {
return res(ctx.status(400), ctx.json({email: 'Email exists'}))
}),
rest.post(
`${BASE_URL.API_BASE_URL}/api/v1/auth/register/`,
(req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({message: 'Akun berhasil terdaftar'}),
)
},
),
)
beforeAll(() => server.listen())
......@@ -270,29 +276,12 @@ test('data is successfully registered and phone number start with 08', async ()
testRender(<Register />, {store})
const emailElement = screen.getByLabelText(/Email/i)
fireEvent.change(emailElement, {
target: {value: 'sigendud@email.com'},
})
const noHandphoneElement = screen.getByLabelText(/No Handphone Aktif/i)
fireEvent.change(noHandphoneElement, {
target: {value: '08123456789'},
})
const passwordElement = screen.getByLabelText('Password*')
fireEvent.change(passwordElement, {
target: {value: 'sebuahPassYangValid22'},
})
const konfirmasiPasswordElement = screen.getByLabelText(
'Konfirmasi Password*',
)
fireEvent.change(konfirmasiPasswordElement, {
target: {value: 'sebuahPassYangValid22'},
})
userEvent.type(screen.getByLabelText(/Email/i), 'sigendud@email.com')
userEvent.type(screen.getByLabelText(/No Handphone Aktif/i), '08123456789')
userEvent.type(screen.getByLabelText('Password*'), 'sebuahPassYangValid22')
userEvent.type(screen.getByLabelText('Konfirmasi Password*'), 'sebuahPassYangValid22')
const daftarButton = screen.getByTestId('daftar')
const daftarButton = screen.getByRole('button')
fireEvent.click(daftarButton)
})
......@@ -313,27 +302,10 @@ test('data is successfully registered and phone number start with 8', async () =
testRender(<Register />, {store})
const emailElement = screen.getByLabelText(/Email/i)
fireEvent.change(emailElement, {
target: {value: 'siunyil@email.com'},
})
const noHandphoneElement = screen.getByLabelText(/No Handphone Aktif/i)
fireEvent.change(noHandphoneElement, {
target: {value: '8123456789'},
})
const passwordElement = screen.getByLabelText('Password*')
fireEvent.change(passwordElement, {
target: {value: 'sebuahPassValid22'},
})
const konfirmasiPasswordElement = screen.getByLabelText(
'Konfirmasi Password*',
)
fireEvent.change(konfirmasiPasswordElement, {
target: {value: 'sebuahPassValid22'},
})
userEvent.type(screen.getByLabelText(/Email/i), 'sigendud@email.com')
userEvent.type(screen.getByLabelText(/No Handphone Aktif/i), '8123456789')
userEvent.type(screen.getByLabelText('Password*'), 'sebuahPassYangValid22')
userEvent.type(screen.getByLabelText('Konfirmasi Password*'), 'sebuahPassYangValid22')
const daftarButton = screen.getByRole('button')
fireEvent.click(daftarButton)
......@@ -347,7 +319,7 @@ test('data is unsuccessfully registered because account already exists', async (
return res(
ctx.status(400),
ctx.json({
non_field_errors: 'account with this email already exists.',
email: 'account with this email already exists.',
}),
)
},
......@@ -362,8 +334,8 @@ test('data is unsuccessfully registered because account already exists', async (
userEvent.type(screen.getByLabelText(/Email/i), 'sigendud@email.com')
userEvent.type(screen.getByLabelText(/No Handphone Aktif/i), '8123456789')
userEvent.type(screen.getByLabelText('Password*'), 'password01')
userEvent.type(screen.getByLabelText('Konfirmasi Password*'), 'password01')
userEvent.type(screen.getByLabelText('Password*'), 'sebuahPassYangValid22')
userEvent.type(screen.getByLabelText('Konfirmasi Password*'), 'sebuahPassYangValid22')
const daftarButton = screen.getByRole('button')
fireEvent.click(daftarButton)
......@@ -372,7 +344,7 @@ test('data is unsuccessfully registered because account already exists', async (
expect(toastError).toBeInTheDocument()
})
test('data is unsuccessfully registered due to phone number count error', async () => {
test('data is unsuccessfully registered due to phone number char less than 9', async () => {
server.use(
rest.post(
`${BASE_URL.API_BASE_URL}/api/v1/auth/register/`,
......@@ -392,40 +364,29 @@ test('data is unsuccessfully registered due to phone number count error', async
testRender(<Register />, {store})
const emailElement = screen.getByLabelText(/Email/i)
fireEvent.change(emailElement, {
target: {value: 'sibolang@email.com'},
})
const noHandphoneElement = screen.getByLabelText(/No Handphone Aktif/i)
fireEvent.change(noHandphoneElement, {
target: {value: '8123'},
})
const passwordElement = screen.getByLabelText('Password*')
fireEvent.change(passwordElement, {
target: {value: 'sebuahPass22'},
})
userEvent.type(screen.getByLabelText(/Email/i), 'sibolang@email.com')
userEvent.type(screen.getByLabelText(/No Handphone Aktif/i), '8123')
userEvent.type(screen.getByLabelText('Password*'), 'sebuahPass22')
userEvent.type(screen.getByLabelText('Konfirmasi Password*'), 'sebuahPass22')
const konfirmasiPasswordElement = screen.getByLabelText(
'Konfirmasi Password*',
)
fireEvent.change(konfirmasiPasswordElement, {
target: {value: 'sebuahPass22'},
})
const daftarButton = screen.getByTestId('daftar')
const daftarButton = screen.getByRole('button')
fireEvent.click(daftarButton)
const toastError = await screen.findByTestId('toast-error')
expect(toastError).toBeInTheDocument()
})
test('data is unsuccessfully registered because password is too common', async () => {
test('data is unsuccessfully registered due to phone number char more than 12', async () => {
server.use(
rest.post(
`${BASE_URL.API_BASE_URL}/api/v1/auth/register/`,
(req, res, ctx) => {
return res(
ctx.status(400),
ctx.json({non_field_errors: 'This password is too common.'}),
ctx.json({
phone_number:
'Ensure this field has no more than 12 characters.',
}),
)
},
),
......@@ -435,50 +396,37 @@ test('data is unsuccessfully registered because password is too common', async (
testRender(<Register />, {store})
const emailElement = screen.getByLabelText(/Email/i)
fireEvent.change(emailElement, {
target: {value: 'sibolang@email.com'},
})
const noHandphoneElement = screen.getByLabelText(/No Handphone Aktif/i)
fireEvent.change(noHandphoneElement, {
target: {value: '8123000000'},
})
const passwordElement = screen.getByLabelText('Password*')
fireEvent.change(passwordElement, {
target: {value: 'abcde123'},
})
userEvent.type(screen.getByLabelText(/Email/i), 'sibolang@email.com')
userEvent.type(screen.getByLabelText(/No Handphone Aktif/i), '8123999999999')
userEvent.type(screen.getByLabelText('Password*'), 'sebuahPass22')
userEvent.type(screen.getByLabelText('Konfirmasi Password*'), 'sebuahPass22')
const konfirmasiPasswordElement = screen.getByLabelText(
'Konfirmasi Password*',
)
fireEvent.change(konfirmasiPasswordElement, {
target: {value: 'abcde123'},
})
const daftarButton = screen.getByTestId('daftar')
const daftarButton = screen.getByRole('button')
fireEvent.click(daftarButton)
})
test('show error toast if there is email error from backend', async () => {
test('data is unsuccessfully registered because password is too common', async () => {
server.use(
rest.post(
`${BASE_URL.API_BASE_URL}/api/v1/auth/register/`,
(req, res, ctx) => {
return res(
ctx.status(400),
ctx.json({non_field_errors: 'This password is too common.'}),
)
},
),
)
const store = makeTestStore()
testRender(<Register />, {
store,
})
testRender(<Register />, {store})
userEvent.type(screen.getByLabelText(/Email/i), 'sigendud@email.com')
userEvent.type(screen.getByLabelText(/No Handphone Aktif/i), '8123456789')
userEvent.type(screen.getByLabelText('Password*'), 'sebuahPassYangValid22')
userEvent.type(
screen.getByLabelText('Konfirmasi Password*'),
'sebuahPassYangValid22',
)
userEvent.type(screen.getByLabelText(/Email/i), 'sibolang@email.com')
userEvent.type(screen.getByLabelText(/No Handphone Aktif/i), '81239999999')
userEvent.type(screen.getByLabelText('Password*'), 'abcde123')
userEvent.type(screen.getByLabelText('Konfirmasi Password*'), 'abcde123')
const daftarButton = screen.getByRole('button')
fireEvent.click(daftarButton)
const toastError = await screen.findByTestId('toast-error')
expect(toastError).toBeInTheDocument()
})
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment