-
Notifications
You must be signed in to change notification settings - Fork 894
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
Add default to onDiscard in UnsavedChangesModal #21756
Merged
leonidasmi
merged 1 commit into
trunk
from
21728-new-general-page-ftc-tab-gives-react-warning
Oct 31, 2024
Merged
Changes from all commits
Commits
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
77 changes: 66 additions & 11 deletions
77
packages/js/tests/shared-admin/components/unsaved-changes-modal.test.js
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,36 +1,91 @@ | ||
import { render, screen } from "../../test-utils"; | ||
import { beforeEach, describe, expect, it, jest } from "@jest/globals"; | ||
import { UnsavedChangesModal } from "../../../src/shared-admin/components"; | ||
import { fireEvent, render, screen } from "../../test-utils"; | ||
|
||
describe( "UnsavedChangesModal", () => { | ||
const onClose = jest.fn(); | ||
const onDiscard = jest.fn(); | ||
|
||
beforeEach( () => { | ||
jest.clearAllMocks(); | ||
render( <UnsavedChangesModal | ||
isOpen={ true } | ||
onClose={ jest.fn() } | ||
onClose={ onClose } | ||
onDiscard={ onDiscard } | ||
title="Unsaved changes" | ||
description="There are unsaved changes in one or more steps of the first-time configuration. Leaving means that those changes will be lost. Are you sure you want to leave this page?" | ||
onDiscard={ jest.fn() } | ||
dismissLabel="No, continue editing" | ||
discardLabel="Yes, leave page" | ||
/> ); | ||
} ); | ||
|
||
it( "should have dismiss and leave page buttons", () => { | ||
const leaveButton = screen.queryByText( "Yes, leave page" ); | ||
expect( leaveButton ).toBeInTheDocument(); | ||
it( "should have a role of dialog", () => { | ||
expect( screen.queryByRole( "dialog" ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
expect( screen.queryByText( "Close" ) ).toBeInTheDocument(); | ||
it.each( [ | ||
[ "dismiss", "No, continue editing" ], | ||
[ "discard", "Yes, leave page" ], | ||
] )( "should have the %s button: %s", ( _, text ) => { | ||
const button = screen.queryByText( text ); | ||
expect( button ).toBeInTheDocument(); | ||
expect( button.tagName ).toBe( "BUTTON" ); | ||
expect( button ).toHaveAttribute( "type", "button" ); | ||
} ); | ||
|
||
const dismissButton = screen.queryByText( "No, continue editing" ); | ||
expect( dismissButton ).toBeInTheDocument(); | ||
it( "should have the close button", () => { | ||
const screenReaderText = screen.queryByText( "Close" ); | ||
expect( screenReaderText ).toBeInTheDocument(); | ||
expect( screenReaderText.parentElement ).toHaveAttribute( "type", "button" ); | ||
expect( screenReaderText.parentElement.tagName ).toBe( "BUTTON" ); | ||
} ); | ||
|
||
it( "should have a title", () => { | ||
const title = screen.queryByText( "Unsaved changes" ); | ||
expect( title ).toBeInTheDocument(); | ||
expect( title.tagName ).toBe( "H1" ); | ||
} ); | ||
|
||
it( "should have a description", () => { | ||
const title = screen.queryByText( "There are unsaved changes in one or more steps of the first-time configuration. Leaving means that those changes will be lost. Are you sure you want to leave this page?" ); | ||
expect( title ).toBeInTheDocument(); | ||
const description = screen.queryByText( "There are unsaved changes in one or more steps of the first-time configuration. Leaving means that those changes will be lost. Are you sure you want to leave this page?" ); | ||
expect( description ).toBeInTheDocument(); | ||
expect( description.tagName ).toBe( "P" ); | ||
} ); | ||
|
||
it( "should call onClose when clicking the close button", () => { | ||
fireEvent.click( screen.queryByText( "Close" ) ); | ||
expect( onClose ).toHaveBeenCalled(); | ||
} ); | ||
|
||
it( "should call onClose when clicking the continue editing button", () => { | ||
fireEvent.click( screen.queryByText( "No, continue editing" ) ); | ||
expect( onClose ).toHaveBeenCalled(); | ||
} ); | ||
|
||
it( "should call onDiscard when clicking the leave page button", () => { | ||
fireEvent.click( screen.queryByText( "Yes, leave page" ) ); | ||
expect( onDiscard ).toHaveBeenCalled(); | ||
} ); | ||
|
||
describe( "fallback props", () => { | ||
it( "should not throw an error when the onClose and onDiscard props are not passed", () => { | ||
const currentImplementation = console.error; | ||
console.error = jest.fn(); | ||
|
||
render( <UnsavedChangesModal | ||
isOpen={ true } | ||
title="Unsaved changes" | ||
description="There are unsaved changes in one or more steps of the first-time configuration. Leaving means that those changes will be lost. Are you sure you want to leave this page?" | ||
dismissLabel="No, continue editing" | ||
discardLabel="Yes, leave page" | ||
/> ); | ||
|
||
try { | ||
expect( console.error ).not.toHaveBeenCalled(); | ||
} finally { | ||
// Cleanup. | ||
console.error = currentImplementation; | ||
} | ||
} ); | ||
} ); | ||
} ); |
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.
A bit weird because it does not throw, it logs.
This tests the original issue. That the React warning is gone when not passing
onClose
oronDiscard
.Try to put the
isRequired
back on them to see if it then fails.