mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-09-29 05:34:21 +08:00
This adds a helper function to avoid sprinkling casts to `Platform.DevToolsPath.UrlString` all over our unit test suite, combined with an ESLint rule to enforce that we use this helper consistently. This leads to much more readable test code for unit tests that are concerned with APIs that take `UrlString`s, and the code is more consistent. Bug: 323797639 Change-Id: I6d797c0db9e5bd2fd019b83b9a7a269c7437ebdf Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6148578 Auto-Submit: Benedikt Meurer <bmeurer@chromium.org> Commit-Queue: Benedikt Meurer <bmeurer@chromium.org> Reviewed-by: Danil Somsikov <dsv@chromium.org>
85 lines
3.5 KiB
TypeScript
85 lines
3.5 KiB
TypeScript
// Copyright 2021 The Chromium Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import * as Platform from './platform.js';
|
|
|
|
describe('Platform', () => {
|
|
describe('DevToolsPath', () => {
|
|
function fnExpectingUrlString(urlString: Platform.DevToolsPath.UrlString): void {
|
|
void urlString;
|
|
}
|
|
|
|
function fnExpectingRawPathString(rawPathString: Platform.DevToolsPath.RawPathString): void {
|
|
void rawPathString;
|
|
}
|
|
|
|
function fnExpectingEncodedPathString(encPathString: Platform.DevToolsPath.EncodedPathString): void {
|
|
void encPathString;
|
|
}
|
|
|
|
describe('UrlString', () => {
|
|
it('is correctly type checked', () => {
|
|
// eslint-disable-next-line rulesdir/prefer-url-string
|
|
const urlString = 'urlStr' as Platform.DevToolsPath.UrlString;
|
|
fnExpectingUrlString(urlString);
|
|
// @ts-expect-error Passing a UrlString when RawPathString is expected
|
|
fnExpectingRawPathString(urlString);
|
|
// @ts-expect-error Passing a UrlString when EncodedPathString is expected
|
|
fnExpectingEncodedPathString(urlString);
|
|
// @ts-expect-error Passing a plain string when UrlString is expected
|
|
fnExpectingUrlString('foo');
|
|
});
|
|
});
|
|
|
|
describe('RawPathString', () => {
|
|
it('is correctly type checked', () => {
|
|
const rawPathString = 'rawPathStr' as Platform.DevToolsPath.RawPathString;
|
|
fnExpectingRawPathString(rawPathString);
|
|
// @ts-expect-error Passing a RawPathString when UrlString is expected
|
|
fnExpectingUrlString(rawPathString);
|
|
// @ts-expect-error Passing a RawPathString when EncodedPathString is expected
|
|
fnExpectingEncodedPathString(rawPathString);
|
|
// @ts-expect-error Passing a plain string when RawPathString is expected
|
|
fnExpectingRawPathString('foo');
|
|
});
|
|
});
|
|
|
|
describe('EncodedPathString', () => {
|
|
it('is correctly type checked', () => {
|
|
const encPathString = 'encPathStr' as Platform.DevToolsPath.EncodedPathString;
|
|
fnExpectingEncodedPathString(encPathString);
|
|
// @ts-expect-error Passing a EncodedPathString when UrlString is expected
|
|
fnExpectingUrlString(encPathString);
|
|
// @ts-expect-error Passing a EncodedPathString when RawPathString is expected
|
|
fnExpectingRawPathString(encPathString);
|
|
// @ts-expect-error Passing a plain string when EncodedPathString is expected
|
|
fnExpectingEncodedPathString('foo');
|
|
});
|
|
});
|
|
|
|
describe('urlString', () => {
|
|
const {urlString} = Platform.DevToolsPath;
|
|
|
|
it('acts like an identity function on URL strings', () => {
|
|
assert.strictEqual('https://www.example.com', urlString`https://www.example.com`);
|
|
assert.strictEqual('http://host/My%20File.txt', urlString`http://host/My%20File.txt`);
|
|
});
|
|
|
|
it('correctly composes URL strings', () => {
|
|
const host = 'foo.com';
|
|
const file = 'file.js';
|
|
assert.strictEqual('http://foo.com/file.js', urlString`http://${host}/${file}`);
|
|
});
|
|
|
|
it('is correctly type checked', () => {
|
|
fnExpectingUrlString(urlString`http://localhost`);
|
|
// @ts-expect-error Passing a UrlString when RawPathString is expected
|
|
fnExpectingRawPathString(urlString`http://localhost`);
|
|
// @ts-expect-error Passing a UrlString when EncodedPathString is expected
|
|
fnExpectingEncodedPathString(urlString`http://localhost`);
|
|
});
|
|
});
|
|
});
|
|
});
|