-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
usePostPrePublishValue
hook (#39119)
* Create usePostPrePublishValue hook * Extract re-usable test logic to common utilities * Add tests for usePostPrePublishValue * Add changelog * Make CI happy
- Loading branch information
1 parent
649eae9
commit fbfd26c
Showing
5 changed files
with
175 additions
and
35 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
projects/js-packages/publicize-components/changelog/add-use-post-pre-publish-value-hook
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,4 @@ | ||
Significance: patch | ||
Type: added | ||
|
||
Added usePostPrePublishValue hook |
39 changes: 39 additions & 0 deletions
39
projects/js-packages/publicize-components/src/hooks/use-post-pre-publish-value/index.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,39 @@ | ||
import { useSelect } from '@wordpress/data'; | ||
import { store as editorStore } from '@wordpress/editor'; | ||
import { useEffect, useRef, useState } from '@wordpress/element'; | ||
|
||
/** | ||
* Preserve a value from the post pre-publish state. | ||
* | ||
* The value will stop updating after the post is published. | ||
* | ||
* @template V | ||
* @param {V} value - The value to preserve. | ||
* | ||
* @return {V} The preserved value. | ||
*/ | ||
export function usePostPrePublishValue< V >( value: V ) { | ||
const isPublishing = useSelect( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- `@wordpress/editor` is a nightmare to work with TypeScript | ||
select => ( select( editorStore ) as any ).isPublishingPost(), | ||
[] | ||
); | ||
|
||
const [ currentValue, setCurrentValue ] = useState( value ); | ||
|
||
const valueFrozen = useRef( false ); | ||
|
||
useEffect( () => { | ||
// Freeze the value after publishing starts. | ||
if ( isPublishing ) { | ||
valueFrozen.current = true; | ||
} | ||
|
||
// Since the value is not frozen yet, update the current value. | ||
if ( ! valueFrozen.current ) { | ||
setCurrentValue( value ); | ||
} | ||
}, [ isPublishing, value ] ); | ||
|
||
return currentValue; | ||
} |
79 changes: 79 additions & 0 deletions
79
.../js-packages/publicize-components/src/hooks/use-post-pre-publish-value/test/index.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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { act, renderHook } from '@testing-library/react'; | ||
import { RegistryProvider } from '@wordpress/data'; | ||
import { store as editorStore } from '@wordpress/editor'; | ||
import { usePostPrePublishValue } from '../'; | ||
import { | ||
connections as connectionsList, | ||
createRegistryWithStores, | ||
} from '../../../utils/test-utils'; | ||
|
||
const connections = connectionsList.map( connection => ( { ...connection, enabled: true } ) ); | ||
|
||
const post = { | ||
jetpack_publicize_connections: [ connections[ 0 ] ], | ||
}; | ||
|
||
describe( 'usePostPrePublishValue', () => { | ||
it( 'should return the value by default', async () => { | ||
const registry = createRegistryWithStores( post ); | ||
|
||
const { result } = renderHook( () => usePostPrePublishValue( 'test-value' ), { | ||
wrapper: ( { children } ) => ( | ||
<RegistryProvider value={ registry }>{ children }</RegistryProvider> | ||
), | ||
} ); | ||
|
||
expect( result.current ).toBe( 'test-value' ); | ||
} ); | ||
|
||
it( 'should return the updated value when the post is not being published', async () => { | ||
const registry = createRegistryWithStores( post ); | ||
|
||
const { rerender, result } = renderHook( | ||
( initialValue = 'first-value' ) => usePostPrePublishValue( initialValue ), | ||
{ | ||
wrapper: ( { children } ) => ( | ||
<RegistryProvider value={ registry }>{ children }</RegistryProvider> | ||
), | ||
} | ||
); | ||
|
||
rerender( 'second-value' ); | ||
|
||
await act( async () => { | ||
await registry.dispatch( editorStore ).editPost( { | ||
status: 'draft', | ||
content: 'Some test content', | ||
} ); | ||
} ); | ||
|
||
expect( result.current ).toBe( 'second-value' ); | ||
} ); | ||
|
||
it( 'should preserve the pre-publish value', async () => { | ||
const registry = createRegistryWithStores( post ); | ||
|
||
const { rerender, result } = renderHook( | ||
( initialValue = 'first-value' ) => usePostPrePublishValue( initialValue ), | ||
{ | ||
wrapper: ( { children } ) => ( | ||
<RegistryProvider value={ registry }>{ children }</RegistryProvider> | ||
), | ||
} | ||
); | ||
|
||
rerender( 'second-value' ); | ||
|
||
await act( async () => { | ||
registry.dispatch( editorStore ).editPost( { | ||
status: 'publish', | ||
content: 'Some test content', | ||
} ); | ||
registry.dispatch( editorStore ).savePost(); | ||
} ); | ||
|
||
rerender( 'third-value' ); | ||
|
||
expect( result.current ).toBe( 'second-value' ); | ||
} ); | ||
} ); |
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
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