@@ -8,6 +8,8 @@ let scope: Scope<Author>
88beforeEach ( ( ) => {
99 const model = sinon . stub ( ) as any
1010 model . jsonapiType = "people"
11+ model . serializeKey = SpraypaintBase . serializeKey
12+ model . keyCase = { server : "snake" , client : "camel" }
1113 scope = new Scope ( model )
1214} )
1315
@@ -55,6 +57,14 @@ describe("Scope", () => {
5557 expect ( newScope ) . to . be . instanceof ( Scope )
5658 expect ( newScope ) . not . to . equal ( scope )
5759 } )
60+
61+ it ( "respects the server keycase configuration" , ( ) => {
62+ scope = scope . where ( { fooBar : "baz" } ) . where ( { fooBarBaz : "fooBar" } )
63+ expect ( ( scope as any ) . _filter ) . to . eql ( {
64+ foo_bar : "baz" ,
65+ foo_bar_baz : "fooBar"
66+ } )
67+ } )
5868 } )
5969
6070 describe ( "#stats()" , ( ) => {
@@ -72,6 +82,13 @@ describe("Scope", () => {
7282 expect ( newScope ) . to . be . instanceof ( Scope )
7383 expect ( newScope ) . not . to . equal ( scope )
7484 } )
85+
86+ it ( "respects the server keycase configuration" , ( ) => {
87+ scope = scope . stats ( { someThing : "someOtherThing" } )
88+ expect ( ( scope as any ) . _stats ) . to . eql ( {
89+ some_thing : "some_other_thing"
90+ } )
91+ } )
7592 } )
7693
7794 describe ( "#order()" , ( ) => {
@@ -88,6 +105,14 @@ describe("Scope", () => {
88105 expect ( newScope ) . to . be . instanceof ( Scope )
89106 expect ( newScope ) . not . to . equal ( scope )
90107 } )
108+
109+ it ( "respects the server keycase configuration" , ( ) => {
110+ scope = scope . order ( "fooBar" ) . order ( { fooBarBaz : "desc" } )
111+ expect ( ( scope as any ) . _sort ) . to . eql ( {
112+ foo_bar : "asc" ,
113+ foo_bar_baz : "desc"
114+ } )
115+ } )
91116 } )
92117
93118 describe ( "#select()" , ( ) => {
@@ -107,6 +132,13 @@ describe("Scope", () => {
107132 expect ( newScope ) . not . to . equal ( scope )
108133 } )
109134
135+ it ( "respects the server keycase configuration" , ( ) => {
136+ scope = scope . select ( { someThing : [ "firstThing" , "secondThing" ] } )
137+ expect ( ( scope as any ) . _fields ) . to . eql ( {
138+ some_thing : [ "first_thing" , "second_thing" ]
139+ } )
140+ } )
141+
110142 describe ( "when passed an array of strings" , ( ) => {
111143 it ( "infers the jsonapi type" , ( ) => {
112144 const newScope = scope . select ( [ "foo" ] )
@@ -134,6 +166,13 @@ describe("Scope", () => {
134166 expect ( newScope ) . not . to . equal ( scope )
135167 } )
136168
169+ it ( "respects the server keycase configuration" , ( ) => {
170+ scope = scope . selectExtra ( { someThing : [ "firstThing" , "secondThing" ] } )
171+ expect ( ( scope as any ) . _extra_fields ) . to . eql ( {
172+ some_thing : [ "first_thing" , "second_thing" ]
173+ } )
174+ } )
175+
137176 describe ( "when passed an array of strings" , ( ) => {
138177 it ( "infers the jsonapi type" , ( ) => {
139178 const newScope = scope . selectExtra ( [ "foo" ] )
@@ -152,6 +191,13 @@ describe("Scope", () => {
152191 foo : { }
153192 } )
154193 } )
194+
195+ it ( "respects the server keycase configuration" , ( ) => {
196+ scope = scope . includes ( "fooBar" )
197+ expect ( ( scope as any ) . _include ) . to . eql ( {
198+ foo_bar : { }
199+ } )
200+ } )
155201 } )
156202
157203 describe ( "when passed an array" , ( ) => {
@@ -162,6 +208,14 @@ describe("Scope", () => {
162208 bar : { }
163209 } )
164210 } )
211+
212+ it ( "respects the server keycase configuration" , ( ) => {
213+ scope = scope . includes ( [ "fooBar" , "fooBarBaz" ] )
214+ expect ( ( scope as any ) . _include ) . to . eql ( {
215+ foo_bar : { } ,
216+ foo_bar_baz : { }
217+ } )
218+ } )
165219 } )
166220
167221 describe ( "when passed a nested object" , ( ) => {
@@ -176,6 +230,20 @@ describe("Scope", () => {
176230 }
177231 } )
178232 } )
233+
234+ it ( "respects the server keycase configuration" , ( ) => {
235+ scope = scope . includes ( {
236+ fooBar : [ "fooBarBaz" , { fizzBuzz : "fizzBuzzBar" } ]
237+ } )
238+ expect ( ( scope as any ) . _include ) . to . eql ( {
239+ foo_bar : {
240+ foo_bar_baz : { } ,
241+ fizz_buzz : {
242+ fizz_buzz_bar : { }
243+ }
244+ }
245+ } )
246+ } )
179247 } )
180248
181249 it ( "returns a new scope" , ( ) => {
@@ -185,6 +253,34 @@ describe("Scope", () => {
185253 } )
186254 } )
187255
256+ describe ( "#merge()" , ( ) => {
257+ it ( "updates the scope" , ( ) => {
258+ scope = scope
259+ . includes ( [ "foo_bar" ] )
260+ . merge ( { foo_bar : scope . where ( { foo : "bar" } ) } )
261+ const qp = scope . asQueryParams ( )
262+
263+ expect ( qp . filter ) . to . eql ( {
264+ foo_bar : {
265+ foo : "bar"
266+ }
267+ } )
268+ } )
269+
270+ it ( "respects the server keycase configuration" , ( ) => {
271+ scope = scope
272+ . includes ( [ "foo_bar_baz" ] )
273+ . merge ( { fooBarBaz : scope . where ( { foo : "bar" } ) } )
274+ const qp = scope . asQueryParams ( )
275+
276+ expect ( qp . filter ) . to . eql ( {
277+ foo_bar_baz : {
278+ foo : "bar"
279+ }
280+ } )
281+ } )
282+ } )
283+
188284 describe ( "#scope()" , ( ) => {
189285 it ( "returns itself" , ( ) => {
190286 expect ( scope . scope ( ) ) . to . equal ( scope )
@@ -239,14 +335,14 @@ describe("Scope", () => {
239335 scope = scope
240336 . page ( 2 )
241337 . per ( 10 )
242- . where ( { foo : "bar " } )
338+ . where ( { fooBar : "baz " } )
243339 . order ( "foo" )
244340 . order ( { bar : "desc" } )
245341 . select ( { people : [ "name" , "age" ] } )
246342 . stats ( { total : "count" } )
247343 . includes ( { a : [ "b" , { c : "d" } ] } )
248344 expect ( scope . toQueryParams ( ) ) . to . eq (
249- "page[number]=2&page[size]=10&filter[foo]=bar &sort=foo,-bar&fields[people]=name,age&stats[total]=count&include=a.b,a.c.d"
345+ "page[number]=2&page[size]=10&filter[foo_bar]=baz &sort=foo,-bar&fields[people]=name,age&stats[total]=count&include=a.b,a.c.d"
250346 )
251347 } )
252348
0 commit comments