@@ -103,4 +103,87 @@ describe('Chart component', () => {
103
103
104
104
expect ( CHART_ACTION_OBJECT . destroy ) . toHaveBeenCalledTimes ( 1 ) ;
105
105
} ) ;
106
+
107
+ it ( 'should handle dom reference' , async ( ) => {
108
+ const { default : Chart } = await import ( '../chart.svelte' ) ;
109
+ const ref = jest . fn < HTMLElement | null , [ ] > ( ) ;
110
+ const target = document . createElement ( 'div' ) ;
111
+ const component = new Chart ( {
112
+ target,
113
+ props : {
114
+ width : 100 ,
115
+ height : 100 ,
116
+ container : {
117
+ ref,
118
+ }
119
+ } ,
120
+ } ) ;
121
+
122
+ await tick ( ) ;
123
+
124
+ expect ( ref ) . toHaveBeenCalledTimes ( 1 ) ;
125
+ expect ( ref ) . toHaveBeenLastCalledWith ( target . firstElementChild ) ;
126
+
127
+ component . $set ( {
128
+ width : 100 ,
129
+ height : 100 ,
130
+ container : {
131
+ ref,
132
+ }
133
+ } ) ;
134
+
135
+ await tick ( ) ;
136
+
137
+ expect ( ref ) . toHaveBeenCalledTimes ( 1 ) ;
138
+
139
+ const nextRef = jest . fn < HTMLElement | null , [ ] > ( ) ;
140
+ component . $set ( {
141
+ width : 100 ,
142
+ height : 100 ,
143
+ container : {
144
+ ref : nextRef ,
145
+ }
146
+ } ) ;
147
+
148
+ await tick ( ) ;
149
+
150
+ expect ( ref ) . toHaveBeenCalledTimes ( 2 ) ;
151
+ expect ( ref ) . toHaveBeenLastCalledWith ( null ) ;
152
+
153
+ expect ( nextRef ) . toHaveBeenCalledTimes ( 1 ) ;
154
+ expect ( nextRef ) . toHaveBeenLastCalledWith ( target . firstElementChild ) ;
155
+
156
+ component . $destroy ( ) ;
157
+
158
+ await tick ( ) ;
159
+
160
+ expect ( ref ) . toHaveBeenCalledTimes ( 2 ) ;
161
+
162
+ expect ( nextRef ) . toHaveBeenCalledTimes ( 2 ) ;
163
+ expect ( nextRef ) . toHaveBeenLastCalledWith ( null ) ;
164
+ } ) ;
165
+
166
+ it ( 'should spread attributes to dom container' , async ( ) => {
167
+ const { default : Chart } = await import ( '../chart.svelte' ) ;
168
+
169
+ const target = document . createElement ( 'div' )
170
+ new Chart ( {
171
+ target,
172
+ props : {
173
+ width : 100 ,
174
+ height : 100 ,
175
+ container : {
176
+ class : 'container' ,
177
+ id : 'container' ,
178
+ 'data-test' : 'container' ,
179
+ }
180
+ } ,
181
+ } ) ;
182
+
183
+ await tick ( ) ;
184
+
185
+ expect ( target . firstElementChild . className ) . toBe ( 'container' ) ;
186
+ expect ( target . firstElementChild . id ) . toBe ( 'container' ) ;
187
+ expect ( target . firstElementChild . getAttribute ( 'data-test' ) ) . toBe ( 'container' ) ;
188
+ } ) ;
106
189
} )
0 commit comments