Skip to content

Commit

Permalink
fix: usage of React while using Divi in dev environment (WP_DEBUG, #3…
Browse files Browse the repository at this point in the history
…rfqjk)
  • Loading branch information
matzeeable committed Mar 9, 2020
1 parent 8b11589 commit bcbeec6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/utils/src/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,16 @@ public function enqueueReact() {
if ($coreReact !== false && version_compare($coreReact->ver, '16.8', '<')) {
// Replace the already existing React version with ours
$publicFolder = $this->getPublicFolder(true);
$publicSrc = $publicFolder . ($useNonMinifiedSources ? $reactDev : $reactProd);
$devFileExists = file_exists(
$this->getPluginConstant(PluginReceiver::$PLUGIN_CONST_PATH) . '/' . $publicFolder . $reactDev
);
$publicSrc = $publicFolder . ($useNonMinifiedSources && $devFileExists ? $reactDev : $reactProd);
$coreReact->src = plugins_url($publicSrc, $this->getPluginConstant(PluginReceiver::$PLUGIN_CONST_FILE));

// Also use our ReactDOM, instead we get this error: https://reactjs.org/docs/error-decoder.html/?invariant=321
$coreReactDom = wp_scripts()->query(self::$HANDLE_REACT_DOM);
if ($coreReactDom !== false) {
$publicSrc = $publicFolder . ($useNonMinifiedSources ? $reactDomDev : $reactDomProd);
$publicSrc = $publicFolder . ($useNonMinifiedSources && $devFileExists ? $reactDomDev : $reactDomProd);
$coreReactDom->src = plugins_url(
$publicSrc,
$this->getPluginConstant(PluginReceiver::$PLUGIN_CONST_FILE)
Expand Down
35 changes: 35 additions & 0 deletions packages/utils/test/phpunit/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ public function testEnqueueReactLower168Dev() {

$this->assets->shouldReceive('getPublicFolder')->with(true);

redefine('file_exists', always(true));

WP_Mock::userFunction('plugins_url', [
'times' => 1,
'args' => ['react/umd/react.development.js', PHPUNIT_FILE]
Expand Down Expand Up @@ -273,6 +275,8 @@ public function testEnqueueReactLower168NoReactDom() {

$this->assets->shouldReceive('getPublicFolder')->with(true);

redefine('file_exists', always(true));

WP_Mock::userFunction('plugins_url', [
'times' => 1,
'args' => ['react/umd/react.production.min.js', PHPUNIT_FILE]
Expand All @@ -289,6 +293,37 @@ public function testEnqueueReactLower168NoReactDom() {
$this->addToAssertionCount(1);
}

public function testEnqueueReactLower168DevNotExisting() {
$this->assets->shouldReceive('enqueueReact')->passthru();
$this->assets->shouldReceive('useNonMinifiedSources')->andReturnTrue();

redefine(WP_Scripts::class . '::query', always((object) ['ver' => '16.7']));

WP_Mock::userFunction('wp_scripts', ['return' => new WP_Scripts()]);

$this->assets->shouldReceive('getPublicFolder')->with(true);

redefine('file_exists', always(false));

WP_Mock::userFunction('plugins_url', [
'times' => 1,
'args' => ['react/umd/react.production.min.js', PHPUNIT_FILE]
]);
WP_Mock::userFunction('plugins_url', [
'times' => 1,
'args' => ['react-dom/umd/react-dom.production.min.js', PHPUNIT_FILE]
]);

$this->assets->shouldNotReceive('enqueueLibraryScript')->with(Assets::$HANDLE_REACT, Mockery::any());
$this->assets
->shouldNotReceive('enqueueLibraryScript')
->with(Assets::$HANDLE_REACT_DOM, Mockery::any(), Assets::$HANDLE_REACT);

$this->assets->enqueueReact();

$this->addToAssertionCount(1);
}

public function testEnqueueMobx() {
$this->assets->shouldReceive('enqueueMobx')->passthru();
$this->assets->shouldReceive('useNonMinifiedSources')->andReturnFalse();
Expand Down

0 comments on commit bcbeec6

Please sign in to comment.