function ComponentRenderTest::testVariants

Ensure that components variants render.

File

core/tests/Drupal/KernelTests/Components/ComponentRenderTest.php, line 358

Class

ComponentRenderTest
Tests the correct rendering of components.

Namespace

Drupal\KernelTests\Components

Code

public function testVariants() : void {
    $build = [
        '#type' => 'component',
        '#component' => 'sdc_test:my-cta',
        '#variant' => 'primary',
        '#props' => [
            'text' => 'Test link',
        ],
    ];
    $crawler = $this->renderComponentRenderArray($build);
    $this->assertNotEmpty($crawler->filter('#sdc-wrapper a[data-component-id="sdc_test:my-cta"][data-component-variant="primary"][class*="my-cta-primary"]'));
    // If there were an existing prop named variant, we don't override that for BC reasons.
    $build = [
        '#type' => 'component',
        '#component' => 'sdc_test:my-cta-with-variant-prop',
        '#variant' => 'tertiary',
        '#props' => [
            'text' => 'Test link',
            'variant' => 'secondary',
        ],
    ];
    $crawler = $this->renderComponentRenderArray($build);
    $this->assertEmpty($crawler->filter('#sdc-wrapper a[data-component-id="sdc_test:my-cta-with-variant-prop"][data-component-variant="tertiary"]'));
    $this->assertNotEmpty($crawler->filter('#sdc-wrapper a[data-component-id="sdc_test:my-cta-with-variant-prop"][data-component-variant="secondary"]'));
}

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