feat: model projects as posts with a picked Selected work
Projects were hardcoded h3 elements in a pattern: no URL, no feed, no search, uneditable without touching PHP. They follow the podcast model now, a chrnx_topic term plus posts. Adds a chrnx_project_featured flag with an editor toggle so the stage lists chosen projects first and fills with the most recent. Sticky was already spoken for by the home featured stories, so the choice needed its own signal. journal-stream gains a pinned topic attribute, reusing the archive filter, pagination and empty state it already had. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
14 files changed
+539
-31
No files matched your search
@@ -0,0 +1,102 @@
|
||||
const modulePath = '../../assets/js/editor-project.js';
|
||||
|
||||
function flatten( node ) {
|
||||
if ( ! node || typeof node !== 'object' ) {
|
||||
return [];
|
||||
}
|
||||
return [ node ].concat( ( node.children || [] ).flatMap( flatten ) );
|
||||
}
|
||||
|
||||
function createWp( topicSlug = 'projects', meta = { unrelated: 'keep' } ) {
|
||||
const editPost = jest.fn();
|
||||
const registerPlugin = jest.fn();
|
||||
const state = { meta, topic: [ 12 ] };
|
||||
const wp = {
|
||||
components: { ToggleControl: 'ToggleControl' },
|
||||
i18n: { __: jest.fn( ( text ) => text ) },
|
||||
data: {
|
||||
select: ( store ) => {
|
||||
if ( store === 'core/editor' ) {
|
||||
return {
|
||||
getCurrentPostType: () => 'post',
|
||||
getEditedPostAttribute: ( key ) => state[ key ],
|
||||
};
|
||||
}
|
||||
return {
|
||||
getEntityRecord: () => ( topicSlug ? { id: 12, slug: topicSlug } : undefined ),
|
||||
};
|
||||
},
|
||||
dispatch: () => ( { editPost } ),
|
||||
useDispatch: ( store ) => wp.data.dispatch( store ),
|
||||
useSelect: ( mapper ) => mapper( wp.data.select ),
|
||||
},
|
||||
editPost: { PluginDocumentSettingPanel: 'PluginDocumentSettingPanel' },
|
||||
element: {
|
||||
createElement: ( type, props, ...children ) => ( { type, props: props || {}, children } ),
|
||||
},
|
||||
plugins: { registerPlugin },
|
||||
};
|
||||
return { wp, editPost, registerPlugin };
|
||||
}
|
||||
|
||||
function renderPanel( context ) {
|
||||
const { initialize } = require( modulePath );
|
||||
expect( initialize( context.wp ) ).toBe( true );
|
||||
const [ , plugin ] = context.registerPlugin.mock.calls[ 0 ];
|
||||
return plugin.render();
|
||||
}
|
||||
|
||||
describe( 'project editor panel', () => {
|
||||
beforeEach( () => {
|
||||
jest.resetModules();
|
||||
} );
|
||||
|
||||
test( 'refuses to initialize without the required editor globals', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
expect( initialize( undefined ) ).toBe( false );
|
||||
expect( initialize( {} ) ).toBe( false );
|
||||
} );
|
||||
|
||||
test( 'renders the Selected work toggle only for the projects topic', () => {
|
||||
const context = createWp( 'projects' );
|
||||
const toggles = flatten( renderPanel( context ) ).filter(
|
||||
( node ) => node.type === 'ToggleControl',
|
||||
);
|
||||
expect( toggles ).toHaveLength( 1 );
|
||||
expect( toggles[ 0 ].props.checked ).toBe( false );
|
||||
} );
|
||||
|
||||
test.each( [ 'podcasts', 'code', null ] )( 'renders nothing for the %s topic', ( slug ) => {
|
||||
expect( renderPanel( createWp( slug ) ) ).toBeNull();
|
||||
} );
|
||||
|
||||
test( 'reflects an existing flag and preserves unrelated meta when toggling', () => {
|
||||
const context = createWp( 'projects', { unrelated: 'keep', chrnx_project_featured: true } );
|
||||
const toggle = flatten( renderPanel( context ) ).find(
|
||||
( node ) => node.type === 'ToggleControl',
|
||||
);
|
||||
expect( toggle.props.checked ).toBe( true );
|
||||
|
||||
toggle.props.onChange( false );
|
||||
expect( context.editPost ).toHaveBeenCalledWith( {
|
||||
meta: { unrelated: 'keep', chrnx_project_featured: false },
|
||||
} );
|
||||
} );
|
||||
|
||||
test( 'coerces the toggle value to a boolean', () => {
|
||||
const context = createWp( 'projects' );
|
||||
const toggle = flatten( renderPanel( context ) ).find(
|
||||
( node ) => node.type === 'ToggleControl',
|
||||
);
|
||||
|
||||
toggle.props.onChange( 'yes' );
|
||||
expect( context.editPost ).toHaveBeenCalledWith( {
|
||||
meta: { unrelated: 'keep', chrnx_project_featured: true },
|
||||
} );
|
||||
} );
|
||||
|
||||
test( 'updateMeta ignores a missing dispatcher', () => {
|
||||
const { updateMeta } = require( modulePath );
|
||||
expect( () => updateMeta( undefined, {}, 'chrnx_project_featured', true ) ).not.toThrow();
|
||||
} );
|
||||
} );
|
||||
@@ -45,7 +45,7 @@ class EditorAssetsTest extends WP_UnitTestCase {
|
||||
* Clear only the CHRNX editor script state.
|
||||
*/
|
||||
private function reset_editor_scripts() {
|
||||
foreach ( array( 'chrnx-editor-taxonomies', 'chrnx-editor-podcast' ) as $handle ) {
|
||||
foreach ( array( 'chrnx-editor-taxonomies', 'chrnx-editor-podcast', 'chrnx-editor-project', 'chrnx-editor-track' ) as $handle ) {
|
||||
wp_dequeue_script( $handle );
|
||||
wp_deregister_script( $handle );
|
||||
}
|
||||
@@ -83,13 +83,14 @@ class EditorAssetsTest extends WP_UnitTestCase {
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertSame( array( 'chrnx-editor-taxonomies', 'chrnx-editor-podcast' ), $handles );
|
||||
$this->assertSame( array( 'chrnx-editor-taxonomies', 'chrnx-editor-podcast', 'chrnx-editor-project', 'chrnx-editor-track' ), $handles );
|
||||
$expected_dependencies = array( 'wp-components', 'wp-core-data', 'wp-data', 'wp-edit-post', 'wp-element', 'wp-i18n', 'wp-plugins' );
|
||||
|
||||
foreach (
|
||||
array(
|
||||
'chrnx-editor-taxonomies' => 'assets/js/editor-taxonomies.js',
|
||||
'chrnx-editor-podcast' => 'assets/js/editor-podcast.js',
|
||||
'chrnx-editor-project' => 'assets/js/editor-project.js',
|
||||
) as $handle => $asset
|
||||
) {
|
||||
$this->assertArrayHasKey( $handle, $scripts->registered );
|
||||
@@ -122,6 +123,8 @@ class EditorAssetsTest extends WP_UnitTestCase {
|
||||
do_action( 'enqueue_block_editor_assets' );
|
||||
$this->assertFalse( wp_script_is( 'chrnx-editor-taxonomies', 'enqueued' ) );
|
||||
$this->assertFalse( wp_script_is( 'chrnx-editor-podcast', 'enqueued' ) );
|
||||
$this->assertFalse( wp_script_is( 'chrnx-editor-project', 'enqueued' ) );
|
||||
$this->assertFalse( wp_script_is( 'chrnx-editor-track', 'enqueued' ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ class TaxonomiesTest extends WP_UnitTestCase {
|
||||
'terms' => array(
|
||||
'research' => 'Research',
|
||||
'code' => 'Code',
|
||||
'projects' => 'Projects',
|
||||
'literature' => 'Literature',
|
||||
'music' => 'Music',
|
||||
'podcasts' => 'Podcasts',
|
||||
|
||||
Reference in new issue
Block a user