Skip to content

Commit 032208d

Browse files
EdmondChuiHWfacebook-github-bot
authored andcommitted
Update tests for URL assertions
Summary: Changelog: [Internal] Update tests to be more resilient against prod changes; and make it easier to read the actual vs expected values upon failure. Reviewed By: motiz88 Differential Revision: D53762409 fbshipit-source-id: d627d5041295f645ed00aa5d0645419a9ac4a7f8
1 parent cc147ce commit 032208d

1 file changed

Lines changed: 33 additions & 23 deletions

File tree

packages/dev-middleware/src/__tests__/getDevToolsFrontendUrl-test.js

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ describe('getDevToolsFrontendUrl', () => {
2929
webSocketDebuggerUrl,
3030
devServerUrl,
3131
);
32-
const decoded = decodeURIComponent(actual);
33-
const doubleDecoded = decodeURIComponent(decoded);
34-
expect(decoded).toBe(doubleDecoded);
35-
expect(actual).toMatchInlineSnapshot(
36-
`"http://localhost:8081/debugger-frontend/rn_inspector.html?ws=localhost%3A8081%2Finspector%2Fdebug%3Fdevice%3D1a9372c%26page%3D-1&sources.hide_add_folder=true"`,
32+
const url = new URL(actual);
33+
expect(url.host).toBe('localhost:8081');
34+
expect(url.pathname).toBe('/debugger-frontend/rn_inspector.html');
35+
expect(url.searchParams.get('ws')).toBe(
36+
'localhost:8081/inspector/debug?device=1a9372c&page=-1',
3737
);
3838
});
3939

@@ -48,17 +48,28 @@ describe('getDevToolsFrontendUrl', () => {
4848
webSocketDebuggerUrl,
4949
devServerUrl,
5050
);
51-
const decoded = decodeURIComponent(actual);
52-
const doubleDecoded = decodeURIComponent(decoded);
53-
expect(decoded).toBe(doubleDecoded);
54-
expect(actual).toMatchInlineSnapshot(
55-
`"http://localhost:8081/debugger-frontend/rn_inspector.html?ws=localhost%3A8081%2Finspector%2Fdebug%3Fdevice%3D1a9372c%26page%3D-1&sources.hide_add_folder=true&unstable_enableNetworkPanel=true"`,
51+
const url = new URL(actual);
52+
expect(url.host).toBe('localhost:8081');
53+
expect(url.pathname).toBe('/debugger-frontend/rn_inspector.html');
54+
expect(url.searchParams.get('unstable_enableNetworkPanel')).toBe('true');
55+
expect(url.searchParams.get('ws')).toBe(
56+
'localhost:8081/inspector/debug?device=1a9372c&page=-1',
5657
);
5758
});
5859
});
5960

6061
describe('given a relative devServerUrl', () => {
61-
const devServerUrl = '';
62+
const relativeDevServerUrl = '';
63+
64+
function assertValidRelativeURL(relativeURL: string): URL {
65+
const anyBaseURL = new URL('https://www.example.com');
66+
try {
67+
// By definition, a valid relative URL must be valid when combined with any base URL
68+
return new URL(relativeURL, anyBaseURL);
69+
} catch (e) {
70+
throw new Error(`Relative URL is invalid: ${relativeURL}`, {cause: e});
71+
}
72+
}
6273

6374
it('should return a valid url for all experiments off', async () => {
6475
const experiments = {
@@ -69,13 +80,12 @@ describe('getDevToolsFrontendUrl', () => {
6980
const actual = getDevToolsFrontendUrl(
7081
experiments,
7182
webSocketDebuggerUrl,
72-
devServerUrl,
83+
relativeDevServerUrl,
7384
);
74-
const decoded = decodeURIComponent(actual);
75-
const doubleDecoded = decodeURIComponent(decoded);
76-
expect(decoded).toBe(doubleDecoded);
77-
expect(actual).toMatchInlineSnapshot(
78-
`"/debugger-frontend/rn_inspector.html?ws=localhost%3A8081%2Finspector%2Fdebug%3Fdevice%3D1a9372c%26page%3D-1&sources.hide_add_folder=true"`,
85+
const url = assertValidRelativeURL(actual);
86+
expect(url.pathname).toBe('/debugger-frontend/rn_inspector.html');
87+
expect(url.searchParams.get('ws')).toBe(
88+
'localhost:8081/inspector/debug?device=1a9372c&page=-1',
7989
);
8090
});
8191

@@ -88,13 +98,13 @@ describe('getDevToolsFrontendUrl', () => {
8898
const actual = getDevToolsFrontendUrl(
8999
experiments,
90100
webSocketDebuggerUrl,
91-
devServerUrl,
101+
relativeDevServerUrl,
92102
);
93-
const decoded = decodeURIComponent(actual);
94-
const doubleDecoded = decodeURIComponent(decoded);
95-
expect(decoded).toBe(doubleDecoded);
96-
expect(actual).toMatchInlineSnapshot(
97-
`"/debugger-frontend/rn_inspector.html?ws=localhost%3A8081%2Finspector%2Fdebug%3Fdevice%3D1a9372c%26page%3D-1&sources.hide_add_folder=true&unstable_enableNetworkPanel=true"`,
103+
const url = assertValidRelativeURL(actual);
104+
expect(url.pathname).toBe('/debugger-frontend/rn_inspector.html');
105+
expect(url.searchParams.get('unstable_enableNetworkPanel')).toBe('true');
106+
expect(url.searchParams.get('ws')).toBe(
107+
'localhost:8081/inspector/debug?device=1a9372c&page=-1',
98108
);
99109
});
100110
});

0 commit comments

Comments
 (0)