33 getField ,
44 mapFields ,
55 mapMultiRowFields ,
6+ mapObjectFields ,
67 updateField ,
78} from './' ;
89
@@ -199,6 +200,119 @@ 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+ const x = getterSetters ; // Trigger getter function.
221+ } ) ;
222+
223+ test ( `It should get the value of a top-level property via the \`getField()\` function.` , ( ) => {
224+ const mockObjectField = {
225+ foo : `Foo` ,
226+ bar : `Bar` ,
227+ } ;
228+ const mockGetField = jest . fn ( ) . mockReturnValue ( mockObjectField ) . mockReturnValueOnce ( mockObjectField ) ;
229+ const mappedObjectFields = mapObjectFields ( { objProps : `*` } ) ;
230+
231+ const getterSetters = mappedObjectFields . objProps . get . apply ( {
232+ $store : { getters : { getField : mockGetField } } ,
233+ } ) ;
234+
235+ // eslint-disable-next-line no-unused-vars
236+ const x = getterSetters . foo ; // Trigger getter function.
237+ expect ( mockGetField ) . lastCalledWith ( `foo` ) ;
238+
239+ // eslint-disable-next-line no-unused-vars
240+ const y = getterSetters . bar ; // Trigger getter function.
241+ expect ( mockGetField ) . lastCalledWith ( `bar` ) ;
242+ } ) ;
243+
244+ test ( `It should get the value of nested property via the \`getField()\` function.` , ( ) => {
245+ const mockObjectField = {
246+ obj : {
247+ foo : `Foo` ,
248+ bar : `Bar` ,
249+ } ,
250+ } ;
251+ const mockGetField = jest . fn ( ) . mockReturnValue ( mockObjectField ) . mockReturnValueOnce ( mockObjectField . obj ) ;
252+ const mappedObjectFields = mapObjectFields ( { objProps : `obj.*` } ) ;
253+
254+ const getterSetters = mappedObjectFields . objProps . get . apply ( {
255+ $store : { getters : { getField : mockGetField } } ,
256+ } ) ;
257+
258+ // eslint-disable-next-line no-unused-vars
259+ const x = getterSetters . foo ; // Trigger getter function.
260+ expect ( mockGetField ) . lastCalledWith ( `obj.foo` ) ;
261+
262+ // eslint-disable-next-line no-unused-vars
263+ const y = getterSetters . bar ; // Trigger getter function.
264+ expect ( mockGetField ) . lastCalledWith ( `obj.bar` ) ;
265+ } ) ;
266+
267+ test ( `It should commit new values to the store (top).` , ( ) => {
268+ const mockObjectField = {
269+ foo : `Foo` ,
270+ bar : `Bar` ,
271+ } ;
272+ const mockCommit = jest . fn ( ) ;
273+ const mockGetField = jest . fn ( ) . mockReturnValue ( mockObjectField ) . mockReturnValueOnce ( mockObjectField ) ;
274+ const mappedObjectFields = mapObjectFields ( { objProps : `*` } ) ;
275+
276+ const getterSetters = mappedObjectFields . objProps . get . apply ( {
277+ $store : {
278+ getters : { getField : mockGetField } ,
279+ commit : mockCommit ,
280+ } ,
281+ } ) ;
282+
283+ getterSetters . bar = `New Bar` ; // Trigger setter function.
284+ expect ( mockCommit ) . toBeCalledWith ( `updateField` , { path : `bar` , value : `New Bar` } ) ;
285+
286+ getterSetters . foo = `New Foo` ; // Trigger setter function.
287+ expect ( mockCommit ) . toBeCalledWith ( `updateField` , { path : `foo` , value : `New Foo` } ) ;
288+ } ) ;
289+
290+ test ( `It should commit new values to the store (nested).` , ( ) => {
291+ const mockObjectField = {
292+ obj : {
293+ foo : `Foo` ,
294+ bar : `Bar` ,
295+ }
296+ } ;
297+ const mockCommit = jest . fn ( ) ;
298+ const mockGetField = jest . fn ( ) . mockReturnValue ( mockObjectField ) . mockReturnValueOnce ( mockObjectField . obj ) ;
299+ const mappedObjectFields = mapObjectFields ( { objProps : `obj.*` } ) ;
300+
301+ const getterSetters = mappedObjectFields . objProps . get . apply ( {
302+ $store : {
303+ getters : { getField : mockGetField } ,
304+ commit : mockCommit ,
305+ } ,
306+ } ) ;
307+
308+ getterSetters . bar = `New Bar` ; // Trigger setter function.
309+ expect ( mockCommit ) . toBeCalledWith ( `updateField` , { path : `obj.bar` , value : `New Bar` } ) ;
310+
311+ getterSetters . foo = `New Foo` ; // Trigger setter function.
312+ expect ( mockCommit ) . toBeCalledWith ( `updateField` , { path : `obj.foo` , value : `New Foo` } ) ;
313+ } ) ;
314+ } ) ;
315+
202316 describe ( `createHelpers()` , ( ) => {
203317 test ( `It should be a function.` , ( ) => {
204318 expect ( typeof createHelpers ) . toBe ( `function` ) ;
@@ -211,6 +325,7 @@ describe(`index`, () => {
211325 expect ( typeof helpers . updateFoo ) . toBe ( `function` ) ;
212326 expect ( typeof helpers . mapFields ) . toBe ( `function` ) ;
213327 expect ( typeof helpers . mapMultiRowFields ) . toBe ( `function` ) ;
328+ expect ( typeof helpers . mapObjectFields ) . toBe ( `function` ) ;
214329 } ) ;
215330
216331 test ( `It should call the \`mapFields()\` function with the provided getter and mutation types.` , ( ) => {
@@ -235,5 +350,16 @@ describe(`index`, () => {
235350
236351 expect ( helpers . mapMultiRowFields ( [ `foo` ] ) ) . toEqual ( expectedResult ) ;
237352 } ) ;
353+
354+ test ( `It should call the \`mapObjectFields()\` function with the provided getter and mutation types.` , ( ) => {
355+ const helpers = createHelpers ( { getterType : `getFoo` , mutationType : `updateFoo` } ) ;
356+ const expectedResult = {
357+ foo : {
358+ get : expect . any ( Function ) ,
359+ } ,
360+ } ;
361+
362+ expect ( helpers . mapObjectFields ( { foo :`foo` } ) ) . toEqual ( expectedResult ) ;
363+ } ) ;
238364 } ) ;
239365} ) ;
0 commit comments