You are here

public function AttachedAssetsTest::testRenderDifferentWeight in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php \Drupal\KernelTests\Core\Asset\AttachedAssetsTest::testRenderDifferentWeight()

Tests rendering the JavaScript with a file's weight above jQuery's.

File

core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php, line 377

Class

AttachedAssetsTest
Tests #attached assets: attached asset libraries and JavaScript settings.

Namespace

Drupal\KernelTests\Core\Asset

Code

public function testRenderDifferentWeight() {

  // If a library contains assets A and B, and A is listed first, then B can
  // still make itself appear first by defining a lower weight.
  $build['#attached']['library'][] = 'core/jquery';
  $build['#attached']['library'][] = 'common_test/weight';
  $assets = AttachedAssets::createFromRenderArray($build);
  $js = $this->assetResolver
    ->getJsAssets($assets, FALSE)[1];
  $js_render_array = \Drupal::service('asset.js.collection_renderer')
    ->render($js);
  $rendered_js = $this->renderer
    ->renderPlain($js_render_array);

  // Verify that lighter CSS assets are rendered first.
  $this
    ->assertLessThan(strpos($rendered_js, 'first.js'), strpos($rendered_js, 'lighter.css'));

  // Verify that lighter JavaScript assets are rendered first.
  $this
    ->assertLessThan(strpos($rendered_js, 'first.js'), strpos($rendered_js, 'lighter.js'));

  // Verify that a JavaScript file is rendered before jQuery.
  $this
    ->assertLessThan(strpos($rendered_js, 'core/assets/vendor/jquery/jquery.min.js'), strpos($rendered_js, 'before-jquery.js'));
}