33 getField ,
44 mapFields ,
55 mapMultiRowFields ,
6+ mapObjectFields ,
67 updateField ,
78} from './' ;
89
@@ -199,6 +200,128 @@ describe(`index`, () => {
199200 } ) ;
200201 } ) ;
201202
203+ describe ( `mapObjectFields()` , ( ) => {
204+ test ( `It should be possible to re-map the initial path.` , ( ) => {
205+ const expectedResult = {
206+ otherFieldRows : { get : expect . any ( Function ) } ,
207+ } ;
208+
209+ expect ( mapObjectFields ( { otherFieldRows : `*` } ) ) . toEqual ( expectedResult ) ;
210+ } ) ;
211+
212+ test ( `It should do nothing if path doesn't exist.` , ( ) => {
213+ const mockGetField = jest . fn ( ) . mockReturnValue ( undefined ) ;
214+ const mappedObjectFields = mapObjectFields ( { objProps : `obj.*` } ) ;
215+
216+ const getterSetters = mappedObjectFields . objProps . get . apply ( {
217+ $store : { getters : { getField : mockGetField } } ,
218+ } ) ;
219+
220+ // eslint-disable-next-line no-unused-vars
221+ const x = getterSetters ; // Trigger getter function.
222+ } ) ;
223+
224+ test ( `It should get the value of a top-level property via the \`getField()\` function.` , ( ) => {
225+ const mockObjectField = {
226+ foo : `Foo` ,
227+ bar : `Bar` ,
228+ } ;
229+ const mockGetField = jest . fn ( ) . mockReturnValue ( mockObjectField ) ;
230+ mockGetField . mockReturnValueOnce ( mockObjectField ) ;
231+
232+ const mappedObjectFields = mapObjectFields ( { objProps : `*` } ) ;
233+
234+ const getterSetters = mappedObjectFields . objProps . get . apply ( {
235+ $store : { getters : { getField : mockGetField } } ,
236+ } ) ;
237+
238+ // eslint-disable-next-line no-unused-vars
239+ const x = getterSetters . foo ; // Trigger getter function.
240+ expect ( mockGetField ) . lastCalledWith ( `foo` ) ;
241+
242+ // eslint-disable-next-line no-unused-vars
243+ const y = getterSetters . bar ; // Trigger getter function.
244+ expect ( mockGetField ) . lastCalledWith ( `bar` ) ;
245+ } ) ;
246+
247+ test ( `It should get the value of nested property via the \`getField()\` function.` , ( ) => {
248+ const mockObjectField = {
249+ obj : {
250+ foo : `Foo` ,
251+ bar : `Bar` ,
252+ } ,
253+ } ;
254+ const mockGetField = jest . fn ( ) . mockReturnValue ( mockObjectField ) ;
255+ mockGetField . mockReturnValueOnce ( mockObjectField . obj ) ;
256+
257+ const mappedObjectFields = mapObjectFields ( { objProps : `obj.*` } ) ;
258+
259+ const getterSetters = mappedObjectFields . objProps . get . apply ( {
260+ $store : { getters : { getField : mockGetField } } ,
261+ } ) ;
262+
263+ // eslint-disable-next-line no-unused-vars
264+ const x = getterSetters . foo ; // Trigger getter function.
265+ expect ( mockGetField ) . lastCalledWith ( `obj.foo` ) ;
266+
267+ // eslint-disable-next-line no-unused-vars
268+ const y = getterSetters . bar ; // Trigger getter function.
269+ expect ( mockGetField ) . lastCalledWith ( `obj.bar` ) ;
270+ } ) ;
271+
272+ test ( `It should commit new values to the store (top).` , ( ) => {
273+ const mockObjectField = {
274+ foo : `Foo` ,
275+ bar : `Bar` ,
276+ } ;
277+ const mockCommit = jest . fn ( ) ;
278+ const mockGetField = jest . fn ( ) . mockReturnValue ( mockObjectField ) ;
279+ mockGetField . mockReturnValueOnce ( mockObjectField ) ;
280+
281+ const mappedObjectFields = mapObjectFields ( { objProps : `*` } ) ;
282+
283+ const getterSetters = mappedObjectFields . objProps . get . apply ( {
284+ $store : {
285+ getters : { getField : mockGetField } ,
286+ commit : mockCommit ,
287+ } ,
288+ } ) ;
289+
290+ getterSetters . bar = `New Bar` ; // Trigger setter function.
291+ expect ( mockCommit ) . toBeCalledWith ( `updateField` , { path : `bar` , value : `New Bar` } ) ;
292+
293+ getterSetters . foo = `New Foo` ; // Trigger setter function.
294+ expect ( mockCommit ) . toBeCalledWith ( `updateField` , { path : `foo` , value : `New Foo` } ) ;
295+ } ) ;
296+
297+ test ( `It should commit new values to the store (nested).` , ( ) => {
298+ const mockObjectField = {
299+ obj : {
300+ foo : `Foo` ,
301+ bar : `Bar` ,
302+ } ,
303+ } ;
304+ const mockCommit = jest . fn ( ) ;
305+ const mockGetField = jest . fn ( ) . mockReturnValue ( mockObjectField ) ;
306+ mockGetField . mockReturnValueOnce ( mockObjectField . obj ) ;
307+
308+ const mappedObjectFields = mapObjectFields ( { objProps : `obj.*` } ) ;
309+
310+ const getterSetters = mappedObjectFields . objProps . get . apply ( {
311+ $store : {
312+ getters : { getField : mockGetField } ,
313+ commit : mockCommit ,
314+ } ,
315+ } ) ;
316+
317+ getterSetters . bar = `New Bar` ; // Trigger setter function.
318+ expect ( mockCommit ) . toBeCalledWith ( `updateField` , { path : `obj.bar` , value : `New Bar` } ) ;
319+
320+ getterSetters . foo = `New Foo` ; // Trigger setter function.
321+ expect ( mockCommit ) . toBeCalledWith ( `updateField` , { path : `obj.foo` , value : `New Foo` } ) ;
322+ } ) ;
323+ } ) ;
324+
202325 describe ( `createHelpers()` , ( ) => {
203326 test ( `It should be a function.` , ( ) => {
204327 expect ( typeof createHelpers ) . toBe ( `function` ) ;
@@ -211,6 +334,7 @@ describe(`index`, () => {
211334 expect ( typeof helpers . updateFoo ) . toBe ( `function` ) ;
212335 expect ( typeof helpers . mapFields ) . toBe ( `function` ) ;
213336 expect ( typeof helpers . mapMultiRowFields ) . toBe ( `function` ) ;
337+ expect ( typeof helpers . mapObjectFields ) . toBe ( `function` ) ;
214338 } ) ;
215339
216340 test ( `It should call the \`mapFields()\` function with the provided getter and mutation types.` , ( ) => {
@@ -235,5 +359,16 @@ describe(`index`, () => {
235359
236360 expect ( helpers . mapMultiRowFields ( [ `foo` ] ) ) . toEqual ( expectedResult ) ;
237361 } ) ;
362+
363+ test ( `It should call the \`mapObjectFields()\` function with the provided getter and mutation types.` , ( ) => {
364+ const helpers = createHelpers ( { getterType : `getFoo` , mutationType : `updateFoo` } ) ;
365+ const expectedResult = {
366+ foo : {
367+ get : expect . any ( Function ) ,
368+ } ,
369+ } ;
370+
371+ expect ( helpers . mapObjectFields ( { foo : `foo` } ) ) . toEqual ( expectedResult ) ;
372+ } ) ;
238373 } ) ;
239374} ) ;
0 commit comments