From 58bb4037870ac3e5f778cd4c268d8380d2b4c14c Mon Sep 17 00:00:00 2001 From: William Will <10997562+willwill96@users.noreply.github.com> Date: Thu, 30 Mar 2023 02:46:25 -0600 Subject: [PATCH] test: refactor existing tests to vitest --- test/bin/index.ts | 1 - test/bin/main.ts | 1 - ...tic.ts => replaceImportFromStatic.spec.ts} | 460 +++++++++--------- .../{getKcContext.ts => getKcContext.spec.ts} | 99 ++-- test/lib/index.ts | 1 - .../tools/AndByDiscriminatingKey.type.spec.ts | 94 ++++ test/lib/tools/AndByDiscriminatingKey.type.ts | 91 ---- test/tools/assertIsSameCode.ts | 7 - test/tools/isSameCode.ts | 5 + 9 files changed, 367 insertions(+), 392 deletions(-) delete mode 100644 test/bin/index.ts rename test/bin/{replaceImportFromStatic.ts => replaceImportFromStatic.spec.ts} (55%) rename test/lib/{getKcContext.ts => getKcContext.spec.ts} (77%) delete mode 100644 test/lib/index.ts create mode 100644 test/lib/tools/AndByDiscriminatingKey.type.spec.ts delete mode 100644 test/lib/tools/AndByDiscriminatingKey.type.ts delete mode 100644 test/tools/assertIsSameCode.ts create mode 100644 test/tools/isSameCode.ts diff --git a/test/bin/index.ts b/test/bin/index.ts deleted file mode 100644 index 6cbf91c6..00000000 --- a/test/bin/index.ts +++ /dev/null @@ -1 +0,0 @@ -import "./replaceImportFromStatic"; diff --git a/test/bin/main.ts b/test/bin/main.ts index 08be679b..531d4b3d 100644 --- a/test/bin/main.ts +++ b/test/bin/main.ts @@ -1,4 +1,3 @@ -import "./replaceImportFromStatic"; import { setupSampleReactProject, sampleReactProjectDirPath } from "./setupSampleReactProject"; import * as st from "scripting-tools"; import * as fs from "fs"; diff --git a/test/bin/replaceImportFromStatic.ts b/test/bin/replaceImportFromStatic.spec.ts similarity index 55% rename from test/bin/replaceImportFromStatic.ts rename to test/bin/replaceImportFromStatic.spec.ts index e66d97a3..c7894aca 100644 --- a/test/bin/replaceImportFromStatic.ts +++ b/test/bin/replaceImportFromStatic.spec.ts @@ -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"); + }); +}); diff --git a/test/lib/getKcContext.ts b/test/lib/getKcContext.spec.ts similarity index 77% rename from test/lib/getKcContext.ts rename to test/lib/getKcContext.spec.ts index f52364aa..20023fff 100644 --- a/test/lib/getKcContext.ts +++ b/test/lib/getKcContext.spec.ts @@ -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>(); - 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>(); - 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>(); + const { kcContext } = getKcContext({ + "mockPageId": pageId + }); - assert(same(deepClone(kcContext), deepClone(kcContextMocks.find(({ pageId: pageId_i }) => pageId_i === pageId)!))); + assert>(); - 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>(); - assert>(); - - assert(kcContext === undefined); - - console.log("PASS no extension, no mock"); -} + assert(kcContext === undefined); + }); +}); diff --git a/test/lib/index.ts b/test/lib/index.ts deleted file mode 100644 index 8df0b5d4..00000000 --- a/test/lib/index.ts +++ /dev/null @@ -1 +0,0 @@ -import "./getKcContext"; diff --git a/test/lib/tools/AndByDiscriminatingKey.type.spec.ts b/test/lib/tools/AndByDiscriminatingKey.type.spec.ts new file mode 100644 index 00000000..8edb9f87 --- /dev/null +++ b/test/lib/tools/AndByDiscriminatingKey.type.spec.ts @@ -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>(); + + 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>(); + }); +}); diff --git a/test/lib/tools/AndByDiscriminatingKey.type.ts b/test/lib/tools/AndByDiscriminatingKey.type.ts deleted file mode 100644 index f45e897f..00000000 --- a/test/lib/tools/AndByDiscriminatingKey.type.ts +++ /dev/null @@ -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>(); - - 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>(); -} diff --git a/test/tools/assertIsSameCode.ts b/test/tools/assertIsSameCode.ts deleted file mode 100644 index b63cb822..00000000 --- a/test/tools/assertIsSameCode.ts +++ /dev/null @@ -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); -} diff --git a/test/tools/isSameCode.ts b/test/tools/isSameCode.ts new file mode 100644 index 00000000..6d62a225 --- /dev/null +++ b/test/tools/isSameCode.ts @@ -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); +}