<?php

namespace Modules\Module;

use App\View\Setting;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Livewire\Livewire;
use Modules\Module\Components\SettingView;
use Tests\TestCase;

uses(TestCase::class);
uses(DatabaseTransactions::class);

it('it renders page', function () {
    test()->withoutExceptionHandling()->login()->loginNami();

    test()->get('/setting/module')->assertSeeLivewire(SettingView::class);
});

it('it displays acive modules', function () {
    test()->withoutExceptionHandling()->login()->loginNami();
    app(ModuleSettings::class)->fill(['modules' => ['bill']])->save();

    Livewire::test(SettingView::class)
        ->assertSee('Module')
        ->assertSee('Ausbildung')
        ->assertSet('modules', fn ($modules) => $modules === ['bill'])
        ->assertSeeHtml('data-active');
});

it('it saves modules', function () {
    test()->withoutExceptionHandling()->login()->loginNami();
    app(ModuleSettings::class)->fill(['modules' => ['bill']])->save();

    Livewire::test(SettingView::class)
        ->set('modules', ['bill', 'course'])
        ->call('save')
        ->assertDispatched('success', 'Einstellungen gespeichert.');

    test()->assertEquals(['bill', 'course'], app(ModuleSettings::class)->modules);
});

it('test module must exist', function () {
    test()->withoutExceptionHandling()->login()->loginNami();
    app(ModuleSettings::class)->fill(['modules' => ['bill']])->save();

    Livewire::test(SettingView::class)
        ->set('modules', ['bill', 'lala'])
        ->call('save')
        ->assertHasErrors('modules.1');
});