Skip to content

Commit

Permalink
Set the endpoint for the block by using the self link property
Browse files Browse the repository at this point in the history
  • Loading branch information
ryelle committed Jun 17, 2020
1 parent c66486a commit 6c9ac20
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
8 changes: 4 additions & 4 deletions packages/block-directory/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export function* installBlockType( block ) {
},
method: 'POST',
} );
yield addInstalledBlockType( { ...block, plugin: response.plugin } );
const endpoint = response?._links?.self[ 0 ]?.href;
yield addInstalledBlockType( { ...block, endpoint } );

yield loadAssets( assets );
const registeredBlocks = yield select( 'core/blocks', 'getBlockTypes' );
Expand All @@ -95,17 +96,16 @@ export function* installBlockType( block ) {
* @param {Object} block The blockType object.
*/
export function* uninstallBlockType( block ) {
const plugin = block.plugin.replace( '.php', '' );
try {
yield apiFetch( {
path: `__experimental/plugins/${ plugin }`,
url: block.endpoint,
data: {
status: 'inactive',
},
method: 'PUT',
} );
yield apiFetch( {
path: `__experimental/plugins/${ plugin }`,
url: block.endpoint,
method: 'DELETE',
} );
yield removeInstalledBlockType( block );
Expand Down
29 changes: 18 additions & 11 deletions packages/block-directory/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@
import { installBlockType, uninstallBlockType } from '../actions';

describe( 'actions', () => {
const endpoint = '/wp-json/__experimental/plugins/block/block';
const item = {
id: 'block/block',
name: 'Test Block',
assets: [ 'script.js' ],
};

const plugin = {
plugin: 'block/block.php',
status: 'active',
name: 'Test Block',
version: '1.0.0',
_links: {
self: [
{
href: endpoint,
},
],
},
};

describe( 'installBlockType', () => {
Expand All @@ -41,10 +48,10 @@ describe( 'actions', () => {
},
} );

const itemWithPlugin = { ...item, plugin: plugin.plugin };
const itemWithEndpoint = { ...item, endpoint };
expect( generator.next( plugin ).value ).toEqual( {
type: 'ADD_INSTALLED_BLOCK_TYPE',
item: itemWithPlugin,
item: itemWithEndpoint,
} );

expect( generator.next().value ).toEqual( {
Expand Down Expand Up @@ -144,16 +151,16 @@ describe( 'actions', () => {
} );

describe( 'uninstallBlockType', () => {
const itemWithPlugin = { ...item, plugin: plugin.plugin };
const itemWithEndpoint = { ...item, endpoint };

it( 'should uninstall a block successfully', () => {
const generator = uninstallBlockType( itemWithPlugin );
const generator = uninstallBlockType( itemWithEndpoint );

// First the deactivation step
expect( generator.next().value ).toMatchObject( {
type: 'API_FETCH',
request: {
path: '__experimental/plugins/block/block',
url: endpoint,
method: 'PUT',
},
} );
Expand All @@ -162,14 +169,14 @@ describe( 'actions', () => {
expect( generator.next().value ).toMatchObject( {
type: 'API_FETCH',
request: {
path: '__experimental/plugins/block/block',
url: endpoint,
method: 'DELETE',
},
} );

expect( generator.next().value ).toEqual( {
type: 'REMOVE_INSTALLED_BLOCK_TYPE',
item: itemWithPlugin,
item: itemWithEndpoint,
} );

expect( generator.next() ).toEqual( {
Expand All @@ -179,20 +186,20 @@ describe( 'actions', () => {
} );

it( "should set a global notice if the plugin can't be deleted", () => {
const generator = uninstallBlockType( itemWithPlugin );
const generator = uninstallBlockType( itemWithEndpoint );

expect( generator.next().value ).toMatchObject( {
type: 'API_FETCH',
request: {
path: '__experimental/plugins/block/block',
url: endpoint,
method: 'PUT',
},
} );

expect( generator.next().value ).toMatchObject( {
type: 'API_FETCH',
request: {
path: '__experimental/plugins/block/block',
url: endpoint,
method: 'DELETE',
},
} );
Expand Down

0 comments on commit 6c9ac20

Please sign in to comment.