Skip to content

Commit

Permalink
fix: correctly enqueue dependencies (#52jf92)
Browse files Browse the repository at this point in the history
  • Loading branch information
matzeeable committed May 5, 2020
1 parent 5b036c3 commit 3bdca45
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
19 changes: 19 additions & 0 deletions packages/utils/src/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,25 @@ public function enqueueReact() {
}
}

/**
* Enqueue utils and return an array of dependencies.
*/
public function enqueueUtils() {
$this->enqueueReact();
$this->enqueueMobx();
$scriptDeps = [
self::$HANDLE_REACT,
self::$HANDLE_REACT_DOM,
self::$HANDLE_MOBX,
'moment',
'wp-i18n',
'jquery'
];
$handleUtils = $this->enqueueComposerScript('utils', $scriptDeps);
array_push($scriptDeps, $handleUtils);
return $scriptDeps;
}

/**
* Enqueue mobx state management library.
*/
Expand Down
18 changes: 18 additions & 0 deletions packages/utils/test/phpunit/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,24 @@ public function testEnqueueReactLower168NoReactDom() {
$this->addToAssertionCount(1);
}

public function testEnqueueUtils() {
$scriptDeps = ['react', 'react-dom', 'mobx', 'moment', 'wp-i18n', 'jquery'];
$should = array_merge($scriptDeps, [PHPUNIT_ROOT_SLUG . '-utils']);
$this->assets->shouldReceive('enqueueUtils')->passthru();

$this->assets->shouldReceive('enqueueReact')->once();
$this->assets->shouldReceive('enqueueMobx')->once();
$this->assets
->shouldReceive('enqueueComposerScript')
->once()
->with('utils', $scriptDeps)
->andReturn(PHPUNIT_ROOT_SLUG . '-utils');

$actual = $this->assets->enqueueUtils();

$this->assertEquals($should, $actual);
}

public function testEnqueueMobx() {
$this->assets->shouldReceive('enqueueMobx')->passthru();
$this->assets->shouldReceive('useNonMinifiedSources')->andReturnFalse();
Expand Down
8 changes: 1 addition & 7 deletions plugins/wp-reactjs-starter/src/inc/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,11 @@ public function enqueue_scripts_and_styles($type, $hook_suffix = null) {
return;
}

$this->enqueueReact();
$this->enqueueMobx();

// Your assets implementation here... See utils Assets for enqueue* methods
// $useNonMinifiedSources = $this->useNonMinifiedSources(); // Use this variable if you need to differ between minified or non minified sources
// Our utils package relies on jQuery, but this shouldn't be a problem as the most themes still use jQuery (might be replaced with https://github.com/github/fetch)
$scriptDeps = [self::$HANDLE_REACT, self::$HANDLE_REACT_DOM, self::$HANDLE_MOBX, 'moment', 'wp-i18n', 'jquery'];

// Enqueue external utils package
$handleUtils = $this->enqueueComposerScript('utils', $scriptDeps);
array_push($scriptDeps, $handleUtils);
$scriptDeps = $this->enqueueUtils();

// Enqueue plugin entry points
if ($type === self::$TYPE_ADMIN) {
Expand Down
6 changes: 2 additions & 4 deletions plugins/wp-reactjs-starter/test/phpunit/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public function testEnqueueScriptsAndStylesAdmin() {
$usedHandle = PHPUNIT_SLUG . '-admin';

$this->assets->shouldReceive('enqueue_scripts_and_styles')->passthru();
$this->assets->shouldReceive('enqueueReact');
$this->assets->shouldReceive('enqueueMobx');
$this->assets->shouldReceive('enqueueUtils')->once()->andReturn(['react']);
$this->assets->shouldReceive('enqueueComposerScript')->with('utils', Mockery::any());
$this->assets
->shouldReceive('enqueueScript')
Expand All @@ -48,8 +47,7 @@ public function testEnqueueScriptsAndStylesFrontend() {
$usedHandle = PHPUNIT_SLUG . '-widget';

$this->assets->shouldReceive('enqueue_scripts_and_styles')->passthru();
$this->assets->shouldReceive('enqueueReact');
$this->assets->shouldReceive('enqueueMobx');
$this->assets->shouldReceive('enqueueUtils')->once()->andReturn(['react']);
$this->assets->shouldReceive('enqueueComposerScript')->with('utils', Mockery::any());
$this->assets
->shouldReceive('enqueueScript')
Expand Down

0 comments on commit 3bdca45

Please sign in to comment.