2021-06-28 04:04:48 +02:00
|
|
|
import { getKcContext } from "../../lib/getKcContext";
|
|
|
|
import type { KcContextBase } from "../../lib/getKcContext";
|
2021-11-07 20:17:14 +01:00
|
|
|
import type { ExtendsKcContextBase } from "../../lib/getKcContext";
|
2021-06-28 04:04:48 +02:00
|
|
|
import { same } from "evt/tools/inDepth";
|
|
|
|
import { assert } from "tsafe/assert";
|
2021-10-11 21:35:40 +02:00
|
|
|
import type { Equals } from "tsafe";
|
2021-10-12 00:26:29 +02:00
|
|
|
import { kcContextMocks, kcContextCommonMock } from "../../lib/getKcContext/kcContextMocks";
|
2021-07-03 02:39:39 +02:00
|
|
|
import { deepClone } from "../../lib/tools/deepClone";
|
2021-06-28 04:04:48 +02:00
|
|
|
|
2021-07-03 02:39:39 +02:00
|
|
|
{
|
2021-10-12 00:26:29 +02:00
|
|
|
const authorizedMailDomains = ["example.com", "another-example.com", "*.yet-another-example.com", "*.example.com", "hello-world.com"];
|
2021-10-11 21:35:40 +02:00
|
|
|
|
|
|
|
const displayName = "this is an overwritten common value";
|
|
|
|
|
|
|
|
const aNonStandardValue1 = "a non standard value 1";
|
|
|
|
const aNonStandardValue2 = "a non standard value 2";
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2021-10-12 00:26:29 +02:00
|
|
|
const getKcContextProxy = (params: { mockPageId: ExtendsKcContextBase<KcContextExtended>["pageId"] }) => {
|
2021-10-11 21:35:40 +02:00
|
|
|
const { mockPageId } = params;
|
|
|
|
|
|
|
|
const { kcContext } = getKcContext<KcContextExtended>({
|
|
|
|
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);
|
|
|
|
|
|
|
|
assert<Equals<typeof kcContext, KcContextBase.Login>>();
|
|
|
|
|
|
|
|
assert(
|
|
|
|
same(
|
|
|
|
//NOTE: deepClone for printIfExists or other functions...
|
|
|
|
deepClone(kcContext),
|
|
|
|
(() => {
|
2021-10-12 00:26:29 +02:00
|
|
|
const mock = deepClone(kcContextMocks.find(({ pageId: pageId_i }) => pageId_i === pageId)!);
|
2021-10-11 21:35:40 +02:00
|
|
|
|
|
|
|
mock.realm.displayName = displayName;
|
|
|
|
|
|
|
|
return mock;
|
|
|
|
})(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
console.log(`PASS ${pageId}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const pageId = "info.ftl";
|
|
|
|
|
|
|
|
const { kcContext } = getKcContextProxy({ "mockPageId": pageId });
|
|
|
|
|
|
|
|
assert(kcContext?.pageId === pageId);
|
|
|
|
|
|
|
|
//NOTE: I don't understand the need to add: pageId: typeof pageId; ...
|
|
|
|
assert<
|
|
|
|
Equals<
|
|
|
|
typeof kcContext,
|
|
|
|
KcContextBase.Info & {
|
|
|
|
pageId: typeof pageId;
|
|
|
|
aNonStandardValue1: string;
|
|
|
|
}
|
|
|
|
>
|
|
|
|
>();
|
|
|
|
|
|
|
|
assert(
|
|
|
|
same(
|
|
|
|
deepClone(kcContext),
|
|
|
|
(() => {
|
2021-10-12 00:26:29 +02:00
|
|
|
const mock = deepClone(kcContextMocks.find(({ pageId: pageId_i }) => pageId_i === pageId)!);
|
2021-10-11 21:35:40 +02:00
|
|
|
|
|
|
|
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; ...
|
|
|
|
assert<
|
|
|
|
Equals<
|
|
|
|
typeof kcContext,
|
|
|
|
KcContextBase.Register & {
|
|
|
|
pageId: typeof pageId;
|
|
|
|
authorizedMailDomains: string[];
|
|
|
|
}
|
|
|
|
>
|
|
|
|
>();
|
|
|
|
|
|
|
|
assert(
|
|
|
|
same(
|
|
|
|
deepClone(kcContext),
|
|
|
|
(() => {
|
2021-10-12 00:26:29 +02:00
|
|
|
const mock = deepClone(kcContextMocks.find(({ pageId: pageId_i }) => pageId_i === pageId)!);
|
2021-10-11 21:35:40 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
assert<
|
|
|
|
Equals<
|
|
|
|
typeof kcContext,
|
|
|
|
KcContextBase.Common & {
|
|
|
|
pageId: typeof pageId;
|
|
|
|
aNonStandardValue2: string;
|
|
|
|
}
|
|
|
|
>
|
|
|
|
>();
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-10-12 00:26:29 +02:00
|
|
|
assert<Equals<typeof kcContext, KcContextBase.Common & { pageId: typeof pageId }>>();
|
2021-06-28 04:04:48 +02:00
|
|
|
|
2021-10-11 21:35:40 +02:00
|
|
|
assert(
|
|
|
|
same(
|
|
|
|
deepClone(kcContext),
|
|
|
|
(() => {
|
|
|
|
const mock = deepClone(kcContextCommonMock);
|
2021-06-28 04:04:48 +02:00
|
|
|
|
2021-10-11 21:35:40 +02:00
|
|
|
Object.assign(mock, { pageId });
|
2021-06-28 04:04:48 +02:00
|
|
|
|
2021-10-11 21:35:40 +02:00
|
|
|
return mock;
|
|
|
|
})(),
|
|
|
|
),
|
|
|
|
);
|
2021-06-28 04:04:48 +02:00
|
|
|
|
2021-10-11 21:35:40 +02:00
|
|
|
console.log(`PASS ${pageId}`);
|
|
|
|
}
|
2021-06-28 04:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-10-11 21:35:40 +02:00
|
|
|
const pageId = "login.ftl";
|
|
|
|
|
|
|
|
const { kcContext } = getKcContext({
|
|
|
|
"mockPageId": pageId,
|
|
|
|
});
|
|
|
|
|
|
|
|
assert<Equals<typeof kcContext, KcContextBase | undefined>>();
|
|
|
|
|
2021-10-12 00:26:29 +02:00
|
|
|
assert(same(deepClone(kcContext), deepClone(kcContextMocks.find(({ pageId: pageId_i }) => pageId_i === pageId)!)));
|
2021-10-11 21:35:40 +02:00
|
|
|
|
|
|
|
console.log("PASS no extension");
|
2021-06-28 04:04:48 +02:00
|
|
|
}
|
|
|
|
|
2021-07-03 02:39:39 +02:00
|
|
|
{
|
2021-10-11 21:35:40 +02:00
|
|
|
const { kcContext } = getKcContext();
|
2021-06-28 04:04:48 +02:00
|
|
|
|
2021-10-11 21:35:40 +02:00
|
|
|
assert<Equals<typeof kcContext, KcContextBase | undefined>>();
|
2021-07-03 02:39:39 +02:00
|
|
|
|
2021-10-11 21:35:40 +02:00
|
|
|
assert(kcContext === undefined);
|
2021-07-03 02:39:39 +02:00
|
|
|
|
2021-10-11 21:35:40 +02:00
|
|
|
console.log("PASS no extension, no mock");
|
2021-07-03 02:39:39 +02:00
|
|
|
}
|