1+ import test from 'ava'
2+ import { assign } from '../src'
3+
4+ test ( `replace object | 1st degree` , t => {
5+ const replaced = assign ( {
6+ a : 'a'
7+ } , {
8+ a : 'b'
9+ } , undefined , 'replace' )
10+ t . true ( replaced . a == 'b' )
11+ } )
12+
13+ test ( `replace and extend object | 3rd degree` , t => {
14+ const replaced = assign ( {
15+ a : { a : {
16+ a : 'a' ,
17+ b : 'b'
18+ } } ,
19+ c : 'c'
20+ } , {
21+ a : { a : { a : 'b' } } ,
22+ b : 'b'
23+ } , undefined , 'replace' )
24+ t . true ( replaced . a . a . a == 'b' , 'assignes new value' )
25+ t . true ( replaced . a . a . b == 'b' , 'keeps previously assigned value' )
26+ t . true ( replaced . b == 'b' )
27+ t . true ( replaced . c == 'c' )
28+ } )
29+
30+ test ( `don't replace object | 1st degree` , t => {
31+ const replaced = assign ( {
32+ a : 'a'
33+ } , {
34+ b : 'b'
35+ } , undefined , 'replace' )
36+
37+ t . true ( replaced . a == 'a' )
38+ t . true ( replaced . b == 'b' )
39+ } )
40+
41+ test ( `append object | 2nd degree` , t => {
42+ const replaced = assign ( {
43+ a : { aIn : 'a' }
44+ } , {
45+ a : { bIn : 'b' }
46+ } , undefined , 'append' )
47+
48+ t . true ( replaced . a . aIn == 'a' )
49+ t . true ( replaced . a . bIn == 'b' )
50+ } )
51+
52+ test ( `append object | 3rd degree` , t => {
53+ const replaced = assign ( {
54+ a : { aIn : { aInIn : 'a' } }
55+ } , {
56+ a : { aIn : { bInIn : 'b' } }
57+ } , undefined , 'append' )
58+
59+ t . true ( replaced . a . aIn . aInIn == 'a' )
60+ t . true ( replaced . a . aIn . bInIn == 'b' )
61+ } )
62+
63+ test ( `append array | 3rd degree` , t => {
64+ const replaced = assign ( {
65+ a : { aIn : { aInIn : [ 'a' ] } }
66+ } , {
67+ a : { aIn : { aInIn : [ 'b' ] } }
68+ } , undefined , 'append' )
69+
70+ t . true ( replaced . a . aIn . aInIn [ 0 ] == 'a' )
71+ t . true ( replaced . a . aIn . aInIn [ 1 ] == 'b' )
72+ } )
73+
74+ test ( `prepend array | 3rd degree` , t => {
75+ const replaced = assign ( {
76+ a : { aIn : { aInIn : [ 'a' ] } }
77+ } , {
78+ a : { aIn : { aInIn : [ 'b' ] } }
79+ } , undefined , 'prepend' )
80+
81+ t . true ( replaced . a . aIn . aInIn [ 0 ] == 'b' )
82+ t . true ( replaced . a . aIn . aInIn [ 1 ] == 'a' )
83+ } )
84+
85+ test ( `string replace | 1st degree` , t => {
86+ const replaced = assign ( {
87+ a : 'before'
88+ } , {
89+ a : 'after'
90+ } , undefined , 'replace' )
91+
92+ t . true ( replaced . a == 'after' )
93+ } )
94+
95+ test ( `prepend combinations | multiple degrees` , t => {
96+ const replaced = assign ( {
97+ obj : { aIn : { aInIn : [ 'a' ] , bInIn : 'oldField' } } ,
98+ arr : [ 'a' ]
99+ } , {
100+ obj : { aIn : { aInIn : [ 'b' ] , bInIn : 'newField' } } ,
101+ arr : [ 'b' ]
102+ } , undefined , 'prepend' )
103+
104+ t . true ( replaced . obj . aIn . aInIn . length == 2 )
105+ t . true ( replaced . obj . aIn . aInIn [ 0 ] == 'b' )
106+ t . true ( replaced . obj . aIn . aInIn [ 1 ] == 'a' )
107+ t . true ( replaced . obj . aIn . bInIn == 'newField' , 'change non-object/non-array objects with prepend/append' )
108+ t . true ( replaced . arr . length == 2 )
109+ t . true ( replaced . arr [ 0 ] == 'b' )
110+ t . true ( replaced . arr [ 1 ] == 'a' )
111+ } )
0 commit comments