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

fix(metadata-sidebar): handle onCancel action for editing templates #3669

Merged
merged 8 commits into from
Oct 1, 2024

Conversation

JakubKida
Copy link
Contributor

@JakubKida JakubKida commented Sep 19, 2024

Description

  • fix and refactor cancelling logic for MetadataInstanceEditor
  • add new additional props for MetadataInstanceEditor: setIsUnsavedChangesModalOpen and onUnsavedChangesModalCancel for handling the UnsavedChangesModal directly from BUIE
  • fix issues with templates in AddMetadataTemplateDropdown still being disabled after cancelling editing a new template
  • handle adding a new template when another one is still in edit mode with UnsavedChangesModal, add handleCancelUnsavedChanges in MetadataSidebarRedesign ('Cancel' action in UnsavedChangesModal) for checking if there are pending editors to switch to immediately after cancelling the previous one

Additional Notes:

Temporary conversion between MetadataTemplateInstance and MetadataTemplate was added, which will be handled properly in the future PR

@JakubKida JakubKida requested a review from a team as a code owner September 19, 2024 12:46
@CLAassistant
Copy link

CLAassistant commented Sep 19, 2024

CLA assistant check
All committers have signed the CLA.

@JakubKida JakubKida force-pushed the metadata-cancel-editing branch 2 times, most recently from bab51ea to a365a22 Compare September 20, 2024 09:00
wpiesiak
wpiesiak previously approved these changes Sep 20, 2024
Copy link
Contributor

@jankowiakdawid jankowiakdawid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add one more story that shows one can add new template, cancel it, and add it again?
that was not working before

and maybe one that showcases current behavior of canceling editing

Copy link
Contributor

@greg-in-a-box greg-in-a-box left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add in some unit test coverage for your changes? i see the mock props in the defaultProps in one of the tests files but i dont see any tests added with this change to do with the onCancel action

Comment on lines 86 to 91
if (editingTemplate) {
setPendingTemplateToEdit(selectedTemplate as MetadataTemplateInstance);
setIsUnsavedChangesModalOpen(true);
} else {
setSelectedTemplates([...selectedTemplates, selectedTemplate]);
setEditingTemplate(selectedTemplate as MetadataTemplateInstance);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future, as soon as @karolinaru merges #3663 we should use convertTemplateToTemplateInstance here.

jankowiakdawid
jankowiakdawid previously approved these changes Sep 24, 2024
jankowiakdawid
jankowiakdawid previously approved these changes Sep 24, 2024
@JakubKida JakubKida force-pushed the metadata-cancel-editing branch 2 times, most recently from 1573571 to acc6392 Compare September 24, 2024 15:27
const { getByRole } = render(<MetadataInstanceEditor {...props} />);

act(() => {
fireEvent.click(getByRole('button', { name: 'Cancel' }));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fireEvent.click(getByRole('button', { name: 'Cancel' }));
await userEvent.click(getByRole('button', { name: 'Cancel' }));

I dont think we need act if we are using the right combination of functions, if you still get the act error/warning still i wondering its you need to do use findByRole instead of getByRolewhich also might fix that error/warning

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed that to not use it, is act deprecated or unwanted here though ? I thought it's just recommended to change where it's imported from.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you dont need to use act if you use userEvent, should already being doing the act within the function, if start seeing "maybe you need an act" errors, you could probably switch to userEvent in that case.

Comment on lines 62 to 76
// Mock window.matchMedia to simulate media query behavior for tests
// in which UnsavedChangesModal component relies on it.
const mockMatchMedia = () =>
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was just added to master, in the global jest setup file,

Object.defineProperty(window, 'matchMedia', {
rebase and remove this and verify if you still need this

@JakubKida JakubKida force-pushed the metadata-cancel-editing branch 2 times, most recently from 7e5810c to 5cb0050 Compare September 26, 2024 13:53
greg-in-a-box
greg-in-a-box previously approved these changes Sep 26, 2024
jankowiakdawid
jankowiakdawid previously approved these changes Sep 26, 2024
Copy link
Contributor

@JChan106 JChan106 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mergify mergify bot merged commit 6ad083b into box:master Oct 1, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants