skip to Main Content

I’m using Laravel 10 with Livewire 2. I was writing a simple test for a component, which is just an upload form. I ran into a cryptic message error:

FAILED  TestsFeatureLivewireUploadAssetTest > book can be saved correctly                                                             
Type is not supported

  at vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php:88
     84▕             default => json_encode($data, $this->encodingOptions),
     85▕         };
     86▕ 
     87▕         if (! $this->hasValidJson(json_last_error())) {
  ➜  88▕             throw new InvalidArgumentException(json_last_error_msg());
     89▕         }
     90▕ 
     91▕         return $this->update();
     92▕     }

The test is:

    /** @test */
    public function book_can_be_saved_correctly()
    {
        $admin = User::factory()->create();
        $this->actingAs($admin);

        Storage::fake('private');

        $testBook = UploadedFile::fake()->create('private/books/test_book.pdf');
        $testCover = UploadedFile::fake()->image('private/covers/test_image.png');

        $emptyForm = (new UploadBookService())->buildForm();

        $book = array_merge($emptyForm,[
            'title' => 'Title Test',
            'author' => 'Author Test',
            'description' => 'Description Test',
            'publishedAt' => '2022',
            'cover' => $testCover,
            'book' => $testBook,
        ]);

        $component = Livewire::test(UploadAsset::class)
            ->set('assetMeta', $book)
            ->call('save');

            Storage::assertExists('books/test_book.pdf');
            Storage::assertExists('covers/test_cover.png');
    }

For what I’ve debugged so far, the error is triggered once i call set() in the test.
I can confirm that the component is working while using it.

2

Answers


  1. Chosen as BEST ANSWER

    Ok, I guess I found an answer: probably the objects inside the array are not parsed correctly, so if I modify my test in:

        /** @test */
        public function book_can_be_saved_correctly()
        {
            $admin = User::factory()->create();
            $this->actingAs($admin);
    
            Storage::fake('private');
    
            $testBook = UploadedFile::fake()->create('test_book.pdf');
            $testCover = UploadedFile::fake()->image('test_image.png');
    
            $emptyForm = (new UploadBookService())->buildForm();
    
            $book = array_merge($emptyForm,[
                'title' => 'Title Test',
                'author' => 'Author Test',
                'description' => 'Description Test',
                'publishedAt' => '2022',
            ]);
    
            $component = Livewire::test(UploadAsset::class)
                ->set('assetMeta', $book)
                ->set('assetMeta.book', $testBook)
                ->set('assetMeta.cover', $testCover)
                ->call('save');
    
                Storage::assertExists('test_book.pdf');
                Storage::assertExists('test_cover.png');
        }
    

    by separately calling the set() for assetMeta.book and assetMeta.cover the Type error disappears.


  2. The error is that you are passing a value that is not compatible with the json_encode() function, I suggest the following:

    $book = array_merge($emptyForm,[
        'title' => 'Title Test',
        'author' => 'Author Test',
        'description' => 'Description Test',
        'publishedAt' => '2022',
        'coverPath' => 'private/covers/test_image.png',
        'bookPath' => 'private/books/test_book.pdf', 
    ]);
    
    $component = Livewire::test(UploadAsset::class)
        ->set('assetMeta', $book)
        ->call('save');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search