Skip to content
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

Forms: Tests: Shouldn't submit if empty #41504

Merged
merged 3 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions projects/packages/forms/changelog/add-forms-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Forms: Tests: An empty form shouldn't submit test
21 changes: 19 additions & 2 deletions projects/packages/forms/tests/js/contact-form/contact-form.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,15 @@ describe( 'Contact Form', () => {
beforeEach( () => {
setFormContent( `
<label for="name">Name</label>
<input id="name" name="name" required />
<input id="name" name="name">
<button type="submit">Submit</button>
` );
// Mock offsetParent for all elements
Object.defineProperty( HTMLElement.prototype, 'offsetParent', {
get() {
return {};
}, // Return a truthy value
} );
fireDomReadyEvent();
} );

Expand All @@ -100,7 +106,18 @@ describe( 'Contact Form', () => {
expect( spy ).toHaveBeenCalledTimes( 1 );
} );

it( "shouldn't submit an invalid form", () => {
it( "shouldn't submit form with missing required fields", () => {
const form = screen.getByRole( 'form' );
const input = screen.getByLabelText( 'Name' );
input.setAttribute( 'required', '' );
const spy = jest.spyOn( form, 'submit' ).mockImplementation( () => {} );

fireEvent.submit( form );

expect( spy ).not.toHaveBeenCalled();
} );

it( "shouldn't submit when all fields are empty", () => {
const form = screen.getByRole( 'form' );
const spy = jest.spyOn( form, 'submit' ).mockImplementation( () => {} );

Expand Down
Loading