-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test for userform component #49
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
12acb11
test for userform component
milicarabelos 29193ca
el cambio de user event que pide el tomi
milicarabelos 4f13cf2
fix add describe envolving test
milicarabelos 639507a
merge style with test
milicarabelos 73eae51
feat create game form test + comment console.log
milicarabelos ff4a7b8
fix export of the createGame request
milicarabelos d9497f5
fix userForm test luego del styling
milicarabelos bbfcab0
fix config for render of test adding routes context
milicarabelos deb3a33
fix linter
milicarabelos 1a3ee2f
fix de test userform the back request should be on a try catch block
milicarabelos 31a3f46
style errasing consoles.log and adding status to mock to be more real…
milicarabelos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. En los mocks si el ok es true el status deberia ser 200 |
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,132 @@ | ||
// eslint-disable-next-line no-unused-vars | ||
import React from 'react'; | ||
import {screen, waitFor} from '@testing-library/react'; | ||
// We're using our own custom render function and not RTL's render. | ||
import {renderWithProviders} from '../../services/providerForTest/utils-for-tests'; | ||
import UserForm from './UserForm'; | ||
// eslint-disable-next-line no-unused-vars | ||
import {getByLabelText} from '@testing-library/dom'; | ||
|
||
import userEvent from '@testing-library/user-event'; | ||
|
||
test('correct renderig of User form', () => { | ||
renderWithProviders(<UserForm />); | ||
const submitText = screen.getByText('Submit'); | ||
expect(submitText).toBeInTheDocument(); | ||
|
||
const userNameText = screen.getByText('User Name'); | ||
expect(userNameText).toBeInTheDocument(); | ||
}); | ||
|
||
jest.mock('../request/sendPlayerName', () => { | ||
return { | ||
__esModule: true, | ||
default: async ({player}) => { | ||
player.id = 21; | ||
|
||
// console.log('estoy entrando en el mock'); | ||
// console.log(player); | ||
if (player.name === 'username') { | ||
// console.log('estoy entrando por succes en el mock'); | ||
return { | ||
status: undefined, | ||
ok: true, | ||
id: 21, | ||
name: 'username', | ||
detail: 'User username registered successfully', | ||
}; | ||
} else if (player.name === 'username1') { | ||
// Simulate an error by rejecting the promise | ||
// console.log('estoy entrando por reject en el mock'); | ||
return { | ||
status: undefined, | ||
ok: false, | ||
detail: | ||
'Object Player cannot be stored in the database. IntegrityError: 1062 Duplicate entry "username1" for key "player.name"', | ||
}; | ||
} else { | ||
// Handle other cases as needed | ||
// console.log('estoy entrando por oytros en el mock'); | ||
return { | ||
status: undefined, | ||
ok: false, | ||
detail: 'Some default error message', | ||
}; | ||
} | ||
}, | ||
}; | ||
}); | ||
|
||
test('should register', async () => { | ||
const user = userEvent.setup(); | ||
const screen = renderWithProviders(<UserForm />); | ||
const usernameInput = screen.getByLabelText('User Name'); | ||
|
||
expect(usernameInput).toBeInTheDocument(); | ||
// Focus on the input field and type the username | ||
|
||
user.click(usernameInput); | ||
user.type(usernameInput, 'username'); | ||
|
||
// Use await waitFor to handle asynchronous updates | ||
await waitFor(() => { | ||
expect(usernameInput).toHaveValue('username'); | ||
}); | ||
// Click the submit button | ||
const submitButton = screen.getByRole('button', {name: /submit/i}); | ||
expect(submitButton).toBeInTheDocument(); | ||
|
||
user.click(submitButton); | ||
// Wait for the success message | ||
|
||
await waitFor(() => | ||
screen.getByText(/ User username registered successfully/i), | ||
); | ||
|
||
// Assert that the success message is present | ||
expect( | ||
screen.getByText(/ User username registered successfully/i), | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
test('shouldnt register', async () => { | ||
const user = userEvent.setup(); | ||
|
||
const screen = renderWithProviders(<UserForm />); | ||
const usernameInput = screen.getByLabelText('User Name'); | ||
|
||
expect(usernameInput).toBeInTheDocument(); | ||
// Focus on the input field and type the username | ||
|
||
user.click(usernameInput); | ||
user.type(usernameInput, 'username1'); | ||
|
||
// Use await waitFor to handle asynchronous updates | ||
await waitFor(() => { | ||
expect(usernameInput).toHaveValue('username1'); | ||
}); | ||
// Click the submit button | ||
const submitButton = screen.getByRole('button', {name: /submit/i}); | ||
expect(submitButton).toBeInTheDocument(); | ||
|
||
try { | ||
user.click(submitButton); | ||
} catch (error) { | ||
// console.log('estoy entrando en el catch'); | ||
/// /console.log(screen.debug()); | ||
// console.log(error); | ||
} | ||
|
||
await waitFor(() => | ||
screen.getByText( | ||
/Object Player cannot be stored in the database. IntegrityError: 1062 Duplicate entry "username1" for key "player.name"/i, | ||
), | ||
); | ||
|
||
// Assert that the success message is present | ||
expect( | ||
screen.getByText( | ||
/Object Player cannot be stored in the database. IntegrityError: 1062 Duplicate entry "username1" for key "player.name"/i, | ||
), | ||
).toBeInTheDocument(); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eliminar console.log
El sendPlayerName puede fallar, deberia estar dentro de el try