66
77beforeEach (function () {
88 login ();
9+ });
10+
11+ describe ('global filters ' , function () {
12+ beforeEach (function () {
13+ sharp ()->config ()->addGlobalFilter (
14+ new class () extends GlobalRequiredFilter
15+ {
16+ public function buildFilterConfig (): void
17+ {
18+ $ this ->configureKey ('test ' );
19+ }
20+
21+ public function values (): array
22+ {
23+ return [
24+ 1 => 'One ' ,
25+ 2 => 'Two ' ,
26+ 3 => 'Three ' ,
27+ ];
28+ }
29+
30+ public function defaultValue (): mixed
31+ {
32+ return 2 ;
33+ }
34+ }
35+ );
36+ });
37+
38+ it ('allows to user to update a global filter ' , function () {
39+ $ this ->withoutExceptionHandling ();
40+
41+ $ this
42+ ->followingRedirects ()
43+ ->post (route ('code16.sharp.filters.update ' , ['2 ' ]), ['filterValues ' => ['test ' => '1 ' ]])
44+ ->assertOk ();
45+
46+ $ this ->assertEquals ('1 ' , sharp ()->context ()->globalFilterValue ('test ' ));
47+ });
48+
49+ it ('sets to global filter to the default value if missing ' , function () {
50+ $ this
51+ ->followingRedirects ()
52+ ->post (route ('code16.sharp.filters.update ' , ['2 ' ]), ['filterValues ' => []])
53+ ->assertOk ();
54+
55+ $ this ->assertEquals (2 , sharp ()->context ()->globalFilterValue ('test ' ));
56+ });
57+
58+ it ('does not allow to set a global filter to an unexpected value ' , function () {
59+ $ this
60+ ->followingRedirects ()
61+ ->post (route ('code16.sharp.filters.update ' , ['2 ' ]), ['filterValues ' => ['test ' => 4 ]])
62+ ->assertOk ();
63+
64+ $ this ->assertEquals (2 , sharp ()->context ()->globalFilterValue ('test ' ));
65+ });
66+
67+ it ('sends the current value of the global filter with every inertia request ' , function () {
68+ sharp ()->config ()->declareEntity (PersonEntity::class);
69+
70+ $ this
71+ ->followingRedirects ()
72+ ->get ('/sharp/s-list/person ' )
73+ ->assertInertia (fn (Assert $ page ) => $ page
74+ ->has ('globalFilters.config.filters._root.0 ' , fn (Assert $ filter ) => $ filter
75+ ->where ('key ' , 'test ' )
76+ ->where ('required ' , true )
77+ ->etc ()
78+ )
79+ ->where ('globalFilters.filterValues.current.test ' , 2 )
80+ ->where ('globalFilters.filterValues.default.test ' , 2 )
81+ );
82+
83+ $ this
84+ ->followingRedirects ()
85+ ->get ('/sharp/3/s-list/person ' )
86+ ->assertInertia (fn (Assert $ page ) => $ page
87+ ->has ('globalFilters.config.filters._root.0 ' , fn (Assert $ filter ) => $ filter
88+ ->where ('key ' , 'test ' )
89+ ->etc ()
90+ )
91+ ->where ('globalFilters.filterValues.current.test ' , 3 )
92+ ->where ('globalFilters.filterValues.default.test ' , 2 )
93+ );
94+ });
95+ });
96+
97+ it ('does not sends filters of none configured ' , function () {
98+ sharp ()->config ()->declareEntity (PersonEntity::class);
99+
100+ $ this
101+ ->followingRedirects ()
102+ ->get ('/sharp/s-list/person ' )
103+ ->assertInertia (fn (Assert $ page ) => $ page
104+ ->where ('globalFilters ' , null )
105+ );
106+ });
107+
108+ it ('does not sends filters of there have no values ' , function () {
109+ sharp ()->config ()->declareEntity (PersonEntity::class);
110+
111+ sharp ()->config ()->addGlobalFilter (
112+ new class () extends GlobalRequiredFilter
113+ {
114+ public function buildFilterConfig (): void
115+ {
116+ $ this ->configureKey ('test-no-values ' );
117+ }
118+
119+ public function values (): array
120+ {
121+ return [];
122+ }
123+
124+ public function defaultValue (): mixed
125+ {
126+ return null ;
127+ }
128+ }
129+ );
130+
131+ $ this
132+ ->followingRedirects ()
133+ ->get ('/sharp/s-list/person ' )
134+ ->assertInertia (fn (Assert $ page ) => $ page
135+ ->where ('globalFilters ' , null )
136+ );
9137
10138 sharp ()->config ()->addGlobalFilter (
11139 new class () extends GlobalRequiredFilter
@@ -30,62 +158,15 @@ public function defaultValue(): mixed
30158 }
31159 }
32160 );
33- });
34-
35- it ('allows to user to update a global filter ' , function () {
36- $ this ->withoutExceptionHandling ();
37-
38- $ this
39- ->followingRedirects ()
40- ->post (route ('code16.sharp.filters.update ' , ['2 ' ]), ['filterValues ' => ['test ' => '1 ' ]])
41- ->assertOk ();
42-
43- $ this ->assertEquals ('1 ' , sharp ()->context ()->globalFilterValue ('test ' ));
44- });
45-
46- it ('sets to global filter to the default value if missing ' , function () {
47- $ this
48- ->followingRedirects ()
49- ->post (route ('code16.sharp.filters.update ' , ['2 ' ]), ['filterValues ' => []])
50- ->assertOk ();
51-
52- $ this ->assertEquals (2 , sharp ()->context ()->globalFilterValue ('test ' ));
53- });
54-
55- it ('does not allow to set a global filter to an unexpected value ' , function () {
56- $ this
57- ->followingRedirects ()
58- ->post (route ('code16.sharp.filters.update ' , ['2 ' ]), ['filterValues ' => ['test ' => 4 ]])
59- ->assertOk ();
60-
61- $ this ->assertEquals (2 , sharp ()->context ()->globalFilterValue ('test ' ));
62- });
63-
64- it ('sends the current value of the global filter with every inertia request ' , function () {
65- sharp ()->config ()->declareEntity (PersonEntity::class);
66-
67- $ this
68- ->followingRedirects ()
69- ->get ('/sharp/s-list/person ' )
70- ->assertInertia (fn (Assert $ page ) => $ page
71- ->has ('globalFilters.config.filters._root.0 ' , fn (Assert $ filter ) => $ filter
72- ->where ('key ' , 'test ' )
73- ->where ('required ' , true )
74- ->etc ()
75- )
76- ->where ('globalFilters.filterValues.current.test ' , 2 )
77- ->where ('globalFilters.filterValues.default.test ' , 2 )
78- );
79161
80162 $ this
81163 ->followingRedirects ()
82164 ->get ('/sharp/3/s-list/person ' )
83165 ->assertInertia (fn (Assert $ page ) => $ page
166+ ->has ('globalFilters.config.filters._root ' , 1 )
84167 ->has ('globalFilters.config.filters._root.0 ' , fn (Assert $ filter ) => $ filter
85168 ->where ('key ' , 'test ' )
86169 ->etc ()
87170 )
88- ->where ('globalFilters.filterValues.current.test ' , 3 )
89- ->where ('globalFilters.filterValues.default.test ' , 2 )
90171 );
91172});
0 commit comments