function AttachedAssetsTest::testRenderDifferentWeight

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php \Drupal\KernelTests\Core\Asset\AttachedAssetsTest::testRenderDifferentWeight()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php \Drupal\KernelTests\Core\Asset\AttachedAssetsTest::testRenderDifferentWeight()
  3. 11.x 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 356

Class

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

Namespace

Drupal\KernelTests\Core\Asset

Code

public function testRenderDifferentWeight() : void {
  // 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, \Drupal::languageManager()->getCurrentLanguage())[1];
  $js_render_array = \Drupal::service('asset.js.collection_renderer')->render($js);
  $rendered_js = (string) $this->renderer
    ->renderInIsolation($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'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.