1
+ import * as utils from 'renderer/utils'
2
+
3
+ jest . mock ( 'ui/core/view' , ( ) => {
4
+ return {
5
+ View ( ) {
6
+ }
7
+ }
8
+ } , { virtual : true } )
9
+ jest . mock ( 'ui/content-view' , ( ) => {
10
+ return {
11
+ ContentView ( ) {
12
+ }
13
+ }
14
+ } , { virtual : true } )
15
+ jest . mock ( 'ui/layouts/layout-base' , ( ) => {
16
+ return {
17
+ LayoutBase ( ) {
18
+ }
19
+ }
20
+ } , { virtual : true } )
21
+
22
+ const getParentAndChild = ( parentType ) => {
23
+ return {
24
+ parentNode : {
25
+ nativeView : parentType ? new parentType : { } ,
26
+ meta : { }
27
+ } ,
28
+ childNode : {
29
+ nativeView : { } ,
30
+ meta : { }
31
+ }
32
+ }
33
+ }
34
+
35
+ describe ( 'utils' , ( ) => {
36
+ test ( 'isView' , ( ) => {
37
+ const View = require ( 'ui/core/view' ) . View ;
38
+
39
+ expect ( utils . isView ( ) ) . toEqual ( false )
40
+ expect ( utils . isView ( 'a' ) ) . toEqual ( false )
41
+ expect ( utils . isView ( 1 ) ) . toEqual ( false )
42
+ expect ( utils . isView ( new View ( ) ) ) . toEqual ( true )
43
+ } )
44
+
45
+ test ( 'isLayout' , ( ) => {
46
+ const LayoutBase = require ( 'ui/layouts/layout-base' ) . LayoutBase ;
47
+
48
+ expect ( utils . isLayout ( ) ) . toEqual ( false )
49
+ expect ( utils . isLayout ( 'a' ) ) . toEqual ( false )
50
+ expect ( utils . isLayout ( 1 ) ) . toEqual ( false )
51
+ expect ( utils . isLayout ( new LayoutBase ( ) ) ) . toEqual ( true )
52
+ } )
53
+
54
+ test ( 'isContentView' , ( ) => {
55
+ const ContentView = require ( 'ui/content-view' ) . ContentView ;
56
+
57
+ expect ( utils . isContentView ( ) ) . toEqual ( false )
58
+ expect ( utils . isContentView ( 'a' ) ) . toEqual ( false )
59
+ expect ( utils . isContentView ( 1 ) ) . toEqual ( false )
60
+ expect ( utils . isContentView ( new ContentView ( ) ) ) . toEqual ( true )
61
+ } )
62
+
63
+ test ( 'insertChild returns early if there is no parent or child should not be added to dom' , ( ) => {
64
+ expect ( utils . insertChild ( ) ) . toBeFalsy ( ) ;
65
+ expect ( utils . insertChild ( true , { meta : { skipAddToDom : true } } ) ) . toBeFalsy ( ) ;
66
+ } )
67
+
68
+ test ( 'insertChild skips adding childNode to unknown parent' , ( ) => {
69
+ const { parentNode, childNode} = getParentAndChild ( ) ;
70
+
71
+ expect ( utils . insertChild ( parentNode , childNode ) ) . toBeFalsy ( )
72
+ } )
73
+
74
+
75
+ test ( 'insertChild adds childNode to Layout parent' , ( ) => {
76
+ const LayoutBase = require ( 'ui/layouts/layout-base' ) . LayoutBase ;
77
+ const { parentNode, childNode} = getParentAndChild ( LayoutBase ) ;
78
+ parentNode . nativeView . addChild = jest . fn ( ) ;
79
+ childNode . nativeView . parent = null ;
80
+
81
+ utils . insertChild ( parentNode , childNode ) ;
82
+ expect ( parentNode . nativeView . addChild . mock . calls . length ) . toBe ( 1 )
83
+ } )
84
+
85
+
86
+ test ( 'insertChild adds childNode at index to Layout parent' , ( ) => {
87
+ const LayoutBase = require ( 'ui/layouts/layout-base' ) . LayoutBase ;
88
+ const { parentNode, childNode} = getParentAndChild ( LayoutBase ) ;
89
+ parentNode . nativeView . insertChild = jest . fn ( ) ;
90
+ childNode . nativeView . parent = null ;
91
+
92
+ utils . insertChild ( parentNode , childNode , 1 ) ;
93
+ expect ( parentNode . nativeView . insertChild . mock . calls . length ) . toBe ( 1 )
94
+ } )
95
+
96
+ test ( 'insertChild removes childNode if the parent is the same Layout parent' , ( ) => {
97
+ const LayoutBase = require ( 'ui/layouts/layout-base' ) . LayoutBase ;
98
+ const { parentNode, childNode} = getParentAndChild ( LayoutBase ) ;
99
+ parentNode . nativeView . getChildIndex = jest . fn ( ) . mockReturnValueOnce ( 1 ) . mockReturnValueOnce ( - 1 ) ;
100
+ parentNode . nativeView . removeChild = jest . fn ( ) ;
101
+ parentNode . nativeView . insertChild = jest . fn ( ) ;
102
+ parentNode . nativeView . addChild = jest . fn ( ) ;
103
+ childNode . nativeView . parent = parentNode . nativeView ;
104
+
105
+ utils . insertChild ( parentNode , childNode ) ;
106
+ expect ( parentNode . nativeView . getChildIndex . mock . calls . length ) . toBe ( 1 )
107
+ expect ( parentNode . nativeView . removeChild . mock . calls . length ) . toBe ( 1 )
108
+
109
+ //
110
+ utils . insertChild ( parentNode , childNode ) ;
111
+ expect ( parentNode . nativeView . getChildIndex . mock . calls . length ) . toBe ( 2 )
112
+ } )
113
+
114
+ test ( 'insertChild adds comment node to ContentView parent' , ( ) => {
115
+ const ContentView = require ( 'ui/content-view' ) . ContentView ;
116
+ const { parentNode, childNode} = getParentAndChild ( ContentView ) ;
117
+ childNode . nodeType = 8 ;
118
+ parentNode . nativeView . _addView = jest . fn ( ) ;
119
+
120
+ utils . insertChild ( parentNode , childNode ) ;
121
+ expect ( parentNode . nativeView . _addView . mock . calls . length ) . toBe ( 1 ) ;
122
+ } )
123
+
124
+ test ( 'insertChild sets content of ContentView parent' , ( ) => {
125
+ const ContentView = require ( 'ui/content-view' ) . ContentView ;
126
+ const { parentNode, childNode} = getParentAndChild ( ContentView ) ;
127
+
128
+ utils . insertChild ( parentNode , childNode ) ;
129
+ expect ( parentNode . nativeView . content ) . toEqual ( childNode . nativeView ) ;
130
+ } )
131
+
132
+ test ( 'insertChild adds child from builder to unknown parent' , ( ) => {
133
+ const { parentNode, childNode} = getParentAndChild ( ) ;
134
+ parentNode . nativeView . _addChildFromBuilder = jest . fn ( ) ;
135
+ childNode . _nativeView = {
136
+ constructor : { name : 'test' }
137
+ } ;
138
+
139
+ utils . insertChild ( parentNode , childNode ) ;
140
+ expect ( parentNode . nativeView . _addChildFromBuilder . mock . calls . length ) . toBe ( 1 ) ;
141
+ expect ( parentNode . nativeView . _addChildFromBuilder . mock . calls [ 0 ] [ 0 ] ) . toEqual ( 'test' )
142
+ } )
143
+ } )
0 commit comments