You are here

public function JsAlterEventTest::testJsAlterEventBySet in Hook Event Dispatcher 8

JsAlterEvent by set test.

File

tests/src/Unit/Theme/JsAlterEventTest.php, line 70

Class

JsAlterEventTest
Class JsAlterEventTest.

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Theme

Code

public function testJsAlterEventBySet() {
  $newJavascript = [
    'new' => [
      'new-data',
    ],
  ];
  $this->manager
    ->setEventCallbacks([
    HookEventDispatcherInterface::JS_ALTER => function (JsAlterEvent $event) use ($newJavascript) {
      $javascript = $event
        ->getJavascript();
      $javascript += $newJavascript;
      $event
        ->setJavascript($javascript);
    },
  ]);
  $javascript = $expectedJavascript = [
    'some' => [
      'data',
    ],
    'other' => [
      'other_data',
    ],
  ];
  $expectedJavascript += $newJavascript;
  $attachedAssets = new AttachedAssets();
  hook_event_dispatcher_js_alter($javascript, $attachedAssets);

  /** @var \Drupal\hook_event_dispatcher\Event\Theme\JsAlterEvent $event */
  $event = $this->manager
    ->getRegisteredEvent(HookEventDispatcherInterface::JS_ALTER);
  self::assertSame($expectedJavascript, $event
    ->getJavascript());
  self::assertSame($attachedAssets, $event
    ->getAttachedAssets());
}