From 62c4f8334ab24a65104f4bde7a3a35e7978f096a Mon Sep 17 00:00:00 2001 From: Julian Rutten Date: Thu, 10 Mar 2022 23:43:09 +0100 Subject: [PATCH] chore: add docs and test for issue #36 --- CHANGELOG.md | 2 +- src/BootstrapWidgetTrait.php | 3 ++- tests/ToastTest.php | 47 ++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 661f4ff..927fe75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ Yii Framework 2 bootstrap5 extension Change Log 2.0.3 under development ----------------------- - +- Enh #36: `BootstrapWidgetTrait::$clientOptions = false` disables registerJs in `BootstrapWidgetTrait` (julianrutten) - Enh #33: Updated russian translations (WinterSilence) - Enh #28: Added translations (simialbi) - Enh #24: Accept `Breadcrumbs::$homeLink = false` to omit "Home" link (fetus-hina) diff --git a/src/BootstrapWidgetTrait.php b/src/BootstrapWidgetTrait.php index 5f75e84..3c91846 100644 --- a/src/BootstrapWidgetTrait.php +++ b/src/BootstrapWidgetTrait.php @@ -35,10 +35,11 @@ use yii\helpers\Json; trait BootstrapWidgetTrait { /** - * @var array the options for the underlying Bootstrap JS plugin. + * @var array|bool the options for the underlying Bootstrap JS plugin. * Please refer to the corresponding Bootstrap plugin Web page for possible options. * For example, [this page](http://getbootstrap.com/javascript/#modals) shows * how to use the "Modal" plugin and the supported options (e.g. "remote"). + * If this property is false, registerJs will not be called on the view to initialize the module. */ public $clientOptions = []; /** diff --git a/tests/ToastTest.php b/tests/ToastTest.php index 2ff1340..0d8add4 100644 --- a/tests/ToastTest.php +++ b/tests/ToastTest.php @@ -142,4 +142,51 @@ HTML; $this->assertContainsWithoutLE("jQuery('#w0').toast();", $options); } + + /** + * @see https://github.com/yiisoft/yii2-bootstrap5/issues/36 + */ + public function testWidgetNoInitialization() + { + Toast::$counter = 0; + ob_start(); + $toast = Toast::begin([ + 'title' => 'Toast title', + 'clientOptions' => false, + 'titleOptions' => ['tag' => 'h5', 'style' => ['text-align' => 'left']] + ]); + echo 'test'; + Toast::end(); + $out = ob_get_clean(); + + $this->assertFalse($toast->clientOptions); + $this->assertArrayNotHasKey(View::POS_READY,Yii::$app->view->js); + } + + /** + * + * @see https://github.com/yiisoft/yii2-bootstrap5/issues/36 + */ + public function testWidgetInitializationTrue() + { + Toast::$counter = 0; + ob_start(); + $toast = Toast::begin([ + 'title' => 'Toast title', + 'clientOptions' => true, + 'titleOptions' => ['tag' => 'h5', 'style' => ['text-align' => 'left']] + ]); + echo 'test'; + Toast::end(); + $out = ob_get_clean(); + + $this->assertTrue($toast->clientOptions); + $this->assertArrayHasKey(View::POS_READY,Yii::$app->view->js); + $js = Yii::$app->view->js[View::POS_READY]; + + $this->assertInternalType(IsType::TYPE_ARRAY, $js); + $options = array_shift($js); + + $this->assertContainsWithoutLE("jQuery('#w0').toast(true);", $options); + } }