|
794 | 794 |
|
795 | 795 | })->throws(\HamidRrj\LaravelDatatable\Exceptions\InvalidSortingException::class, "sorting field `name` is not allowed."); |
796 | 796 |
|
797 | | -it('can get data with descending sort on id', function (){ |
| 797 | +it('can get data with descending sort on age', function (){ |
798 | 798 |
|
799 | 799 | $users = User::factory() |
800 | 800 | ->count(6) |
|
808 | 808 | 'filters' => json_encode([]), |
809 | 809 | 'sorting' => json_encode([ |
810 | 810 | [ |
811 | | - 'id' => 'id', |
| 811 | + 'id' => 'age', |
812 | 812 | 'desc' => true, |
813 | 813 | ] |
814 | 814 | ]) |
815 | 815 | ]; |
816 | 816 |
|
817 | | - $allowedSortings = array('id'); |
| 817 | + $allowedSortings = array('age'); |
818 | 818 |
|
819 | 819 | $data = DatatableFacade::run( |
820 | 820 | $query, |
|
823 | 823 | ); |
824 | 824 |
|
825 | 825 | $expected = $users->toArray(); |
826 | | - array_multisort( array_column($expected, "id"), SORT_DESC, $expected); |
| 826 | + array_multisort( array_column($expected, "age"), SORT_DESC, $expected); |
| 827 | + |
| 828 | + expect($data['data']) |
| 829 | + ->toEqual($expected); |
| 830 | + |
| 831 | + expect($data['meta']['totalRowCount']) |
| 832 | + ->toBe(6); |
| 833 | +}); |
827 | 834 |
|
828 | | - expect($data) |
829 | | - ->toEqual([ |
830 | | - 'data' => $expected, |
831 | | - 'meta' => [ |
832 | | - 'totalRowCount' => 6 |
| 835 | +it('can get data with ascending sort on age', function (){ |
| 836 | + |
| 837 | + $users = User::factory() |
| 838 | + ->count(6) |
| 839 | + ->create(); |
| 840 | + |
| 841 | + $query = User::query(); |
| 842 | + |
| 843 | + $requestParameters = [ |
| 844 | + 'start' => 0, |
| 845 | + 'size' => 10, |
| 846 | + 'filters' => json_encode([]), |
| 847 | + 'sorting' => json_encode([ |
| 848 | + [ |
| 849 | + 'id' => 'age', |
| 850 | + 'desc' => false, |
833 | 851 | ] |
834 | | - ]); |
| 852 | + ]) |
| 853 | + ]; |
| 854 | + |
| 855 | + $allowedSortings = array('age'); |
| 856 | + |
| 857 | + $data = DatatableFacade::run( |
| 858 | + $query, |
| 859 | + $requestParameters, |
| 860 | + allowedSortings: $allowedSortings |
| 861 | + ); |
| 862 | + |
| 863 | + $expected = $users->toArray(); |
| 864 | + array_multisort( array_column($expected, "age"), SORT_ASC, $expected); |
| 865 | + |
| 866 | + expect($data['data']) |
| 867 | + ->toEqual($expected); |
835 | 868 |
|
836 | 869 | expect($data['meta']['totalRowCount']) |
837 | 870 | ->toBe(6); |
|
0 commit comments