Skip to content

Conversation

@mtvbrianking
Copy link
Contributor

Improve how to determine if the current request probably expects a JSON response.

That following test can be simplified be adapting $request->expectsJson()

 public function test_a_user_can_view_any_users()
 {
     $user = User::factory()->create();

-    $jsonResponse = $this->actingAs($user)
-        ->withHeaders([
-            'Accept' => 'application/json',
-            'X-Requested-With' => 'XMLHttpRequest',
-        ])
-        ->get(route('users.index'));

+    $jsonResponse = $this->actingAs($user)->json('GET', route('users.index'));

     $jsonResponse->assertSuccessful();

     $this->assertArrayNotHasKey('error', $jsonResponse->getData(true));
 }

Improve how to determine if the current request probably expects a JSON response.

That following test can be simplified be adapting [`$request->expectsJson()`](https://github.com/laravel/framework/blob/master/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php#L24)

```diff
 public function test_a_user_can_view_any_users()
 {
     $user = User::factory()->create();

-    $jsonResponse = $this->actingAs($user)
-        ->withHeaders([
-            'Accept' => 'application/json',
-            'X-Requested-With' => 'XMLHttpRequest',
-        ])
-        ->get(route('users.index'));

+    $jsonResponse = $this->actingAs($user)->json('GET', route('users.index'));

     $jsonResponse->assertSuccessful();

     $this->assertArrayNotHasKey('error', $jsonResponse->getData(true));
 }
```
@yajra yajra merged commit 4337edb into yajra:4.0 Jan 2, 2021
@yajra
Copy link
Owner

yajra commented Jan 2, 2021

Released on v4.10.3, thanks and happy new year!

@mtvbrianking mtvbrianking deleted the patch-1 branch January 2, 2021 17:33
@amir-67
Copy link

amir-67 commented Jan 9, 2021

This commit breaks some functionalities of my project. Sometimes I load datatables by making an ajax post request:

$returnHTML = $dataTable->with(["model" => $model])->render('backend.media.get_media_datatables', $data);
        
  if(request()->isMethod('post')){
      $returnHTML = $returnHTML->render();
      return response()->json(['html' => $returnHTML]);
  }

and now I get the next exception:

Method Illuminate\Http\JsonResponse::render does not exist

Can you revert the commit, please?

I have the same problem so revert it to previous version.

@mtvbrianking
Copy link
Contributor Author

mtvbrianking commented Jan 9, 2021

An AJAX request will result into a JSON response. There's no render method on a JSON response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants