Files
outline/app/utils/polyfills.test.ts
Tom Moor a004b24e83 chore: Upgrade slug from 5 to 11 (#14038)
* chore: Upgrade slug from 5 to 11

* Require Node 22.12+ for require(esm), test θ transliteration

* Polyfill String isWellFormed and toWellFormed for older browsers
2026-10-11 16:13:48 -04:00

61 lines
1.6 KiB
TypeScript

const descriptor = (name: "toWellFormed" | "isWellFormed") =>
Object.getOwnPropertyDescriptor(String.prototype, name);
const nativeToWellFormed = descriptor("toWellFormed");
const nativeIsWellFormed = descriptor("isWellFormed");
const inputs = [
"",
"plain",
"emoji 🎉 test",
"x\uD800y",
"x\uDC00y",
"\uDC00\uDC00",
"\uD800𐀀",
"end\uD83D",
"\uDE00start",
];
describe("polyfills", () => {
beforeAll(async () => {
// @ts-expect-error Removing native methods to exercise the polyfills.
delete String.prototype.toWellFormed;
// @ts-expect-error Removing native methods to exercise the polyfills.
delete String.prototype.isWellFormed;
await import("./polyfills");
});
afterAll(() => {
if (nativeToWellFormed && nativeIsWellFormed) {
Object.defineProperty(
String.prototype,
"toWellFormed",
nativeToWellFormed
);
Object.defineProperty(
String.prototype,
"isWellFormed",
nativeIsWellFormed
);
}
});
it("should polyfill String.prototype.toWellFormed", () => {
expect(descriptor("toWellFormed")?.value).not.toBe(
nativeToWellFormed?.value
);
for (const input of inputs) {
expect(input.toWellFormed()).toBe(nativeToWellFormed?.value.call(input));
}
});
it("should polyfill String.prototype.isWellFormed", () => {
expect(descriptor("isWellFormed")?.value).not.toBe(
nativeIsWellFormed?.value
);
for (const input of inputs) {
expect(input.isWellFormed()).toBe(nativeIsWellFormed?.value.call(input));
}
});
});