F
FilamentGeoff.

Macro is not working when testing with Pest

Hello all I am using this plugin https://github.com/outer-web/filament-translatable-fields and when I want to write a test now I am getting the error that translatable does not exist. How to add the plugin also in the test environment? Here is the error: Illuminate\View\ViewException: Method Filament\Forms\Components\TextInput::translatable does not exist. Here is the test:
it('can validate form', function () {
$rules = [
'description' => ['required', 'max:255'],
'code' => ['required', 'max:6'],
'type' => ['required',Rule::enum(FoodBeverage::class)],
'vat_id' => ['required', 'exists:vat_codes,id'],
];
livewire(CreateProductGroup::class)
->fillForm([
'description' => '',
'code' => '',
])
->call('create')
->assertHasFormErrors($rules);
});
it('can validate form', function () {
$rules = [
'description' => ['required', 'max:255'],
'code' => ['required', 'max:6'],
'type' => ['required',Rule::enum(FoodBeverage::class)],
'vat_id' => ['required', 'exists:vat_codes,id'],
];
livewire(CreateProductGroup::class)
->fillForm([
'description' => '',
'code' => '',
])
->call('create')
->assertHasFormErrors($rules);
});
Solution:
Yeah, I will just add
Filament::bootCurrentPanel();
Filament::bootCurrentPanel();
to my setup before each test. This is working
GitHub
GitHub - outer-web/filament-translatable-fields
Contribute to outer-web/filament-translatable-fields development by creating an account on GitHub.
DK
Dennis Koch17d ago
Do you have more than 1 panel? And is this your default panel? Looking at the plugin, the Macro is registered during Plugin::boot(). So we need to figure out why the plugin is not booted.
G
Geoff.17d ago
Yes I have mor than 1 panel, this is on my app panel and the other panel is the admin panel
DK
Dennis Koch17d ago
I guess admin panel is the default one?
G
Geoff.17d ago
Ye indeed But Plugin is registered on the AppPanelProvider
DK
Dennis Koch17d ago
So you need to set the current panel. I found this in another thread:
\Filament\Facades\Filament::setCurrentPanel(
\Filament\Facades\Filament::getPanel('app')
);
\Filament\Facades\Filament::setCurrentPanel(
\Filament\Facades\Filament::getPanel('app')
);
https://discord.com/channels/883083792112300104/1169961796208185464/1170996029659820203
G
Geoff.17d ago
Thats already in my test
\Filament\Facades\Filament::setCurrentPanel(\Filament\Facades\Filament::getPanel('app'));
\Filament\Facades\Filament::setTenant($store, true);
\Filament\Facades\Filament::setCurrentPanel(\Filament\Facades\Filament::getPanel('app'));
\Filament\Facades\Filament::setTenant($store, true);
DK
Dennis Koch17d ago
Where did you put this? Because it's not included in your code above?
G
Geoff.17d ago
Ah it is on a function i run before each test
public function TenantLogin(): void
{
tenancy()->initialize(getTenant());
$user = \App\Models\App\User::query()->where('name', 'admin')->first();
$store = Store::query()->where('slug', 'test')->first();

$this->actingAs($user);

\Filament\Facades\Filament::setCurrentPanel(\Filament\Facades\Filament::getPanel('app'));
\Filament\Facades\Filament::setTenant($store, true);
}
public function TenantLogin(): void
{
tenancy()->initialize(getTenant());
$user = \App\Models\App\User::query()->where('name', 'admin')->first();
$store = Store::query()->where('slug', 'test')->first();

$this->actingAs($user);

\Filament\Facades\Filament::setCurrentPanel(\Filament\Facades\Filament::getPanel('app'));
\Filament\Facades\Filament::setTenant($store, true);
}
beforeEach(function () {
createTenantDatabase();
$this->TenantLogin();
});
beforeEach(function () {
createTenantDatabase();
$this->TenantLogin();
});
DK
Dennis Koch17d ago
Hm. Looks good to me. Can you make sure the code is running? And output Filament::getCurrentPanel() after setting it and just before your test code is running?
G
Geoff.17d ago
ds(Filament::getCurrentPanel());
\Filament\Facades\Filament::setCurrentPanel(\Filament\Facades\Filament::getPanel('app'));
ds(Filament::getCurrentPanel());
ds(Filament::getCurrentPanel());
\Filament\Facades\Filament::setCurrentPanel(\Filament\Facades\Filament::getPanel('app'));
ds(Filament::getCurrentPanel());
First log is giving admin and second one is giving app
DK
Dennis Koch17d ago
And if you put it right before livewire(CreateProductGroup::class)? Is it still app?
G
Geoff.17d ago
Yeah still app
DK
Dennis Koch17d ago
Hm weird. Let me reconstruct this quickly
G
Geoff.17d ago
When I ds in the boot function I don't get any log
DK
Dennis Koch17d ago
Boot of the plugin? Yeah, that's what I am trying to figure out 😅
G
Geoff.17d ago
Yeah indeed 😄
DK
Dennis Koch17d ago
What if you do it in Filament\Panel::boot()? ds($this->id)?
G
Geoff.17d ago
Where to put this?
DK
Dennis Koch17d ago
vendor/filament/filament/src/Panel.php
G
Geoff.17d ago
Ok, its logging nothing and when going to the webpage it is logging correct
DK
Dennis Koch17d ago
Hm. Actually I don't know where we are booting Panels. If you boot it manually that could work, but not sure whether that's the ideal solution:
$panel = Filament::getPanel('app');
Filament::setCurrentPanel($panel);
$panel->boot();
$panel = Filament::getPanel('app');
Filament::setCurrentPanel($panel);
$panel->boot();
G
Geoff.17d ago
Yeah thats working.
DK
Dennis Koch17d ago
@Dan Harrin Sorry to tag you, but can you maybe shed some light on where Panels are booted and how we could test a different panel? Manual booting feels a bit hacky.
G
Geoff.17d ago
Really appreciate the help btw I think this could be ok to use
DH
Dan Harrin17d ago
panels are booted by middleware based on the current route
G
Geoff.17d ago
Filament::bootCurrentPanel();
Filament::bootCurrentPanel();
DH
Dan Harrin17d ago
you could boot in the setUp() of the test case if you want it to apply to multiple tests
DK
Dennis Koch17d ago
I thought so. But since we are directly testing the LW components there is no nicer way then manually setting the current panel and booting, right?
DH
Dan Harrin17d ago
no, but as i said you can extract it
Solution
G
Geoff.17d ago
Yeah, I will just add
Filament::bootCurrentPanel();
Filament::bootCurrentPanel();
to my setup before each test. This is working
G
Geoff.17d ago
Thanks for the help ❤️
S
Simon16d ago
Hi, Simon here, the developer of the package mentioned here. I see a solution has been found. Is there anything I could change in the package itself to make this easier/better or am I registering the macro correctly in the boot() method of the plugin?
Want results from more Discord servers?
Add your server
More Posts
Why the authenticate rateLimit &throttled not work ?Why the authenticate rateLimit &throttled not work ?TextColumn action->button not working as expected```php Tables\Columns\TextColumn::make('asdas') ->label('See 2 items') ->action( TabFilament resource not showing on websitei made a migration, model and now a resource i followed a tutorial to see how i can make a resource How to translate filament filepond placeholder?I've tried to find it in the vendor, but idk how to translate itFill Select on relationmanager creation formHow can i pre-fill parent relation selector on a create form on a relationmananger? On the Practice Help with github actionsI copied the workflow file from filament: ```yml name: phpstan on: push: pull_request: jobs: File upload preview green gradientHow do I remove the green gradient so that I can see the whole logo while uploading it ? ```<?php Export to Excel - sub folder does not get url correctly.My app referer to the sub directy as : domain.com/demo/meterbill -> link to public folder in LaravelHow I can create Form or Table in another classF.E I want to create another class with form or tableUpdate each repeater field 2 fields outside the repeaterI'm playing around with creating a invoice in Filament and I've created a form that has multiple fieCan't access User profile "isSimple:false" on Team tenantHi. Trying to get to User's profile (http://app.test/dashboard/profile) This works public functHow to handle API error response?I have a resource, once you have create/update/delete action, it will call an API endpoint. How can Select Filter List TruncatedHi Guys, I have a select filter which has more then 50 options, however the list has been truncated Column relationships problemHey ! In a relation manager resource, i have this code : ```public function table(Table $table): custom themeHello, when I create a custom theme (php artisan make:filament-theme) and configure my vite.js fToggle-columnIs there any way to make toggle-column from scratch on a custom page ?$getAction() and visibilityWhen using ->registerActions() on a Form or Infolist component, it seems that visible() and hidden()Modal action or table inside formMy use case is a CRM application where each resource only has an edit screen. A list of related resoCustomizing Data Insertion with Filament's Import ActionHello, Regarding the import: https://filamentphp.com/docs/3.x/actions/prebuilt-actions/import Is iCount SaleItems sold by Product IDHello, I have a Sales Resource with (hasMany) SaleItems Resource which selects a product to sell fro