Skip to content

Commit d4be74e

Browse files
committed
Test updates to ensure we have coverage there too
1 parent 673befd commit d4be74e

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

packages/react-dom/src/__tests__/ReactDOMServerIntegrationUntrustedURL-test.internal.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let React;
1717
let ReactDOM;
1818
let ReactDOMServer;
1919

20-
function runTests(itRenders, itRejects) {
20+
function runTests(itRenders, itRejects, expectToReject) {
2121
itRenders('a http link with the word javascript in it', async render => {
2222
const e = await render(
2323
<a href="http://javascript:0/thisisfine">Click me</a>,
@@ -122,6 +122,14 @@ function runTests(itRenders, itRejects) {
122122
).toBe('javascript:notfine');
123123
},
124124
);
125+
126+
it('rejects a javascript protocol href if it is added during an update', () => {
127+
let container = document.createElement('div');
128+
ReactDOM.render(<a href="thisisfine">click me</a>, container);
129+
expectToReject(() => {
130+
ReactDOM.render(<a href="javascript:notfine">click me</a>, container);
131+
});
132+
});
125133
}
126134

127135
describe('ReactDOMServerIntegration - Untrusted URLs', () => {
@@ -144,7 +152,14 @@ describe('ReactDOMServerIntegration - Untrusted URLs', () => {
144152
resetModules();
145153
});
146154

147-
runTests(itRenders, itRenders);
155+
runTests(itRenders, itRenders, fn =>
156+
expect(fn).toWarnDev(
157+
'Warning: A future version of React will block javascript: URLs as a security precaution. ' +
158+
'Use event handlers instead if you can. If you need to generate unsafe HTML try using ' +
159+
'dangerouslySetInnerHTML instead.\n' +
160+
' in a (at **)',
161+
),
162+
);
148163
});
149164

150165
describe('ReactDOMServerIntegration - Untrusted URLs - disableJavaScriptURLs', () => {
@@ -174,7 +189,20 @@ describe('ReactDOMServerIntegration - Untrusted URLs - disableJavaScriptURLs', (
174189
resetModules();
175190
});
176191

177-
runTests(itRenders, (message, test) =>
178-
itThrowsWhenRendering(message, test, 'blocked a javascript: URL'),
192+
runTests(
193+
itRenders,
194+
(message, test) =>
195+
itThrowsWhenRendering(message, test, 'blocked a javascript: URL'),
196+
fn => {
197+
let msg;
198+
try {
199+
fn();
200+
} catch (x) {
201+
msg = x.message;
202+
}
203+
expect(msg).toContain(
204+
'React has blocked a javascript: URL as a security precaution.',
205+
);
206+
},
179207
);
180208
});

0 commit comments

Comments
 (0)