<?php

declare(strict_types=1);

namespace Drupal\Tests\simple_interactive_maps\Unit;

use Drupal\simple_interactive_maps\ClippableContent;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;

/**
 * Tests ClippableContent::createClippableItem().
 */
#[Group('simple_interactive_maps')]
#[CoversClass(ClippableContent::class)]
final class ClippableContentTest extends UnitTestCase {

  /**
   * Tests that the default wrapper tag is a div.
   */
  public function testDefaultWrapperTag(): void {
    $build = (new ClippableContent())->createClippableItem('[interactive_map map=us_states]', 'embed_code');

    $this->assertSame('html_tag', $build['#type']);
    $this->assertSame('div', $build['#tag']);
    $this->assertSame('[interactive_map map=us_states]', $build['#value']);
    $this->assertSame(['clippable-content__wrapper embed_code__wrapper'], $build['#attributes']['class']);
    $this->assertSame(['simple_interactive_maps/clippable_content'], $build['#attached']['library']);
  }

  /**
   * Tests that a custom wrapper tag can be supplied.
   */
  public function testCustomWrapperTag(): void {
    $build = (new ClippableContent())->createClippableItem('text', 'my_id', 'span');
    $this->assertSame('span', $build['#tag']);
  }

  /**
   * Tests that the copy link carries the clippable text.
   */
  public function testCopyLinkCarriesTheClippableText(): void {
    $build = (new ClippableContent())->createClippableItem('copy-me', 'my_id');

    $this->assertSame('html_tag', $build['copyLink']['#type']);
    $this->assertSame('span', $build['copyLink']['#tag']);
    $this->assertSame('copy-me', $build['copyLink']['#attributes']['data-clipboard-text']);
    $this->assertSame(['clippable-content__link copy-link my_id'], $build['copyLink']['#attributes']['class']);
  }

}
