Clipboard contents after action has been clicked

I want to tests a copy to clipboard action and im wondering if it is possible to grab the clipboard contents inside a test.
6 Replies
LeandroFerreira
LeandroFerreiraβ€’2mo ago
where is the action?
CodeWithDennis
CodeWithDennisOPβ€’2mo ago
Form component (TextInput).
LeandroFerreira
LeandroFerreiraβ€’2mo ago
could you share the code?
CodeWithDennis
CodeWithDennisOPβ€’2mo ago
// Custom action btw
Action::make('copy')
->icon('heroicon-s-clipboard')
->color('gray')
->visible(fn ($state) => $this->getCopyable() && ! empty($state))
->action(function ($livewire, $state, Action $action) {
$action->icon('heroicon-s-clipboard-document-check')
->color('success');

$livewire->dispatch('copy-to-clipboard', text: $state);
}),
// Custom action btw
Action::make('copy')
->icon('heroicon-s-clipboard')
->color('gray')
->visible(fn ($state) => $this->getCopyable() && ! empty($state))
->action(function ($livewire, $state, Action $action) {
$action->icon('heroicon-s-clipboard-document-check')
->color('success');

$livewire->dispatch('copy-to-clipboard', text: $state);
}),
After that is basically writes to the clipboard. navigator.clipboard.writeText(text); But im not sure if we can even access something like that in tests. πŸ€”
LeandroFerreira
LeandroFerreiraβ€’2mo ago
Laravel
Testing | Laravel
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
CodeWithDennis
CodeWithDennisOPβ€’2mo ago
Mmm, what i wanted to test is if the state was matching the clipboard, so have to execute some JS i think πŸ€” This will be a nice addition to it tho.
->assertDispatched('copy-to-clipboard')
->assertDispatched('copy-to-clipboard')
βœ“ it can copy to clipboard with ('email')
βœ“ it can copy to clipboard with ('phone')
βœ“ it can copy to clipboard with ('email')
βœ“ it can copy to clipboard with ('phone')
it('can copy to clipboard', function (string $field) {
$record = Company::factory()->create();

$actionName = 'copy';

livewire(ViewCompany::class, ['record' => $record->getRouteKey()])
->assertSuccessful()
->assertFormComponentActionExists($field, $actionName)
->assertFormComponentActionHasIcon($field, $actionName, 'heroicon-s-clipboard')
->assertFormComponentActionHasColor($field, $actionName, 'gray')
->callFormComponentAction($field, $actionName)
->assertFormComponentActionHasIcon($field, $actionName, 'heroicon-s-clipboard-document-check')
->assertFormComponentActionHasColor($field, $actionName, 'success')
->assertDispatched('copy-to-clipboard');
})->with(['email', 'phone']);
it('can copy to clipboard', function (string $field) {
$record = Company::factory()->create();

$actionName = 'copy';

livewire(ViewCompany::class, ['record' => $record->getRouteKey()])
->assertSuccessful()
->assertFormComponentActionExists($field, $actionName)
->assertFormComponentActionHasIcon($field, $actionName, 'heroicon-s-clipboard')
->assertFormComponentActionHasColor($field, $actionName, 'gray')
->callFormComponentAction($field, $actionName)
->assertFormComponentActionHasIcon($field, $actionName, 'heroicon-s-clipboard-document-check')
->assertFormComponentActionHasColor($field, $actionName, 'success')
->assertDispatched('copy-to-clipboard');
})->with(['email', 'phone']);

Did you find this page helpful?