Skip to content

Commit

Permalink
Added tests for new params support in collections (getgrav#3358)
Browse files Browse the repository at this point in the history
  • Loading branch information
w00fz committed Jun 14, 2021
1 parent f1c623c commit acf8724
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions tests/unit/Grav/Common/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,59 @@ public function testAddingCSSAssetPropertiesWithArrayFromCollection(): void
'<link href="/test.css" type="text/css" rel="stylesheet" async>' . PHP_EOL, $css);
}

public function testAddingAssetPropertiesWithArrayFromCollectionAndParameters(): void
{
$this->assets->registerCollection('collection_multi_params', [
'foo.js' => [ 'defer' => true ],
'bar.js' => [ 'integrity' => 'sha512-abc123' ],
'foobar.css' => [ 'defer' => null, 'loading' => null ]
]);

// # Test adding properties with array
$this->assets->addJs('collection_multi_params', ['loading' => 'async']);
$js = $this->assets->js();

// expected output
$expected = [
'<script src="/foo.js" async defer="1"></script>',
'<script src="/bar.js" async integrity="sha512-abc123"></script>',
'<script src="/foobar.css"></script>',
];

self::assertCount(count($expected), array_filter(explode("\n", $js)));
self::assertSame(implode("\n", $expected) . PHP_EOL, $js);

// # Test priority as second argument + render JS should not have any css
$this->assets->reset();
$this->assets->add('low_priority.js', 1);
$this->assets->add('collection_multi_params', 2);
$js = $this->assets->js();

// expected output
$expected = [
'<script src="/foo.js" defer="1"></script>',
'<script src="/bar.js" integrity="sha512-abc123"></script>',
'<script src="/low_priority.js"></script>',
];

self::assertCount(3, array_filter(explode("\n", $js)));
self::assertSame(implode("\n", $expected) . PHP_EOL, $js);

// # Test rendering CSS, should not have any JS
$this->assets->reset();
$this->assets->add('collection_multi_params', [ 'class' => '__classname' ]);
$css = $this->assets->css();

// expected output
$expected = [
'<link href="/foobar.css" type="text/css" rel="stylesheet" class="__classname">',
];


self::assertCount(1, array_filter(explode("\n", $css)));
self::assertSame(implode("\n", $expected) . PHP_EOL, $css);
}

public function testPriorityOfAssets(): void
{
$this->assets->reset();
Expand Down Expand Up @@ -679,6 +732,27 @@ public function testRegisterCollection(): void
self::assertContains('debugger', array_keys($this->assets->getCollections()));
}

public function testRegisterCollectionWithParameters(): void
{
$this->assets->registerCollection('collection_multi_params', [
'foo.js' => [ 'defer' => true ],
'bar.js' => [ 'integrity' => 'sha512-abc123' ],
'foobar.css' => [ 'defer' => null ],
]);

self::assertTrue($this->assets->exists('collection_multi_params'));

$collection = $this->assets->getCollections()['collection_multi_params'];
self::assertArrayHasKey('foo.js', $collection);
self::assertArrayHasKey('bar.js', $collection);
self::assertArrayHasKey('foobar.css', $collection);
self::assertArrayHasKey('defer', $collection['foo.js']);
self::assertArrayHasKey('defer', $collection['foobar.css']);

self::assertNull($collection['foobar.css']['defer']);
self::assertTrue($collection['foo.js']['defer']);
}

public function testReset(): void
{
$this->assets->addInlineJs('alert("test")');
Expand Down

0 comments on commit acf8724

Please sign in to comment.