@@ -4,13 +4,33 @@ console.warn = jest.fn();
44jest . mock ( "./processConfig" , ( ) => jest . fn ( config => config ) ) ;
55jest . mock ( "./adjustContainer" , ( ) => jest . fn ( ) ) ;
66jest . mock ( "raf" , ( ) => jest . fn ( cb => cb ( ) ) ) ;
7+ jest . mock ( "es6-weak-map" , ( ) => {
8+ const mock = jest . fn ( ) ;
9+
10+ mock . prototype . set = jest . fn ( ) ;
11+ mock . prototype . get = jest . fn ( ) ;
12+ mock . prototype . has = jest . fn ( ) ;
13+ mock . prototype . delete = jest . fn ( ) ;
14+
15+ return mock ;
16+ } ) ;
717
818jest . mock ( "resize-observer-polyfill" , ( ) => {
919 const mock = jest . fn ( cb => {
10- mock . triggerResizeEvent = cb ;
20+ mock . triggerEvent = cb ;
21+ } ) ;
22+
23+ mock . prototype . observe = jest . fn ( ) ;
24+ mock . prototype . unobserve = jest . fn ( ) ;
25+
26+ return mock ;
27+ } ) ;
28+
29+ jest . mock ( "mutation-observer" , ( ) => {
30+ const mock = jest . fn ( cb => {
31+ mock . triggerEvent = cb ;
1132 } ) ;
1233
13- // mock.default = mock;
1434 mock . prototype . observe = jest . fn ( ) ;
1535 mock . prototype . unobserve = jest . fn ( ) ;
1636
@@ -28,7 +48,10 @@ test("should instantiate properly", () => {
2848 const adjustContainer = require ( "./adjustContainer" ) ;
2949 const raf = require ( "raf" ) ;
3050
31- const containerElement = { } ;
51+ const containerElement = {
52+ parentNode : document . createElement ( "div" )
53+ } ;
54+
3255 const config = { } ;
3356 const processedConfig = { } ;
3457
@@ -54,7 +77,9 @@ test("should be able to observe resize events and switch off initial adjust call
5477 const raf = require ( "raf" ) ;
5578 const adjustContainer = require ( "./adjustContainer" ) ;
5679
57- const containerElement = { } ;
80+ const containerElement = {
81+ parentNode : document . createElement ( "div" )
82+ } ;
5883 const config = { } ;
5984
6085 const containerInstance = new Container ( containerElement , config , {
@@ -85,19 +110,34 @@ test("should be able to observe resize events and switch off initial adjust call
85110} ) ;
86111
87112test ( "should call adjust() on resize changes" , ( ) => {
113+ const WeakMap = require ( "es6-weak-map" ) ;
114+ WeakMap . prototype . set = jest . fn ( ) ;
115+ WeakMap . prototype . get . mockImplementationOnce ( ( ) => undefined ) ;
116+ WeakMap . prototype . get . mockImplementationOnce ( element => {
117+ expect ( element ) . toBe ( containerElement ) ;
118+
119+ return containerInstance ;
120+ } ) ;
88121 const ResizeObserver = require ( "resize-observer-polyfill" ) ;
89- const containerElement = "<ContainerElement>" ;
122+ const parentElement = document . createElement ( "div" ) ;
123+ const containerElement = document . createElement ( "div" ) ;
124+ parentElement . appendChild ( containerElement ) ;
90125 const config = { } ;
91126 const containerInstance = new Container ( containerElement , config , {
92127 adjustOnInstantiation : false ,
93128 adjustOnResize : true
94129 } ) ;
130+ expect ( WeakMap . prototype . set ) . toHaveBeenCalledTimes ( 1 ) ;
131+ expect ( WeakMap . prototype . set ) . toHaveBeenCalledWith (
132+ containerElement ,
133+ containerInstance
134+ ) ;
95135
96136 expect ( ResizeObserver ) . toHaveBeenCalledTimes ( 1 ) ;
97- expect ( typeof ResizeObserver . triggerResizeEvent ) . toBe ( "function" ) ;
98- expect ( ( ) => ResizeObserver . triggerResizeEvent ( ) ) . not . toThrow ( ) ;
137+ expect ( typeof ResizeObserver . triggerEvent ) . toBe ( "function" ) ;
138+ expect ( ( ) => ResizeObserver . triggerEvent ( ) ) . not . toThrow ( ) ;
99139 expect ( ( ) => {
100- ResizeObserver . triggerResizeEvent ( [
140+ ResizeObserver . triggerEvent ( [
101141 {
102142 target : "<HTMLElement>"
103143 }
@@ -107,7 +147,7 @@ test("should call adjust() on resize changes", () => {
107147
108148 containerInstance . adjust = jest . fn ( ) ;
109149 expect ( ( ) => {
110- ResizeObserver . triggerResizeEvent ( [
150+ ResizeObserver . triggerEvent ( [
111151 {
112152 target : containerElement ,
113153 contentRect : {
@@ -118,9 +158,62 @@ test("should call adjust() on resize changes", () => {
118158 ] ) ;
119159 } ) . not . toThrow ( ) ;
120160 expect ( console . warn ) . toHaveBeenCalledTimes ( 1 ) ;
161+ expect ( WeakMap . prototype . get ) . toHaveBeenCalledTimes ( 2 ) ;
121162 expect ( containerInstance . adjust ) . toHaveBeenCalledTimes ( 1 ) ;
122163 expect ( containerInstance . adjust ) . toHaveBeenCalledWith ( {
123164 width : 1 ,
124165 height : 2
125166 } ) ;
126167} ) ;
168+
169+ test ( "should clean up after container element is detached from the DOM" , ( ) => {
170+ const WeakMap = require ( "es6-weak-map" ) ;
171+ WeakMap . prototype . set = jest . fn ( ) ;
172+ WeakMap . prototype . has = jest . fn ( ( ) => true ) ;
173+ const MutationObserver = require ( "mutation-observer" ) ;
174+ const ResizeObserver = require ( "resize-observer-polyfill" ) ;
175+ ResizeObserver . prototype . unobserve = jest . fn ( ) ;
176+ const parentElement = document . createElement ( "div" ) ;
177+ const containerElement = document . createElement ( "div" ) ;
178+ parentElement . appendChild ( containerElement ) ;
179+ const config = { } ;
180+ const containerInstance = new Container ( containerElement , config , {
181+ adjustOnInstantiation : false ,
182+ adjustOnResize : false
183+ } ) ;
184+ expect ( WeakMap . prototype . set ) . toHaveBeenCalledTimes ( 1 ) ;
185+ expect ( WeakMap . prototype . set ) . toHaveBeenCalledWith (
186+ containerElement ,
187+ containerInstance
188+ ) ;
189+
190+ let mutationRecords = [
191+ {
192+ removedNodes : [ containerElement ]
193+ }
194+ ] ;
195+
196+ MutationObserver . triggerEvent ( mutationRecords ) ;
197+
198+ expect ( WeakMap . prototype . has ) . toHaveBeenCalledTimes ( 1 ) ;
199+ expect ( WeakMap . prototype . delete ) . toHaveBeenCalledTimes ( 1 ) ;
200+ expect ( ResizeObserver . prototype . unobserve ) . toHaveBeenCalledTimes ( 1 ) ;
201+ expect ( WeakMap . prototype . delete ) . toHaveBeenCalledWith ( containerElement ) ;
202+ expect ( ResizeObserver . prototype . unobserve ) . toHaveBeenCalledWith (
203+ containerElement
204+ ) ;
205+
206+ // Should not clean up after non-container elements
207+ mutationRecords = [
208+ {
209+ removedNodes : [ document . createElement ( "div" ) ]
210+ }
211+ ] ;
212+
213+ WeakMap . prototype . has = jest . fn ( ( ) => false ) ;
214+ MutationObserver . triggerEvent ( mutationRecords ) ;
215+
216+ expect ( WeakMap . prototype . has ) . toHaveBeenCalledTimes ( 1 ) ;
217+ expect ( WeakMap . prototype . delete ) . toHaveBeenCalledTimes ( 1 ) ;
218+ expect ( ResizeObserver . prototype . unobserve ) . toHaveBeenCalledTimes ( 1 ) ;
219+ } ) ;
0 commit comments