-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Create pending sticker when loading the "how to migrate" step #95413
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
100 changes: 100 additions & 0 deletions
100
.../declarative-flow/internals/steps-repository/site-migration-how-to-migrate/test/index.tsx
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,100 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
import { fireEvent } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { useUpdateMigrationStatus } from 'calypso/data/site-migration/use-update-migration-status'; | ||
import { RenderStepOptions, mockStepProps, renderStep } from '../../test/helpers'; | ||
import SiteMigrationHowToMigrate from '../index'; | ||
import type { StepProps } from '../../../types'; | ||
|
||
const siteId = 1; | ||
|
||
jest.mock( 'calypso/data/site-migration/use-update-migration-status', () => ( { | ||
useUpdateMigrationStatus: jest.fn(), | ||
} ) ); | ||
|
||
jest.mock( 'calypso/lib/presales-chat', () => ( { | ||
usePresalesChat: jest.fn(), | ||
} ) ); | ||
|
||
jest.mock( 'calypso/data/site-profiler/use-analyze-url-query', () => ( { | ||
useAnalyzeUrlQuery: () => ( { data: {} } ), | ||
} ) ); | ||
|
||
jest.mock( 'calypso/data/site-profiler/use-hosting-provider-query', () => ( { | ||
useHostingProviderQuery: () => ( { data: {} } ), | ||
} ) ); | ||
|
||
jest.mock( 'calypso/site-profiler/hooks/use-hosting-provider-name', () => jest.fn() ); | ||
|
||
jest.mock( 'calypso/landing/stepper/hooks/use-site', () => ( { | ||
useSite: jest.fn( () => ( { | ||
ID: siteId, | ||
} ) ), | ||
} ) ); | ||
|
||
const render = ( props?: Partial< StepProps >, renderOptions?: RenderStepOptions ) => { | ||
const combinedProps = { ...mockStepProps( props ) }; | ||
return renderStep( <SiteMigrationHowToMigrate { ...combinedProps } />, renderOptions ); | ||
}; | ||
|
||
describe( 'SiteMigrationHowToMigrate', () => { | ||
const mockSubmit = jest.fn(); | ||
let mockUpdateMigrationStatus; | ||
|
||
beforeEach( () => { | ||
mockUpdateMigrationStatus = jest.fn(); | ||
( useUpdateMigrationStatus as jest.Mock ).mockReturnValue( { | ||
updateMigrationStatus: mockUpdateMigrationStatus, | ||
} ); | ||
} ); | ||
|
||
it( 'should register pending migration status when the component is loaded', () => { | ||
render( { navigation: { submit: mockSubmit } } ); | ||
|
||
expect( mockUpdateMigrationStatus ).toHaveBeenCalledWith( siteId, 'migration-pending' ); | ||
} ); | ||
|
||
it( 'should call updateMigrationStatus with correct value for DIFM option', () => { | ||
const { getByText } = render( { navigation: { submit: mockSubmit } } ); | ||
|
||
const optionButton = getByText( 'Do it for me' ); | ||
fireEvent.click( optionButton ); | ||
|
||
// Check the last call value | ||
const lastCallValue = | ||
mockUpdateMigrationStatus.mock.calls[ mockUpdateMigrationStatus.mock.calls.length - 1 ][ 1 ]; | ||
expect( lastCallValue ).toBe( 'migration-pending-difm' ); | ||
} ); | ||
|
||
it( 'should call updateMigrationStatus with correct value for DIY option', () => { | ||
const { getByText } = render( { navigation: { submit: mockSubmit } } ); | ||
|
||
const optionButton = getByText( "I'll do it myself" ); | ||
fireEvent.click( optionButton ); | ||
|
||
// Check the last call value | ||
const lastCallValue = | ||
mockUpdateMigrationStatus.mock.calls[ mockUpdateMigrationStatus.mock.calls.length - 1 ][ 1 ]; | ||
expect( lastCallValue ).toBe( 'migration-pending-diy' ); | ||
} ); | ||
|
||
it( 'should call submit with correct value when DIFM option is clicked', () => { | ||
const { getByText } = render( { navigation: { submit: mockSubmit } } ); | ||
|
||
const optionButton = getByText( 'Do it for me' ); | ||
fireEvent.click( optionButton ); | ||
|
||
expect( mockSubmit ).toHaveBeenCalledWith( { destination: 'upgrade', how: 'difm' } ); | ||
} ); | ||
|
||
it( 'should call submit with correct value for DIY option', () => { | ||
const { getByText } = render( { navigation: { submit: mockSubmit } } ); | ||
|
||
const optionButton = getByText( "I'll do it myself" ); | ||
fireEvent.click( optionButton ); | ||
|
||
expect( mockSubmit ).toHaveBeenCalledWith( { destination: 'upgrade', how: 'myself' } ); | ||
} ); | ||
} ); |
45 changes: 45 additions & 0 deletions
45
.../internals/steps-repository/site-migration-how-to-migrate/use-pending-migration-status.ts
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,45 @@ | ||
import { useEffect } from 'react'; | ||
import { useUpdateMigrationStatus } from 'calypso/data/site-migration/use-update-migration-status'; | ||
import { HOW_TO_MIGRATE_OPTIONS } from 'calypso/landing/stepper/constants'; | ||
import { useSite } from 'calypso/landing/stepper/hooks/use-site'; | ||
import type { NavigationControls } from '../../types'; | ||
|
||
interface PendingMigrationStatusProps { | ||
onSubmit?: Pick< NavigationControls, 'submit' >[ 'submit' ]; | ||
} | ||
|
||
const usePendingMigrationStatus = ( { onSubmit }: PendingMigrationStatusProps ) => { | ||
const site = useSite(); | ||
const siteId = site?.ID; | ||
|
||
const canInstallPlugins = site?.plan?.features?.active.find( | ||
( feature ) => feature === 'install-plugins' | ||
) | ||
? true | ||
: false; | ||
|
||
const { updateMigrationStatus } = useUpdateMigrationStatus(); | ||
|
||
// Register pending migration status when loading the step. | ||
useEffect( () => { | ||
if ( siteId ) { | ||
updateMigrationStatus( siteId, 'migration-pending' ); | ||
} | ||
}, [ siteId, updateMigrationStatus ] ); | ||
|
||
const setPendingMigration = ( how: string ) => { | ||
const destination = canInstallPlugins ? 'migrate' : 'upgrade'; | ||
if ( siteId ) { | ||
const parsedHow = how === HOW_TO_MIGRATE_OPTIONS.DO_IT_MYSELF ? 'diy' : how; | ||
updateMigrationStatus( siteId, `migration-pending-${ parsedHow }` ); | ||
} | ||
|
||
if ( onSubmit ) { | ||
return onSubmit( { how, destination } ); | ||
} | ||
}; | ||
|
||
return { setPendingMigration }; | ||
}; | ||
|
||
export default usePendingMigrationStatus; |
Oops, something went wrong.
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.
Hey folks! I stumbled upon this code today; I got really confused. I don't understand why we'd wrap the existing
useUpdateMigrationStatus
in another hook. Can you enlighten me on the reasoning for this? Even for me, who worked on this, I had difficulties finding where the status was being updated.Especially since the hook is strictly tightened with the How to migrate step, as it won't work anywhere else.
I also disagree that the action on the
How to migrate
step is to "set a migration as pending", and that's the idea that it we give when we do:For me, setting the pending state is a side effect of the submit action and not the other way around. I also feel that for future maintenance, removing the submit logic from the How to migrate step file just makes it harder to understand the code. We added a new layer for the
onSubmit
event, which by itself is already deep enough.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.
I agree that it seems not necessary to have the
submit inside the
usePendingMigrationStatus`Another point is that we are not waiting for the request to be finished before calling the onSubmit.
I am ok with the extraction because it removes business logic from the original component and prevents some components from re-rendering. For example, useEffects inside hooks don't trigger component rendering.
But I am unaware of why we need to use 2 states to set migration as pending.
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.
I think the first one is to flag when the user gets to the How to migrate step so we can redirect them back to that stage. The
migration-pending-{difm/diy}
means that the user has already submitted the intent.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.
In this case, would it trigger the render? I asked this because we are not setting any states that would justify that.
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.
The idea was to extract the portion of logic related to the sticker creation in the how to migrate. Otherwise, it would introduce a big complexity to the
SiteMigrationHowToMigrate
, which already has many other logic too. I thought having it in the same folder as the component would be enough, but maybe we could try to rename the hook to something else to avoid the confusion? I tried to think something to add the HowToMigrate in the name, but my ideas seem too verbose. Something likeusePendingMigrationStatusHandlerForHowToMigrateStep
?Hm! Looking at this again, I totally agree with you. Maybe we could refator it removing the
onSubmit
dependency from that and adding a clickHandler that will call thesetPendingMigration
and thenavigation.submit
?This is already being fixed as part of #95612
Also, notice that any refactor would be nice to do on top of this last PR to avoid conflicts. 👆 @m1r0, I think we could merge that, right?
That's correct!
That's less about it and more about the separation of concerns to simplify the component logic and avoid the components with hundreds of lines complex to maintain! 🤭
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.
Yep, I've merged it. 👍
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.
Also, one important thing when moving the submit to the component, it needs to happen after the sticker is created. 😉
The #95612 does it fixing a race condition issue.