Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for the head component #923

Merged
merged 25 commits into from
Feb 5, 2023
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
fba5f48
Create HeadComponentViewTest.php
caendesilva Feb 5, 2023
0491b88
Create render testing helper
caendesilva Feb 5, 2023
c00dc1a
Test component can be rendered
caendesilva Feb 5, 2023
4c0a276
Change snake_case test method name to camelCase
caendesilva Feb 5, 2023
67b9374
Refactor mock control
caendesilva Feb 5, 2023
da0564a
Test rendered title element uses the page's htmlTitle
caendesilva Feb 5, 2023
b460bcf
Change test to mock method as actual composition is irrelevant to sco…
caendesilva Feb 5, 2023
c24ec46
Fix property assignment call
caendesilva Feb 5, 2023
c4d7605
Reset value after use
caendesilva Feb 5, 2023
f661e74
Introduce local variable
caendesilva Feb 5, 2023
d510963
Wrap code to single line
caendesilva Feb 5, 2023
ea2180b
Mock includes
caendesilva Feb 5, 2023
7954cec
Replace code comment with extracted helper method
caendesilva Feb 5, 2023
a31e74e
Remove site name from expectation
caendesilva Feb 5, 2023
5b7ebbb
Call on mocked property directly
caendesilva Feb 5, 2023
d005a58
Inline local test variables
caendesilva Feb 5, 2023
2a4d914
Simplify mocked page handling
caendesilva Feb 5, 2023
9b092c5
Test the favicon link
caendesilva Feb 5, 2023
0f901b2
Test component inclusion
caendesilva Feb 5, 2023
ac88a76
Update includes mocker to escape includes instead
caendesilva Feb 5, 2023
cbb7041
Rename helper method for refactored usage
caendesilva Feb 5, 2023
665c05b
Split out test with multiple assertions to two tests
caendesilva Feb 5, 2023
ad37bf2
Put repeated code in setup method
caendesilva Feb 5, 2023
e1e2bce
Apply fixes from StyleCI
StyleCIBot Feb 5, 2023
e98b44c
Merge branch 'master' into update-head-component
caendesilva Feb 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions packages/framework/tests/Unit/Views/HeadComponentViewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Testing\Unit\Views;

use Hyde\Hyde;
use Hyde\Pages\VirtualPage;
use Hyde\Testing\TestCase;
use Illuminate\Support\Facades\Blade;

/**
* @see resources/views/layouts/styles.blade.php
*/
class HeadComponentViewTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

$this->mockPage();
}

protected function renderTestView(): string
{
return Blade::render($this->escapeIncludes(file_get_contents(Hyde::vendorPath('resources/views/layouts/head.blade.php'))));
}

public function testComponentCanBeRendered()
{
$this->mockPage();
$this->assertStringContainsString('<meta charset="utf-8">', $this->renderTestView());
}

public function testTitleElementUsesPageHtmlTitle()
{
$page = $this->createMock(VirtualPage::class);
$page->method('htmlTitle')->willReturn('Foo Bar');
$this->mockPage($page);

$this->assertStringContainsString('<title>Foo Bar</title>', $this->renderTestView());
}

public function testLinkToFaviconIsNotAddedWhenFileDoesNotExist()
{
$this->assertStringNotContainsString('favicon', $this->renderTestView());
}

public function testLinkToFaviconIsAddedWhenFileExists()
{
$this->mockPage();
$this->file('_media/favicon.ico');

$this->assertStringContainsString('<link rel="shortcut icon" href="media/favicon.ico" type="image/x-icon">', $this->renderTestView());
}

public function testLinkToFaviconUsesRelativeUrl()
{
$this->file('_media/favicon.ico');
$this->mockPage(currentPage: 'foo/bar');

$this->assertStringContainsString('<link rel="shortcut icon" href="../media/favicon.ico" type="image/x-icon">', $this->renderTestView());
}

public function testComponentIncludesMetaView()
{
$this->assertStringContainsString("@include('hyde::layouts.meta')", $this->renderTestView());
}

public function testComponentIncludesStylesView()
{
$this->assertStringContainsString("@include('hyde::layouts.styles')", $this->renderTestView());
}

protected function escapeIncludes(string $contents): string
{
return str_replace('@include', '@@include', $contents);
}
}