diff --git a/packages/utils/src/Assets.php b/packages/utils/src/Assets.php index 947c256..719b9cc 100755 --- a/packages/utils/src/Assets.php +++ b/packages/utils/src/Assets.php @@ -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. */ diff --git a/packages/utils/test/phpunit/AssetsTest.php b/packages/utils/test/phpunit/AssetsTest.php index f7651a6..4a785d9 100755 --- a/packages/utils/test/phpunit/AssetsTest.php +++ b/packages/utils/test/phpunit/AssetsTest.php @@ -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(); diff --git a/plugins/wp-reactjs-starter/src/inc/Assets.php b/plugins/wp-reactjs-starter/src/inc/Assets.php index 3609ad9..175057a 100644 --- a/plugins/wp-reactjs-starter/src/inc/Assets.php +++ b/plugins/wp-reactjs-starter/src/inc/Assets.php @@ -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) { diff --git a/plugins/wp-reactjs-starter/test/phpunit/AssetsTest.php b/plugins/wp-reactjs-starter/test/phpunit/AssetsTest.php index 11deaae..63571b6 100755 --- a/plugins/wp-reactjs-starter/test/phpunit/AssetsTest.php +++ b/plugins/wp-reactjs-starter/test/phpunit/AssetsTest.php @@ -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') @@ -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')