@@ -25,48 +25,70 @@ describe('dangerouslySetInnerHTML', () => {
2525 } ) ;
2626
2727 describe ( 'when the node does not have an innerHTML property' , ( ) => {
28- let container ;
28+ let innerHTMLDescriptor ;
29+
30+ // In some versions of IE (TODO: which ones?) SVG nodes don't have
31+ // innerHTML. To simulate this, we will take it off the Element prototype
32+ // and put it onto the HTMLDivElement prototype. We expect that the logic
33+ // checks for existence of innerHTML on SVG, and if one doesn't exist, falls
34+ // back to using appendChild and removeChild.
35+
2936 beforeEach ( ( ) => {
30- // Create a mock container that looks like a svg in IE (without innerHTML)
31- container = document . createElementNS ( 'http://www.w3.org/2000/svg' , 'svg' ) ;
37+ innerHTMLDescriptor = Object . getOwnPropertyDescriptor (
38+ Element . prototype ,
39+ 'innerHTML' ,
40+ ) ;
41+ delete Element . prototype . innerHTML ;
42+ Object . defineProperty (
43+ HTMLDivElement . prototype ,
44+ 'innerHTML' ,
45+ innerHTMLDescriptor ,
46+ ) ;
47+ } ) ;
3248
33- spyOn ( container , 'appendChild' ) . and . callThrough ( ) ;
34- spyOn ( container , 'removeChild' ) . and . callThrough ( ) ;
49+ afterEach ( ( ) => {
50+ delete HTMLDivElement . prototype . innerHTML ;
51+ Object . defineProperty (
52+ Element . prototype ,
53+ 'innerHTML' ,
54+ innerHTMLDescriptor ,
55+ ) ;
3556 } ) ;
3657
3758 it ( 'sets innerHTML on it' , ( ) => {
3859 const html = '<circle></circle>' ;
39-
60+ const container = document . createElementNS (
61+ 'http://www.w3.org/2000/svg' ,
62+ 'svg' ,
63+ ) ;
4064 ReactDOM . render (
4165 < g dangerouslySetInnerHTML = { { __html : html } } /> ,
4266 container ,
4367 ) ;
44-
45- expect ( container . appendChild . calls . argsFor ( 0 ) [ 0 ] . innerHTML ) . toBe (
46- '<circle></circle>' ,
47- ) ;
68+ const circle = container . firstChild . firstChild ;
69+ expect ( circle . tagName ) . toBe ( 'circle' ) ;
4870 } ) ;
4971
5072 it ( 'clears previous children' , ( ) => {
5173 const firstHtml = '<rect></rect>' ;
5274 const secondHtml = '<circle></circle>' ;
5375
76+ const container = document . createElementNS (
77+ 'http://www.w3.org/2000/svg' ,
78+ 'svg' ,
79+ ) ;
5480 ReactDOM . render (
5581 < g dangerouslySetInnerHTML = { { __html : firstHtml } } /> ,
5682 container ,
5783 ) ;
58- ReactDOM . unmountComponentAtNode ( container ) ;
84+ const rect = container . firstChild . firstChild ;
85+ expect ( rect . tagName ) . toBe ( 'rect' ) ;
5986 ReactDOM . render (
6087 < g dangerouslySetInnerHTML = { { __html : secondHtml } } /> ,
6188 container ,
6289 ) ;
63-
64- expect ( container . removeChild . calls . argsFor ( 0 ) [ 0 ] . innerHTML ) . toBe (
65- '<rect></rect>' ,
66- ) ;
67- expect ( container . appendChild . calls . argsFor ( 1 ) [ 0 ] . innerHTML ) . toBe (
68- '<circle></circle>' ,
69- ) ;
90+ const circle = container . firstChild . firstChild ;
91+ expect ( circle . tagName ) . toBe ( 'circle' ) ;
7092 } ) ;
7193 } ) ;
7294} ) ;
0 commit comments