Better typing for createGetKcContext
This commit is contained in:
parent
d1c7491704
commit
f6e6cf3750
@ -15,10 +15,14 @@ export function createGetKcContext<KcContextExtension extends { pageId: string }
|
||||
}) {
|
||||
const { mockData } = params ?? {};
|
||||
|
||||
function getKcContext<PageId extends ExtendKcContext<KcContextExtension>["pageId"] = ExtendKcContext<KcContextExtension>["pageId"]>(params?: {
|
||||
function getKcContext<PageId extends ExtendKcContext<KcContextExtension>["pageId"]>(params?: {
|
||||
mockPageId?: PageId;
|
||||
storyParams?: DeepPartial<Extract<ExtendKcContext<KcContextExtension>, { pageId: PageId }>>;
|
||||
}): { kcContext: Extract<ExtendKcContext<KcContextExtension>, { pageId: PageId }> | undefined } {
|
||||
}): {
|
||||
kcContext: ExtendKcContext<KcContextExtension>["pageId"] extends PageId
|
||||
? ExtendKcContext<KcContextExtension> | undefined
|
||||
: Extract<ExtendKcContext<KcContextExtension>, { pageId: PageId }>;
|
||||
} {
|
||||
const { mockPageId, storyParams } = params ?? {};
|
||||
|
||||
const realKcContext = getKcContextFromWindow<KcContextExtension>();
|
||||
@ -86,11 +90,11 @@ export function createGetKcContext<KcContextExtension extends { pageId: string }
|
||||
}
|
||||
|
||||
if (realKcContext === undefined) {
|
||||
return { "kcContext": undefined };
|
||||
return { "kcContext": undefined as any };
|
||||
}
|
||||
|
||||
if (id<readonly string[]>(accountThemePageIds).indexOf(realKcContext.pageId) < 0 && !("account" in realKcContext)) {
|
||||
return { "kcContext": undefined };
|
||||
return { "kcContext": undefined as any };
|
||||
}
|
||||
|
||||
{
|
||||
|
@ -18,10 +18,14 @@ export function createGetKcContext<KcContextExtension extends { pageId: string }
|
||||
}) {
|
||||
const { mockData } = params ?? {};
|
||||
|
||||
function getKcContext<PageId extends ExtendKcContext<KcContextExtension>["pageId"] = ExtendKcContext<KcContextExtension>["pageId"]>(params?: {
|
||||
function getKcContext<PageId extends ExtendKcContext<KcContextExtension>["pageId"]>(params?: {
|
||||
mockPageId?: PageId;
|
||||
storyParams?: DeepPartial<Extract<ExtendKcContext<KcContextExtension>, { pageId: PageId }>>;
|
||||
}): { kcContext: Extract<ExtendKcContext<KcContextExtension>, { pageId: PageId }> | undefined } {
|
||||
}): {
|
||||
kcContext: ExtendKcContext<KcContextExtension>["pageId"] extends PageId
|
||||
? ExtendKcContext<KcContextExtension> | undefined
|
||||
: Extract<ExtendKcContext<KcContextExtension>, { pageId: PageId }>;
|
||||
} {
|
||||
const { mockPageId, storyParams } = params ?? {};
|
||||
|
||||
const realKcContext = getKcContextFromWindow<KcContextExtension>();
|
||||
@ -144,11 +148,11 @@ export function createGetKcContext<KcContextExtension extends { pageId: string }
|
||||
}
|
||||
|
||||
if (realKcContext === undefined) {
|
||||
return { "kcContext": undefined };
|
||||
return { "kcContext": undefined as any };
|
||||
}
|
||||
|
||||
if (id<readonly string[]>(loginThemePageIds).indexOf(realKcContext.pageId) < 0 && !("login" in realKcContext)) {
|
||||
return { "kcContext": undefined };
|
||||
return { "kcContext": undefined as any };
|
||||
}
|
||||
|
||||
{
|
||||
|
@ -214,7 +214,7 @@ describe("createGetKcContext", () => {
|
||||
"mockPageId": pageId
|
||||
});
|
||||
|
||||
assert<Equals<typeof kcContext, KcContext.Login | undefined>>();
|
||||
assert<Equals<typeof kcContext, KcContext.Login>>();
|
||||
|
||||
assert(same(deepClone(kcContext), deepClone(kcContextMocks.find(({ pageId: pageId_i }) => pageId_i === pageId)!)));
|
||||
});
|
||||
@ -228,7 +228,7 @@ describe("createGetKcContext", () => {
|
||||
assert(kcContext === undefined);
|
||||
});
|
||||
|
||||
it("mock are properly overwriten", () => {
|
||||
it("mock are properly overwritten", () => {
|
||||
const { getKcContext } = createGetKcContext();
|
||||
|
||||
const displayName = "myDisplayName";
|
||||
@ -242,8 +242,20 @@ describe("createGetKcContext", () => {
|
||||
}
|
||||
});
|
||||
|
||||
assert<Equals<typeof kcContext, KcContext.Login | undefined>>();
|
||||
assert<Equals<typeof kcContext, KcContext.Login>>();
|
||||
|
||||
assert(kcContext?.realm.displayName === displayName);
|
||||
});
|
||||
|
||||
it("mockPageId doesn't have to be a singleton", () => {
|
||||
const { getKcContext } = createGetKcContext();
|
||||
|
||||
const mockPageId: "login.ftl" | "register.ftl" = "login.ftl" as any;
|
||||
|
||||
const { kcContext } = getKcContext({
|
||||
mockPageId
|
||||
});
|
||||
|
||||
assert<Equals<typeof kcContext, KcContext.Login | KcContext.Register>>();
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user