Skip to content

Commit

Permalink
Merge pull request #202 from awcodes/fix/tests-source-action
Browse files Browse the repository at this point in the history
Fix/tests source action
  • Loading branch information
awcodes authored Nov 17, 2023
2 parents 84daf06 + 9c22725 commit 39dbf62
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 50 deletions.
5 changes: 3 additions & 2 deletions resources/views/tiptap-editor.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
$statePath = $getStatePath();
$isDisabled = $isDisabled();
$blocks = $getBlocks();
$shouldSupportBlocks = $shouldSupportBlocks();
@endphp

@if (config('filament-tiptap-editor.extensions_script') || config('filament-tiptap-editor.extensions_styles'))
Expand Down Expand Up @@ -75,7 +76,7 @@ class="relative z-0 tiptap-wrapper rounded-md bg-white dark:bg-gray-900"
@elseif (is_array($tool))
<x-dynamic-component component="{{ $tool['button'] }}" :state-path="$statePath" />
@elseif ($tool === 'blocks')
@if ($blocks)
@if ($blocks && $shouldSupportBlocks)
<x-filament-tiptap-editor::tools.blocks :blocks="$blocks" :state-path="$statePath" />
@endif
@else
Expand Down Expand Up @@ -135,7 +136,7 @@ class="relative z-0 tiptap-wrapper rounded-md bg-white dark:bg-gray-900"
></div>
</div>

@if (count($blocks) && (! $isDisabled) && in_array('blocks', $tools))
@if (! $isDisabled && $shouldSupportBlocks)
<div
x-data="{
isCollapsed: false,
Expand Down
7 changes: 5 additions & 2 deletions src/Actions/SourceAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ protected function setUp(): void
->modalWidth('screen')
->action(function (TiptapEditor $component, $data) {

$content = tiptap_converter()->asJSON($data['source'], decoded: true);
$content = $data['source'];

$content = $component->renderBlockPreviews($content);
if ($component->shouldSupportBlocks()) {
$content = tiptap_converter()->asJSON($content, decoded: true);
$content = $component->renderBlockPreviews($content, $component);
}

$component->getLivewire()->dispatch(
'insert-content',
Expand Down
16 changes: 13 additions & 3 deletions src/TiptapEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,20 @@ protected function setUp(): void
$this->extensions = config('filament-tiptap-editor.extensions') ?? [];

$this->afterStateHydrated(function (TiptapEditor $component, string | array | null $state) {

if (! $state) {
$component->state('<p></p>');
if ($this->expectsJSON()) {
$component->state(null);
} else {
$component->state('<p></p>');
}
return;
}

if ($this->getBlocks() && $this->expectsJSON()) {
$state = $this->renderBlockPreviews($state, $component);
} elseif ($this->expectsHTML()) {
$state = $this->getHTML();
} elseif ($this->expectsText()) {
$state = $this->getText();
}

$component->state($state);
Expand Down Expand Up @@ -370,4 +375,9 @@ public function verifyListener(TiptapEditor $component, string $statePath): bool
{
return $component->isDisabled() || $statePath !== $component->getStatePath();
}

public function shouldSupportBlocks(): bool
{
return filled($this->getBlocks()) && $this->expectsJSON() && in_array('blocks', $this->getTools());
}
}
32 changes: 6 additions & 26 deletions tests/database/factories/PageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,17 @@ class PageFactory extends Factory
protected $model = Page::class;

public function definition(): array
{
return [
'title' => $this->faker->sentence(),
'html_content' => HtmlFaker::make()
->heading()
->paragraphs(withRandomLinks: true)
->generate(),
'json_content' => tiptap_converter()->asJSON(
HtmlFaker::make()
->heading()
->paragraphs(withRandomLinks: true)
->generate()
),
'text_content' => tiptap_converter()->asText(
HtmlFaker::make()
->heading()
->paragraphs(withRandomLinks: true)
->generate()
),
];
}

public function json(): Factory
{
$content = HtmlFaker::make()
->heading()
->paragraphs(withRandomLinks: true)
->generate();

return $this->state(fn () => [
'content' => tiptap_converter()->asJSON($content)
]);
return [
'title' => $this->faker->sentence(),
'html_content' => $content,
'json_content' => tiptap_converter()->asJSON($content, decoded: true),
'text_content' => tiptap_converter()->asText($content),
];
}
}
4 changes: 4 additions & 0 deletions tests/src/AdminPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace FilamentTiptapEditor\Tests;

use App\TiptapBlocks\BatmanBlock;
use App\TiptapBlocks\StaticBlock;
use App\TiptapBlocks\VideoBlock;
use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\DisableBladeIconComponents;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use Filament\Pages;
use Filament\Panel;
use Filament\PanelProvider;
use FilamentTiptapEditor\Tests\Resources\PageResource;
use FilamentTiptapEditor\TiptapEditor;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
Expand Down
33 changes: 16 additions & 17 deletions tests/src/FormsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,31 @@
->assertFormFieldExists('text_content');
});

it('has proper html', function() {
$page = Page::factory()->make();

Livewire::test(TestComponentWithForm::class)
->fillForm($page->toArray())
->assertFormSet([
'html_content' => $page->html_content,
'json_content' => $page->json_content,
'text_content' => $page->text_content,
]);
});

it('creates proper data', function() {
it('creates record', function() {
$page = Page::factory()->make();

Livewire::test(CreatePage::class)
->fillForm([
'title' => $page->title,
'html_content' => $page->html_content,
'json_content' => $page->json_content,
'text_content' => $page->text_content,
])
->call('create')
->assertHasNoFormErrors();

$this->assertDatabaseHas(Page::class, [
'title' => $page->title,
'html_content' => $page->html_content,
'json_content' => $page->json_content,
]);

$storedPage = Page::query()->where('title', $page->title)->first();

expect($storedPage)
->html_content->toBe($page->html_content)
->json_content->toBe($page->json_content);
});

it('updates proper html', function() {
it('updates record', function() {
$page = Page::factory()->create();
$newData = Page::factory()->make();

Expand All @@ -63,7 +56,13 @@
->call('save')
->assertHasNoFormErrors();

expect($page->refresh())
$this->assertDatabaseHas(Page::class, [
'title' => $newData->title,
]);

$storedPage = Page::query()->where('id', $page->id)->first();

expect($storedPage)
->html_content->toBe($newData->html_content)
->json_content->toBe($newData->json_content);
});
Expand Down
4 changes: 4 additions & 0 deletions tests/src/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ protected static function newFactory(): PageFactory
}

protected $guarded = [];

protected $casts = [
'json_content' => 'array',
];
}
1 change: 1 addition & 0 deletions tests/src/Resources/PageResource/Pages/CreatePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use FilamentTiptapEditor\Tests\Resources\PageResource;
use Filament\Resources\Pages\CreateRecord;
use JetBrains\PhpStorm\NoReturn;

class CreatePage extends CreateRecord
{
Expand Down

0 comments on commit 39dbf62

Please sign in to comment.