diff --git a/src/lib/getKcContext/getKcContext.ts b/src/lib/getKcContext/getKcContext.ts index 310c8c90..694da658 100644 --- a/src/lib/getKcContext/getKcContext.ts +++ b/src/lib/getKcContext/getKcContext.ts @@ -8,9 +8,9 @@ import { deepAssign } from "../tools/deepAssign"; export type ExtendsKcContextBase< - KcContextExtended extends ({ pageId: string; } | undefined) + KcContextExtended extends { pageId: string; } > = - KcContextExtended extends undefined ? + [KcContextExtended] extends [never] ? KcContextBase : AndByDiscriminatingKey< "pageId", @@ -18,7 +18,7 @@ export type ExtendsKcContextBase< KcContextBase >; -export function getKcContext( +export function getKcContext( params?: { mockPageId?: ExtendsKcContextBase["pageId"]; mockData?: readonly DeepPartial>[]; @@ -51,22 +51,15 @@ export function getKcContext; diff --git a/src/test/lib/getKcContext.ts b/src/test/lib/getKcContext.ts index 9d7020ff..2a835849 100644 --- a/src/test/lib/getKcContext.ts +++ b/src/test/lib/getKcContext.ts @@ -6,202 +6,245 @@ import { same } from "evt/tools/inDepth"; import { doExtends } from "tsafe/doExtends"; import { assert } from "tsafe/assert"; import { kcContextMocks, kcContextCommonMock } from "../../lib/getKcContext/kcContextMocks"; -import { deepClone } from "../../lib/tools/deepClone"; +import { deepClone } from "../../lib/tools/deepClone"; import type { Any } from "ts-toolbelt"; +{ -const authorizedMailDomains = [ - "example.com", - "another-example.com", - "*.yet-another-example.com", - "*.example.com", - "hello-world.com" -]; + const authorizedMailDomains = [ + "example.com", + "another-example.com", + "*.yet-another-example.com", + "*.example.com", + "hello-world.com" + ]; -const displayName = "this is an overwritten common value"; + const displayName = "this is an overwritten common value"; -const aNonStandardValue = "a non standard value"; + const aNonStandardValue1 = "a non standard value 1"; + const aNonStandardValue2 = "a non standard value 2"; -type KcContextExtended = { - pageId: "register.ftl"; - authorizedMailDomains: string[]; -} | { - pageId: "my-extra-page-1.ftl"; -} | { - pageId: "my-extra-page-2.ftl"; - aNonStandardValue: string; -}; + type KcContextExtended = { + pageId: "register.ftl"; + authorizedMailDomains: string[]; + } | { + pageId: "info.ftl"; + aNonStandardValue1: string; + } | { + pageId: "my-extra-page-1.ftl"; + } | { + pageId: "my-extra-page-2.ftl"; + aNonStandardValue2: string; + }; + + const getKcContextProxy = ( + params: { + mockPageId: ExtendsKcContextBase["pageId"]; + } + ) => { + + const { mockPageId } = params; + + const { kcContext } = getKcContext({ + mockPageId, + "mockData": [ + { + "pageId": "login.ftl", + "realm": { displayName } + }, + { + "pageId": "info.ftl", + aNonStandardValue1 + }, + { + "pageId": "register.ftl", + authorizedMailDomains + }, + { + "pageId": "my-extra-page-2.ftl", + aNonStandardValue2 + } + ] + }); + + return { kcContext }; + + }; + + { + + const pageId = "login.ftl"; + + const { kcContext } = getKcContextProxy({ "mockPageId": pageId }); + + assert(kcContext?.pageId === pageId); + + doExtends, 1>(); + + assert(same( + //NOTE: deepClone for printIfExists or other functions... + deepClone(kcContext), + (() => { + + const mock = deepClone(kcContextMocks.find(({ pageId: pageId_i }) => pageId_i === pageId)!); + + mock.realm.displayName = displayName; + + return mock; + + })() + )); + + console.log(`PASS ${pageId}`); -function getKcContextProxy( - params: { - mockPageId: ExtendsKcContextBase["pageId"]; } -) { - const { mockPageId } = params; + { + const pageId = "info.ftl"; - const { kcContext } = getKcContext({ - mockPageId, - "mockData": [ - { - "pageId": "login.ftl", - "realm": { displayName } - }, - { - "pageId": "register.ftl", - authorizedMailDomains - }, - { - "pageId": "my-extra-page-2.ftl", - aNonStandardValue - } - ] + const { kcContext } = getKcContextProxy({ "mockPageId": pageId }); + + assert(kcContext?.pageId === pageId); + + //NOTE: I don't understand the need to add: pageId: typeof pageId; ... + doExtends, 1>(); + + assert(same( + deepClone(kcContext), + (() => { + + const mock = deepClone(kcContextMocks.find(({ pageId: pageId_i }) => pageId_i === pageId)!); + + Object.assign(mock, { aNonStandardValue1 }); + + return mock; + + })() + )); + + console.log(`PASS ${pageId}`); + + } + + { + const pageId = "register.ftl"; + + const { kcContext } = getKcContextProxy({ "mockPageId": pageId }); + + assert(kcContext?.pageId === pageId); + + //NOTE: I don't understand the need to add: pageId: typeof pageId; ... + doExtends, 1>(); + + assert(same( + deepClone(kcContext), + (() => { + + const mock = deepClone(kcContextMocks.find(({ pageId: pageId_i }) => pageId_i === pageId)!); + + Object.assign(mock, { authorizedMailDomains }); + + return mock; + + })() + )); + + console.log(`PASS ${pageId}`); + + } + + { + + const pageId = "my-extra-page-2.ftl"; + + const { kcContext } = getKcContextProxy({ "mockPageId": pageId }); + + assert(kcContext?.pageId === pageId); + + doExtends, 1>(); + + kcContext.aNonStandardValue2; + + assert(same( + deepClone(kcContext), + (() => { + + const mock = deepClone(kcContextCommonMock); + + Object.assign(mock, { pageId, aNonStandardValue2 }); + + return mock; + + })() + )); + + console.log(`PASS ${pageId}`); + + } + + { + + const pageId = "my-extra-page-1.ftl"; + + console.log("We expect a warning here =>"); + + const { kcContext } = getKcContextProxy({ "mockPageId": pageId }); + + + assert(kcContext?.pageId === pageId); + + doExtends, 1>(); + + assert(same( + deepClone(kcContext), + (() => { + + const mock = deepClone(kcContextCommonMock); + + Object.assign(mock, { pageId }); + + return mock; + + })() + )); + + console.log(`PASS ${pageId}`); + + } + +} + +{ + + const pageId = "login.ftl"; + + const { kcContext } = getKcContext({ + "mockPageId": pageId }); - return { kcContext }; - -} - -{ - - const pageId= "login.ftl"; - - const { kcContext } = getKcContextProxy({ "mockPageId": pageId }); - - //@ts-expect-error - doExtends, 1>(); - - assert(kcContext?.pageId === pageId); - - doExtends(); - - assert(same( - //NOTE: deepClone for printIfExists or other functions... - deepClone(kcContext), - (() => { - - const mock = deepClone(kcContextMocks.find(({ pageId: pageId_i }) => pageId_i === pageId)!); - - mock.realm.displayName = displayName; - - return mock; - - })() - )); - - console.log(`PASS ${pageId}`); - -} - -{ - const pageId = "register.ftl"; - - const { kcContext } = getKcContextProxy({ "mockPageId": pageId }); - - //@ts-expect-error - doExtends, 1>(); - - assert(kcContext?.pageId === pageId); - - doExtends(); + doExtends, 1>(); assert(same( deepClone(kcContext), - (() => { - - const mock = deepClone(kcContextMocks.find(({ pageId: pageId_i }) => pageId_i === pageId)!); - - Object.assign(mock, { authorizedMailDomains }); - - return mock; - - })() + deepClone(kcContextMocks.find(({ pageId: pageId_i }) => pageId_i === pageId)!) )); - console.log(`PASS ${pageId}`); - -} - -{ - - const pageId = "my-extra-page-2.ftl"; - - const { kcContext } = getKcContextProxy({ "mockPageId": pageId }); - - //@ts-expect-error - doExtends, 1>(); - - assert(kcContext?.pageId === pageId); - - //@ts-expect-error - doExtends(); - - doExtends(); - - assert(same( - deepClone(kcContext), - (() => { - - const mock = deepClone(kcContextCommonMock); - - Object.assign(mock, { pageId, aNonStandardValue }); - - return mock; - - })() - )); - - console.log(`PASS ${pageId}`); + console.log("PASS no extension"); } -{ - - const pageId = "my-extra-page-1.ftl"; - - console.log("We expect a warning here =>"); - - const { kcContext } = getKcContextProxy({ "mockPageId": pageId }); - - //@ts-expect-error - doExtends, 1>(); - - assert(kcContext?.pageId === pageId); - - //@ts-expect-error - doExtends(); - - doExtends(); - - assert(same( - deepClone(kcContext), - (() => { - - const mock = deepClone(kcContextCommonMock); - - Object.assign(mock, { pageId }); - - return mock; - - })() - )); - - console.log(`PASS ${pageId}`); - -} { + const { kcContext } = getKcContext(); - //@ts-expect-error - doExtends, 1>(); + doExtends, 1>(); - doExtends(); + assert(kcContext === undefined); + + console.log("PASS no extension, no mock"); } - - -