Skip to content

Commit

Permalink
Forms: Tests: Shouldn't submit if empty (#41504)
Browse files Browse the repository at this point in the history
  • Loading branch information
lezama authored Feb 7, 2025
1 parent b059e75 commit b6327cc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
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

0 comments on commit b6327cc

Please sign in to comment.