test: refactor existing tests to vitest

This commit is contained in:
William Will 2023-03-30 02:46:25 -06:00
parent e4725c23eb
commit 58bb403787
9 changed files with 367 additions and 392 deletions

View File

@ -1 +0,0 @@
import "./replaceImportFromStatic";

View File

@ -1,4 +1,3 @@
import "./replaceImportFromStatic";
import { setupSampleReactProject, sampleReactProjectDirPath } from "./setupSampleReactProject";
import * as st from "scripting-tools";
import * as fs from "fs";

View File

@ -1,11 +1,12 @@
import { replaceImportsFromStaticInJsCode } from "keycloakify/bin/keycloakify/replacers/replaceImportsFromStaticInJsCode";
import { generateCssCodeToDefineGlobals, replaceImportsInCssCode } from "keycloakify/bin/keycloakify/replacers/replaceImportsInCssCode";
import { replaceImportsInInlineCssCode } from "keycloakify/bin/keycloakify/replacers/replaceImportsInInlineCssCode";
import { assert } from "tsafe/assert";
import { same } from "evt/tools/inDepth/same";
import { assetIsSameCode } from "../tools/assertIsSameCode";
import { expect, it, describe } from "vitest";
{
import { isSameCode } from "../tools/isSameCode";
describe("bin/js-transforms", () => {
const jsCodeUntransformed = `
function f() {
return a.p+"static/js/" + ({}[e] || e) + "." + {
@ -32,8 +33,7 @@ import { assetIsSameCode } from "../tools/assertIsSameCode";
}[e]+".chunk.css"
}
`;
{
it("transforms standalone code properly", () => {
const { fixedJsCode } = replaceImportsFromStaticInJsCode({
"jsCode": jsCodeUntransformed,
"buildOptions": {
@ -89,10 +89,9 @@ import { assetIsSameCode } from "../tools/assertIsSameCode";
`;
assetIsSameCode(fixedJsCode, fixedJsCodeExpected);
}
{
expect(isSameCode(fixedJsCode, fixedJsCodeExpected)).toBe(true);
});
it("transforms external app code properly", () => {
const { fixedJsCode } = replaceImportsFromStaticInJsCode({
"jsCode": jsCodeUntransformed,
"buildOptions": {
@ -150,126 +149,128 @@ import { assetIsSameCode } from "../tools/assertIsSameCode";
}
`;
assetIsSameCode(fixedJsCode, fixedJsCodeExpected);
}
}
expect(isSameCode(fixedJsCode, fixedJsCodeExpected)).toBe(true);
});
});
{
const { fixedCssCode, cssGlobalsToDefine } = replaceImportsInCssCode({
"cssCode": `
describe("bin/css-transforms", () => {
it("transforms absolute urls to css globals properly with no urlPathname", () => {
const { fixedCssCode, cssGlobalsToDefine } = replaceImportsInCssCode({
"cssCode": `
.my-div {
background: url(/logo192.png) no-repeat center center;
}
.my-div2 {
background: url(/logo192.png) no-repeat center center;
}
.my-div {
background-image: url(/static/media/something.svg);
}
`
});
const fixedCssCodeExpected = `
.my-div {
background: url(/logo192.png) no-repeat center center;
background: var(--url1f9ef5a892c104c);
}
.my-div2 {
background: url(/logo192.png) no-repeat center center;
background: var(--url1f9ef5a892c104c);
}
.my-div {
background-image: url(/static/media/something.svg);
background-image: var(--urldd75cab58377c19);
}
`
`;
expect(isSameCode(fixedCssCode, fixedCssCodeExpected)).toBe(true);
const cssGlobalsToDefineExpected = {
"url1f9ef5a892c104c": "url(/logo192.png) no-repeat center center",
"urldd75cab58377c19": "url(/static/media/something.svg)"
};
expect(same(cssGlobalsToDefine, cssGlobalsToDefineExpected)).toBe(true);
const { cssCodeToPrependInHead } = generateCssCodeToDefineGlobals({
cssGlobalsToDefine,
"buildOptions": {
"urlPathname": undefined
}
});
const cssCodeToPrependInHeadExpected = `
:root {
--url1f9ef5a892c104c: url(\${url.resourcesPath}/build/logo192.png) no-repeat center center;
--urldd75cab58377c19: url(\${url.resourcesPath}/build/static/media/something.svg);
}
`;
expect(isSameCode(cssCodeToPrependInHead, cssCodeToPrependInHeadExpected)).toBe(true);
});
it("transforms absolute urls to css globals properly with custom urlPathname", () => {
const { fixedCssCode, cssGlobalsToDefine } = replaceImportsInCssCode({
"cssCode": `
.my-div {
background: url(/x/y/z/logo192.png) no-repeat center center;
}
.my-div2 {
background: url(/x/y/z/logo192.png) no-repeat center center;
}
.my-div {
background-image: url(/x/y/z/static/media/something.svg);
}
`
});
const fixedCssCodeExpected = `
.my-div {
background: var(--url1f9ef5a892c104c);
}
.my-div2 {
background: var(--url1f9ef5a892c104c);
}
.my-div {
background-image: var(--urldd75cab58377c19);
}
`;
assetIsSameCode(fixedCssCode, fixedCssCodeExpected);
const cssGlobalsToDefineExpected = {
"url1f9ef5a892c104c": "url(/logo192.png) no-repeat center center",
"urldd75cab58377c19": "url(/static/media/something.svg)"
};
assert(same(cssGlobalsToDefine, cssGlobalsToDefineExpected));
const { cssCodeToPrependInHead } = generateCssCodeToDefineGlobals({
cssGlobalsToDefine,
"buildOptions": {
"urlPathname": undefined
}
});
const cssCodeToPrependInHeadExpected = `
:root {
--url1f9ef5a892c104c: url(\${url.resourcesPath}/build/logo192.png) no-repeat center center;
--urldd75cab58377c19: url(\${url.resourcesPath}/build/static/media/something.svg);
}
`;
assetIsSameCode(cssCodeToPrependInHead, cssCodeToPrependInHeadExpected);
}
{
const { fixedCssCode, cssGlobalsToDefine } = replaceImportsInCssCode({
"cssCode": `
const fixedCssCodeExpected = `
.my-div {
background: url(/x/y/z/logo192.png) no-repeat center center;
background: var(--urlf8277cddaa2be78);
}
.my-div2 {
background: url(/x/y/z/logo192.png) no-repeat center center;
background: var(--urlf8277cddaa2be78);
}
.my-div {
background-image: url(/x/y/z/static/media/something.svg);
background-image: var(--url8bdc0887b97ac9a);
}
`
`;
expect(isSameCode(fixedCssCode, fixedCssCodeExpected)).toBe(true);
const cssGlobalsToDefineExpected = {
"urlf8277cddaa2be78": "url(/x/y/z/logo192.png) no-repeat center center",
"url8bdc0887b97ac9a": "url(/x/y/z/static/media/something.svg)"
};
expect(same(cssGlobalsToDefine, cssGlobalsToDefineExpected)).toBe(true);
const { cssCodeToPrependInHead } = generateCssCodeToDefineGlobals({
cssGlobalsToDefine,
"buildOptions": {
"urlPathname": "/x/y/z/"
}
});
const cssCodeToPrependInHeadExpected = `
:root {
--urlf8277cddaa2be78: url(\${url.resourcesPath}/build/logo192.png) no-repeat center center;
--url8bdc0887b97ac9a: url(\${url.resourcesPath}/build/static/media/something.svg);
}
`;
expect(isSameCode(cssCodeToPrependInHead, cssCodeToPrependInHeadExpected)).toBe(true);
});
});
const fixedCssCodeExpected = `
.my-div {
background: var(--urlf8277cddaa2be78);
}
.my-div2 {
background: var(--urlf8277cddaa2be78);
}
.my-div {
background-image: var(--url8bdc0887b97ac9a);
}
`;
assetIsSameCode(fixedCssCode, fixedCssCodeExpected);
const cssGlobalsToDefineExpected = {
"urlf8277cddaa2be78": "url(/x/y/z/logo192.png) no-repeat center center",
"url8bdc0887b97ac9a": "url(/x/y/z/static/media/something.svg)"
};
assert(same(cssGlobalsToDefine, cssGlobalsToDefineExpected));
const { cssCodeToPrependInHead } = generateCssCodeToDefineGlobals({
cssGlobalsToDefine,
"buildOptions": {
"urlPathname": "/x/y/z/"
}
});
const cssCodeToPrependInHeadExpected = `
:root {
--urlf8277cddaa2be78: url(\${url.resourcesPath}/build/logo192.png) no-repeat center center;
--url8bdc0887b97ac9a: url(\${url.resourcesPath}/build/static/media/something.svg);
}
`;
assetIsSameCode(cssCodeToPrependInHead, cssCodeToPrependInHeadExpected);
}
{
const cssCode = `
describe("bin/css-inline-transforms", () => {
describe("no url pathName", () => {
const cssCode = `
@font-face {
font-family: "Work Sans";
font-style: normal;
@ -299,17 +300,16 @@ import { assetIsSameCode } from "../tools/assertIsSameCode";
src: url("/fonts/WorkSans/worksans-bold-webfont.woff2") format("woff2");
}
`;
it("transforms css for standalone app properly", () => {
const { fixedCssCode } = replaceImportsInInlineCssCode({
cssCode,
"buildOptions": {
"isStandalone": true,
"urlPathname": undefined
}
});
{
const { fixedCssCode } = replaceImportsInInlineCssCode({
cssCode,
"buildOptions": {
"isStandalone": true,
"urlPathname": undefined
}
});
const fixedCssCodeExpected = `
const fixedCssCodeExpected = `
@font-face {
font-family: "Work Sans";
font-style: normal;
@ -344,20 +344,19 @@ import { assetIsSameCode } from "../tools/assertIsSameCode";
}
`;
assetIsSameCode(fixedCssCode, fixedCssCodeExpected);
}
{
const { fixedCssCode } = replaceImportsInInlineCssCode({
cssCode,
"buildOptions": {
"isStandalone": false,
"urlOrigin": "https://demo-app.keycloakify.dev",
"urlPathname": undefined
}
expect(isSameCode(fixedCssCode, fixedCssCodeExpected)).toBe(true);
});
it("transforms css for external app properly", () => {
const { fixedCssCode } = replaceImportsInInlineCssCode({
cssCode,
"buildOptions": {
"isStandalone": false,
"urlOrigin": "https://demo-app.keycloakify.dev",
"urlPathname": undefined
}
});
const fixedCssCodeExpected = `
const fixedCssCodeExpected = `
@font-face {
font-family: "Work Sans";
font-style: normal;
@ -392,12 +391,12 @@ import { assetIsSameCode } from "../tools/assertIsSameCode";
}
`;
assetIsSameCode(fixedCssCode, fixedCssCodeExpected);
}
}
expect(isSameCode(fixedCssCode, fixedCssCodeExpected)).toBe(true);
});
});
{
const cssCode = `
describe("with url pathName", () => {
const cssCode = `
@font-face {
font-family: "Work Sans";
font-style: normal;
@ -427,101 +426,98 @@ import { assetIsSameCode } from "../tools/assertIsSameCode";
src: url("/x/y/z/fonts/WorkSans/worksans-bold-webfont.woff2") format("woff2");
}
`;
it("transforms css for standalone app properly", () => {
const { fixedCssCode } = replaceImportsInInlineCssCode({
cssCode,
"buildOptions": {
"isStandalone": true,
"urlPathname": "/x/y/z/"
}
});
{
const { fixedCssCode } = replaceImportsInInlineCssCode({
cssCode,
"buildOptions": {
"isStandalone": true,
"urlPathname": "/x/y/z/"
}
const fixedCssCodeExpected = `
@font-face {
font-family: "Work Sans";
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(\${url.resourcesPath}/build/fonts/WorkSans/worksans-regular-webfont.woff2)
format("woff2");
}
@font-face {
font-family: "Work Sans";
font-style: normal;
font-weight: 500;
font-display: swap;
src: url(\${url.resourcesPath}/build/fonts/WorkSans/worksans-medium-webfont.woff2)
format("woff2");
}
@font-face {
font-family: "Work Sans";
font-style: normal;
font-weight: 600;
font-display: swap;
src: url(\${url.resourcesPath}/build/fonts/WorkSans/worksans-semibold-webfont.woff2)
format("woff2");
}
@font-face {
font-family: "Work Sans";
font-style: normal;
font-weight: 700;
font-display: swap;
src: url(\${url.resourcesPath}/build/fonts/WorkSans/worksans-bold-webfont.woff2)
format("woff2");
}
`;
expect(isSameCode(fixedCssCode, fixedCssCodeExpected)).toBe(true);
});
it("transforms css for external app properly", () => {
const { fixedCssCode } = replaceImportsInInlineCssCode({
cssCode,
"buildOptions": {
"isStandalone": false,
"urlOrigin": "https://demo-app.keycloakify.dev",
"urlPathname": "/x/y/z/"
}
});
const fixedCssCodeExpected = `
@font-face {
font-family: "Work Sans";
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(\${url.resourcesPath}/build/fonts/WorkSans/worksans-regular-webfont.woff2)
format("woff2");
}
@font-face {
font-family: "Work Sans";
font-style: normal;
font-weight: 500;
font-display: swap;
src: url(\${url.resourcesPath}/build/fonts/WorkSans/worksans-medium-webfont.woff2)
format("woff2");
}
@font-face {
font-family: "Work Sans";
font-style: normal;
font-weight: 600;
font-display: swap;
src: url(\${url.resourcesPath}/build/fonts/WorkSans/worksans-semibold-webfont.woff2)
format("woff2");
}
@font-face {
font-family: "Work Sans";
font-style: normal;
font-weight: 700;
font-display: swap;
src: url(\${url.resourcesPath}/build/fonts/WorkSans/worksans-bold-webfont.woff2)
format("woff2");
}
`;
const fixedCssCodeExpected = `
@font-face {
font-family: "Work Sans";
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(https://demo-app.keycloakify.dev/x/y/z/fonts/WorkSans/worksans-regular-webfont.woff2)
format("woff2");
}
@font-face {
font-family: "Work Sans";
font-style: normal;
font-weight: 500;
font-display: swap;
src: url(https://demo-app.keycloakify.dev/x/y/z/fonts/WorkSans/worksans-medium-webfont.woff2)
format("woff2");
}
@font-face {
font-family: "Work Sans";
font-style: normal;
font-weight: 600;
font-display: swap;
src: url(https://demo-app.keycloakify.dev/x/y/z/fonts/WorkSans/worksans-semibold-webfont.woff2)
format("woff2");
}
@font-face {
font-family: "Work Sans";
font-style: normal;
font-weight: 700;
font-display: swap;
src: url(https://demo-app.keycloakify.dev/x/y/z/fonts/WorkSans/worksans-bold-webfont.woff2)
format("woff2");
}
`;
assetIsSameCode(fixedCssCode, fixedCssCodeExpected);
}
{
const { fixedCssCode } = replaceImportsInInlineCssCode({
cssCode,
"buildOptions": {
"isStandalone": false,
"urlOrigin": "https://demo-app.keycloakify.dev",
"urlPathname": "/x/y/z/"
}
expect(isSameCode(fixedCssCode, fixedCssCodeExpected)).toBe(true);
});
const fixedCssCodeExpected = `
@font-face {
font-family: "Work Sans";
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(https://demo-app.keycloakify.dev/x/y/z/fonts/WorkSans/worksans-regular-webfont.woff2)
format("woff2");
}
@font-face {
font-family: "Work Sans";
font-style: normal;
font-weight: 500;
font-display: swap;
src: url(https://demo-app.keycloakify.dev/x/y/z/fonts/WorkSans/worksans-medium-webfont.woff2)
format("woff2");
}
@font-face {
font-family: "Work Sans";
font-style: normal;
font-weight: 600;
font-display: swap;
src: url(https://demo-app.keycloakify.dev/x/y/z/fonts/WorkSans/worksans-semibold-webfont.woff2)
format("woff2");
}
@font-face {
font-family: "Work Sans";
font-style: normal;
font-weight: 700;
font-display: swap;
src: url(https://demo-app.keycloakify.dev/x/y/z/fonts/WorkSans/worksans-bold-webfont.woff2)
format("woff2");
}
`;
assetIsSameCode(fixedCssCode, fixedCssCodeExpected);
}
}
console.log("PASS replace import from static");
});
});

View File

@ -1,13 +1,14 @@
import { getKcContext } from "../../src/login/kcContext/getKcContext";
import type { ExtendKcContext } from "../../src/login/kcContext/getKcContextFromWindow";
import type { KcContext } from "../../src/login/kcContext";
import { getKcContext } from "keycloakify/login/kcContext/getKcContext";
import type { ExtendKcContext } from "keycloakify/login/kcContext/getKcContextFromWindow";
import type { KcContext } from "keycloakify/login/kcContext";
import { same } from "evt/tools/inDepth";
import { assert } from "tsafe/assert";
import type { Equals } from "tsafe";
import { kcContextMocks, kcContextCommonMock } from "../../src/login/kcContext/kcContextMocks";
import { deepClone } from "../../src/tools/deepClone";
import { kcContextMocks, kcContextCommonMock } from "keycloakify/login/kcContext/kcContextMocks";
import { deepClone } from "keycloakify/tools/deepClone";
import { expect, it, describe } from "vitest";
{
describe("getKcContext", () => {
const authorizedMailDomains = ["example.com", "another-example.com", "*.yet-another-example.com", "*.example.com", "hello-world.com"];
const displayName = "this is an overwritten common value";
@ -59,8 +60,7 @@ import { deepClone } from "../../src/tools/deepClone";
return { kcContext };
};
{
it("has proper API for login.ftl", () => {
const pageId = "login.ftl";
const { kcContext } = getKcContextProxy({ "mockPageId": pageId });
@ -69,7 +69,7 @@ import { deepClone } from "../../src/tools/deepClone";
assert<Equals<typeof kcContext, KcContext.Login>>();
assert(
expect(
same(
//NOTE: deepClone for printIfExists or other functions...
deepClone(kcContext),
@ -81,12 +81,10 @@ import { deepClone } from "../../src/tools/deepClone";
return mock;
})()
)
);
).toBe(true);
});
console.log(`PASS ${pageId}`);
}
{
it("has a proper API for info.ftl", () => {
const pageId = "info.ftl";
const { kcContext } = getKcContextProxy({ "mockPageId": pageId });
@ -104,7 +102,7 @@ import { deepClone } from "../../src/tools/deepClone";
>
>();
assert(
expect(
same(
deepClone(kcContext),
(() => {
@ -115,12 +113,9 @@ import { deepClone } from "../../src/tools/deepClone";
return mock;
})()
)
);
console.log(`PASS ${pageId}`);
}
{
).toBe(true);
});
it("has a proper API for register.ftl", () => {
const pageId = "register.ftl";
const { kcContext } = getKcContextProxy({ "mockPageId": pageId });
@ -138,7 +133,7 @@ import { deepClone } from "../../src/tools/deepClone";
>
>();
assert(
expect(
same(
deepClone(kcContext),
(() => {
@ -149,12 +144,9 @@ import { deepClone } from "../../src/tools/deepClone";
return mock;
})()
)
);
console.log(`PASS ${pageId}`);
}
{
).toBe(true);
});
it("has a proper API for my-extra-page-2.ftl", () => {
const pageId = "my-extra-page-2.ftl";
const { kcContext } = getKcContextProxy({ "mockPageId": pageId });
@ -173,7 +165,7 @@ import { deepClone } from "../../src/tools/deepClone";
kcContext.aNonStandardValue2;
assert(
expect(
same(
deepClone(kcContext),
(() => {
@ -184,12 +176,9 @@ import { deepClone } from "../../src/tools/deepClone";
return mock;
})()
)
);
console.log(`PASS ${pageId}`);
}
{
).toBe(true);
});
it("has a proper API for my-extra-page-1.ftl", () => {
const pageId = "my-extra-page-1.ftl";
console.log("We expect a warning here =>");
@ -200,7 +189,7 @@ import { deepClone } from "../../src/tools/deepClone";
assert<Equals<typeof kcContext, KcContext.Common & { pageId: typeof pageId }>>();
assert(
expect(
same(
deepClone(kcContext),
(() => {
@ -211,32 +200,24 @@ import { deepClone } from "../../src/tools/deepClone";
return mock;
})()
)
);
console.log(`PASS ${pageId}`);
}
}
{
const pageId = "login.ftl";
const { kcContext } = getKcContext({
"mockPageId": pageId
).toBe(true);
});
it("returns the proper mock for login.ftl", () => {
const pageId = "login.ftl";
assert<Equals<typeof kcContext, KcContext | undefined>>();
const { kcContext } = getKcContext({
"mockPageId": pageId
});
assert(same(deepClone(kcContext), deepClone(kcContextMocks.find(({ pageId: pageId_i }) => pageId_i === pageId)!)));
assert<Equals<typeof kcContext, KcContext | undefined>>();
console.log("PASS no extension");
}
assert(same(deepClone(kcContext), deepClone(kcContextMocks.find(({ pageId: pageId_i }) => pageId_i === pageId)!)));
});
it("returns the proper mock for login.ftl", () => {
const { kcContext } = getKcContext();
{
const { kcContext } = getKcContext();
assert<Equals<typeof kcContext, KcContext | undefined>>();
assert<Equals<typeof kcContext, KcContext | undefined>>();
assert(kcContext === undefined);
console.log("PASS no extension, no mock");
}
assert(kcContext === undefined);
});
});

View File

@ -1 +0,0 @@
import "./getKcContext";

View File

@ -0,0 +1,94 @@
import { AndByDiscriminatingKey } from "keycloakify/tools/AndByDiscriminatingKey";
import { assert } from "tsafe/assert";
import type { Equals } from "tsafe";
import { it, describe } from "vitest";
// These test case names are intentionally vague, because I'm not sure what each are testing individually
describe("AndByDiscriminatingKey", () => {
it("Test Case 1", () => {
type Base = { pageId: "a"; onlyA: string } | { pageId: "b"; onlyB: string } | { pageId: "only base"; onlyBase: string };
type Extension = { pageId: "a"; onlyExtA: string } | { pageId: "b"; onlyExtB: string } | { pageId: "only ext"; onlyExt: string };
type Got = AndByDiscriminatingKey<"pageId", Extension, Base>;
type Expected =
| { pageId: "a"; onlyA: string; onlyExtA: string }
| { pageId: "b"; onlyB: string; onlyExtB: string }
| { pageId: "only base"; onlyBase: string }
| { pageId: "only ext"; onlyExt: string };
assert<Equals<Got, Expected>>();
const x: Got = {} as any;
if (x.pageId === "a") {
x.onlyA;
x.onlyExtA;
//@ts-expect-error
x.onlyB;
//@ts-expect-error
x.onlyBase;
//@ts-expect-error
x.onlyExt;
}
if (x.pageId === "b") {
x.onlyB;
x.onlyExtB;
//@ts-expect-error
x.onlyA;
//@ts-expect-error
x.onlyBase;
//@ts-expect-error
x.onlyExt;
}
if (x.pageId === "only base") {
x.onlyBase;
//@ts-expect-error
x.onlyA;
//@ts-expect-error
x.onlyB;
//@ts-expect-error
x.onlyExt;
}
if (x.pageId === "only ext") {
x.onlyExt;
//@ts-expect-error
x.onlyA;
//@ts-expect-error
x.onlyB;
//@ts-expect-error
x.onlyBase;
}
});
it("Test Case 2", () => {
type Base = { pageId: "a"; onlyA: string } | { pageId: "b"; onlyB: string } | { pageId: "only base"; onlyBase: string };
type Extension = { pageId: "only ext"; onlyExt: string };
type Got = AndByDiscriminatingKey<"pageId", Extension, Base>;
type Expected =
| { pageId: "a"; onlyA: string }
| { pageId: "b"; onlyB: string }
| { pageId: "only base"; onlyBase: string }
| { pageId: "only ext"; onlyExt: string };
assert<Equals<Got, Expected>>();
});
});

View File

@ -1,91 +0,0 @@
import { AndByDiscriminatingKey } from "../../../src/tools/AndByDiscriminatingKey";
import { assert } from "tsafe/assert";
import type { Equals } from "tsafe";
{
type Base = { pageId: "a"; onlyA: string } | { pageId: "b"; onlyB: string } | { pageId: "only base"; onlyBase: string };
type Extension = { pageId: "a"; onlyExtA: string } | { pageId: "b"; onlyExtB: string } | { pageId: "only ext"; onlyExt: string };
type Got = AndByDiscriminatingKey<"pageId", Extension, Base>;
type Expected =
| { pageId: "a"; onlyA: string; onlyExtA: string }
| { pageId: "b"; onlyB: string; onlyExtB: string }
| { pageId: "only base"; onlyBase: string }
| { pageId: "only ext"; onlyExt: string };
assert<Equals<Got, Expected>>();
const x: Got = null as any;
if (x.pageId === "a") {
x.onlyA;
x.onlyExtA;
//@ts-expect-error
x.onlyB;
//@ts-expect-error
x.onlyBase;
//@ts-expect-error
x.onlyExt;
}
if (x.pageId === "b") {
x.onlyB;
x.onlyExtB;
//@ts-expect-error
x.onlyA;
//@ts-expect-error
x.onlyBase;
//@ts-expect-error
x.onlyExt;
}
if (x.pageId === "only base") {
x.onlyBase;
//@ts-expect-error
x.onlyA;
//@ts-expect-error
x.onlyB;
//@ts-expect-error
x.onlyExt;
}
if (x.pageId === "only ext") {
x.onlyExt;
//@ts-expect-error
x.onlyA;
//@ts-expect-error
x.onlyB;
//@ts-expect-error
x.onlyBase;
}
}
{
type Base = { pageId: "a"; onlyA: string } | { pageId: "b"; onlyB: string } | { pageId: "only base"; onlyBase: string };
type Extension = { pageId: "only ext"; onlyExt: string };
type Got = AndByDiscriminatingKey<"pageId", Extension, Base>;
type Expected =
| { pageId: "a"; onlyA: string }
| { pageId: "b"; onlyB: string }
| { pageId: "only base"; onlyBase: string }
| { pageId: "only ext"; onlyExt: string };
assert<Equals<Got, Expected>>();
}

View File

@ -1,7 +0,0 @@
import { assert } from "tsafe/assert";
export function assetIsSameCode(code1: string, code2: string, message?: string): void {
const removeSpacesAndNewLines = (code: string) => code.replace(/\s/g, "").replace(/\n/g, "");
assert(removeSpacesAndNewLines(code1) === removeSpacesAndNewLines(code2), message);
}

5
test/tools/isSameCode.ts Normal file
View File

@ -0,0 +1,5 @@
export function isSameCode(code1: string, code2: string): boolean {
const removeSpacesAndNewLines = (code: string) => code.replace(/\s/g, "").replace(/\n/g, "");
return removeSpacesAndNewLines(code1) === removeSpacesAndNewLines(code2);
}