1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2023-03-17 09:33:30 +02:00
parent c9f16beb54
commit 88023e1f70
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
31 changed files with 1070 additions and 1271 deletions

View File

@ -1,2 +1 @@
module.exports = module.exports = require("@k8slens/jest").monorepoPackageConfig(__dirname).configForReact;
require("@k8slens/jest").monorepoPackageConfig(__dirname).configForReact;

View File

@ -20,21 +20,18 @@ import { filter, groupBy, map, nth, toPairs } from "lodash/fp";
import { computedInjectManyInjectable } from "@ogre-tools/injectable-extension-for-mobx"; import { computedInjectManyInjectable } from "@ogre-tools/injectable-extension-for-mobx";
import type { JsonPrimitive } from "type-fest"; import type { JsonPrimitive } from "type-fest";
export type JsonifiableObject = export type JsonifiableObject = { [Key in string]?: Jsonifiable } | { toJSON: () => Jsonifiable };
| { [Key in string]?: Jsonifiable }
| { toJSON: () => Jsonifiable };
export type JsonifiableArray = readonly Jsonifiable[]; export type JsonifiableArray = readonly Jsonifiable[];
export type Jsonifiable = JsonPrimitive | JsonifiableObject | JsonifiableArray; export type Jsonifiable = JsonPrimitive | JsonifiableObject | JsonifiableArray;
export type ComputedChannelFactory = <T>( export type ComputedChannelFactory = <T>(
channel: MessageChannel<T>, channel: MessageChannel<T>,
pendingValue: T pendingValue: T,
) => IComputedValue<T>; ) => IComputedValue<T>;
export const computedChannelInjectionToken = export const computedChannelInjectionToken = getInjectionToken<ComputedChannelFactory>({
getInjectionToken<ComputedChannelFactory>({ id: "computed-channel-injection-token",
id: "computed-channel-injection-token", });
});
export type ChannelObserver<T extends Jsonifiable> = { export type ChannelObserver<T extends Jsonifiable> = {
channel: MessageChannel<T>; channel: MessageChannel<T>;
@ -51,6 +48,10 @@ export const computedChannelObserverInjectionToken = getInjectionToken<
id: "computed-channel-observer", id: "computed-channel-observer",
}); });
export const computedChannelAdministrationChannel: MessageChannel<ComputedChannelAdminMessage> = {
id: "computed-channel-administration-channel",
};
const computedChannelInjectable = getInjectable({ const computedChannelInjectable = getInjectable({
id: "computed-channel", id: "computed-channel",
@ -67,7 +68,7 @@ const computedChannelInjectable = getInjectable({
if (!contextIsReactive) { if (!contextIsReactive) {
throw new Error( throw new Error(
`Tried to access value of computed channel "${channel.id}" outside of reactive context. This is not possible, as the value is acquired asynchronously sometime *after* being observed. Not respecting that, the value could be stale.` `Tried to access value of computed channel "${channel.id}" outside of reactive context. This is not possible, as the value is acquired asynchronously sometime *after* being observed. Not respecting that, the value could be stale.`,
); );
} }
@ -134,17 +135,17 @@ export const duplicateChannelObserverGuardInjectable = getInjectable({
groupBy((observer) => observer.channel.id), groupBy((observer) => observer.channel.id),
toPairs, toPairs,
filter(([, channelObservers]) => channelObservers.length > 1), filter(([, channelObservers]) => channelObservers.length > 1),
map(nth(0)) map(nth(0)),
); );
if (duplicateObserverChannelIds.length) { if (duplicateObserverChannelIds.length) {
throw new Error( throw new Error(
`Tried to register duplicate channel observer for channels "${duplicateObserverChannelIds.join( `Tried to register duplicate channel observer for channels "${duplicateObserverChannelIds.join(
'", "' '", "',
)}"` )}"`,
); );
} }
} },
); );
}, },
}; };
@ -153,59 +154,48 @@ export const duplicateChannelObserverGuardInjectable = getInjectable({
injectionToken: onLoadOfApplicationInjectionToken, injectionToken: onLoadOfApplicationInjectionToken,
}); });
export const computedChannelAdministrationChannel: MessageChannel<ComputedChannelAdminMessage> = export const computedChannelAdministrationListenerInjectable = getMessageChannelListenerInjectable({
{ id: "computed-channel-administration",
id: "computed-channel-administration-channel", getHandler: (di) => {
}; const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
export const computedChannelAdministrationListenerInjectable = const disposersByChannelId = new Map<string, () => void>();
getMessageChannelListenerInjectable({
id: "computed-channel-administration",
getHandler: (di) => {
const sendMessageToChannel = di.inject(
sendMessageToChannelInjectionToken
);
const disposersByChannelId = new Map<string, () => void>(); return (message) => {
if (message.status === "became-observed") {
const result = di
.injectMany(computedChannelObserverInjectionToken)
.find((channelObserver) => channelObserver.channel.id === message.channelId);
return (message) => { if (result === undefined) {
if (message.status === "became-observed") { return;
const result = di
.injectMany(computedChannelObserverInjectionToken)
.find(
(channelObserver) =>
channelObserver.channel.id === message.channelId
);
if (result === undefined) {
return;
}
const disposer = reaction(
() => result.observer.get(),
(observed) =>
sendMessageToChannel(
{
id: message.channelId,
},
observed
),
{
fireImmediately: true,
}
);
disposersByChannelId.set(message.channelId, disposer);
} else {
const disposer = disposersByChannelId.get(message.channelId);
disposer!();
} }
};
},
channel: computedChannelAdministrationChannel, const disposer = reaction(
}); () => result.observer.get(),
(observed) =>
sendMessageToChannel(
{
id: message.channelId,
},
observed,
),
{
fireImmediately: true,
},
);
disposersByChannelId.set(message.channelId, disposer);
} else {
const disposer = disposersByChannelId.get(message.channelId);
disposer?.();
}
};
},
channel: computedChannelAdministrationChannel,
});
export default computedChannelInjectable; export default computedChannelInjectable;

View File

@ -12,9 +12,7 @@ export const messagingFeature = getFeature({
di, di,
targetModule: module, targetModule: module,
getRequireContexts: () => [ getRequireContexts: () => [require.context("./", true, /\.injectable\.(ts|tsx)$/)],
require.context("./", true, /\.injectable\.(ts|tsx)$/),
],
}); });
}, },
}); });

View File

@ -1,10 +1,7 @@
import { getInjectable, getInjectionToken } from "@ogre-tools/injectable"; import { getInjectable, getInjectionToken } from "@ogre-tools/injectable";
import { enlistMessageChannelListenerInjectionToken } from "../message/enlist-message-channel-listener-injection-token"; import { enlistMessageChannelListenerInjectionToken } from "../message/enlist-message-channel-listener-injection-token";
import { import { getStartableStoppable, StartableStoppable } from "@k8slens/startable-stoppable";
getStartableStoppable,
StartableStoppable,
} from "@k8slens/startable-stoppable";
import { computedInjectManyInjectable } from "@ogre-tools/injectable-extension-for-mobx"; import { computedInjectManyInjectable } from "@ogre-tools/injectable-extension-for-mobx";
import { IComputedValue, reaction } from "mobx"; import { IComputedValue, reaction } from "mobx";
@ -15,62 +12,14 @@ import { enlistRequestChannelListenerInjectionToken } from "../request/enlist-re
import type { Channel } from "../channel.no-coverage"; import type { Channel } from "../channel.no-coverage";
export type ListeningOfChannels = StartableStoppable; export type ListeningOfChannels = StartableStoppable;
export const listeningOfChannelsInjectionToken = export const listeningOfChannelsInjectionToken = getInjectionToken<ListeningOfChannels>({
getInjectionToken<ListeningOfChannels>({ id: "listening-of-channels-injection-token",
id: "listening-of-channels-injection-token",
});
const listeningOfChannelsInjectable = getInjectable({
id: "listening-of-channels",
instantiate: (di) => {
const enlistMessageChannelListener = di.inject(
enlistMessageChannelListenerInjectionToken
);
const enlistRequestChannelListener = di.inject(
enlistRequestChannelListenerInjectionToken
);
const computedInjectMany = di.inject(computedInjectManyInjectable);
const messageChannelListeners = computedInjectMany(
messageChannelListenerInjectionToken
);
const requestChannelListeners = computedInjectMany(
requestChannelListenerInjectionToken
);
return getStartableStoppable("listening-of-channels", () => {
const stopListeningOfMessageChannels = listening(
messageChannelListeners,
enlistMessageChannelListener,
(x) => x.id
);
const stopListeningOfRequestChannels = listening(
requestChannelListeners,
enlistRequestChannelListener,
(x) => x.channel.id
);
return () => {
stopListeningOfMessageChannels();
stopListeningOfRequestChannels();
};
});
},
injectionToken: listeningOfChannelsInjectionToken,
}); });
export default listeningOfChannelsInjectable;
const listening = <T extends { id: string; channel: Channel<unknown> }>( const listening = <T extends { id: string; channel: Channel<unknown> }>(
channelListeners: IComputedValue<T[]>, channelListeners: IComputedValue<T[]>,
enlistChannelListener: (listener: T) => () => void, enlistChannelListener: (listener: T) => () => void,
getId: (listener: T) => string getId: (listener: T) => string,
) => { ) => {
const listenerDisposers = new Map<string, () => void>(); const listenerDisposers = new Map<string, () => void>();
@ -78,11 +27,11 @@ const listening = <T extends { id: string; channel: Channel<unknown> }>(
() => channelListeners.get(), () => channelListeners.get(),
(newValues, oldValues = []) => { (newValues, oldValues = []) => {
const addedListeners = newValues.filter( const addedListeners = newValues.filter(
(newValue) => !oldValues.some((oldValue) => oldValue.id === newValue.id) (newValue) => !oldValues.some((oldValue) => oldValue.id === newValue.id),
); );
const removedListeners = oldValues.filter( const removedListeners = oldValues.filter(
(oldValue) => !newValues.some((newValue) => newValue.id === oldValue.id) (oldValue) => !newValues.some((newValue) => newValue.id === oldValue.id),
); );
addedListeners.forEach((listener) => { addedListeners.forEach((listener) => {
@ -90,7 +39,7 @@ const listening = <T extends { id: string; channel: Channel<unknown> }>(
if (listenerDisposers.has(id)) { if (listenerDisposers.has(id)) {
throw new Error( throw new Error(
`Tried to add listener for channel "${listener.channel.id}" but listener already exists.` `Tried to add listener for channel "${listener.channel.id}" but listener already exists.`,
); );
} }
@ -108,7 +57,7 @@ const listening = <T extends { id: string; channel: Channel<unknown> }>(
}); });
}, },
{ fireImmediately: true } { fireImmediately: true },
); );
return () => { return () => {
@ -116,3 +65,42 @@ const listening = <T extends { id: string; channel: Channel<unknown> }>(
listenerDisposers.forEach((dispose) => dispose()); listenerDisposers.forEach((dispose) => dispose());
}; };
}; };
const listeningOfChannelsInjectable = getInjectable({
id: "listening-of-channels",
instantiate: (di) => {
const enlistMessageChannelListener = di.inject(enlistMessageChannelListenerInjectionToken);
const enlistRequestChannelListener = di.inject(enlistRequestChannelListenerInjectionToken);
const computedInjectMany = di.inject(computedInjectManyInjectable);
const messageChannelListeners = computedInjectMany(messageChannelListenerInjectionToken);
const requestChannelListeners = computedInjectMany(requestChannelListenerInjectionToken);
return getStartableStoppable("listening-of-channels", () => {
const stopListeningOfMessageChannels = listening(
messageChannelListeners,
enlistMessageChannelListener,
(x) => x.id,
);
const stopListeningOfRequestChannels = listening(
requestChannelListeners,
enlistRequestChannelListener,
(x) => x.channel.id,
);
return () => {
stopListeningOfMessageChannels();
stopListeningOfRequestChannels();
};
});
},
injectionToken: listeningOfChannelsInjectionToken,
});
export default listeningOfChannelsInjectable;

View File

@ -6,7 +6,7 @@ import type {
} from "./message-channel-listener-injection-token"; } from "./message-channel-listener-injection-token";
export type EnlistMessageChannelListener = ( export type EnlistMessageChannelListener = (
listener: MessageChannelListener<MessageChannel<unknown>> listener: MessageChannelListener<MessageChannel<unknown>>,
) => () => void; ) => () => void;
export const enlistMessageChannelListenerInjectionToken = export const enlistMessageChannelListenerInjectionToken =

View File

@ -1,7 +1,5 @@
import type { MessageChannel } from "./message-channel-listener-injection-token"; import type { MessageChannel } from "./message-channel-listener-injection-token";
export const getMessageChannel = <Request>( export const getMessageChannel = <Request>(id: string): MessageChannel<Request> => ({
id: string
): MessageChannel<Request> => ({
id, id,
}); });

View File

@ -6,9 +6,7 @@ export interface MessageChannel<Message> {
_messageSignature?: Message; _messageSignature?: Message;
} }
export type MessageChannelHandler<Channel> = Channel extends MessageChannel< export type MessageChannelHandler<Channel> = Channel extends MessageChannel<infer Message>
infer Message
>
? (message: Message) => void ? (message: Message) => void
: never; : never;
@ -24,10 +22,7 @@ export const messageChannelListenerInjectionToken = getInjectionToken<
id: "message-channel-listener", id: "message-channel-listener",
}); });
export interface GetMessageChannelListenerInfo< export interface GetMessageChannelListenerInfo<Channel extends MessageChannel<Message>, Message> {
Channel extends MessageChannel<Message>,
Message
> {
id: string; id: string;
channel: Channel; channel: Channel;
getHandler: (di: DiContainerForInjection) => MessageChannelHandler<Channel>; getHandler: (di: DiContainerForInjection) => MessageChannelHandler<Channel>;
@ -36,9 +31,9 @@ export interface GetMessageChannelListenerInfo<
export const getMessageChannelListenerInjectable = < export const getMessageChannelListenerInjectable = <
Channel extends MessageChannel<Message>, Channel extends MessageChannel<Message>,
Message Message,
>( >(
info: GetMessageChannelListenerInfo<Channel, Message> info: GetMessageChannelListenerInfo<Channel, Message>,
) => ) =>
getInjectable({ getInjectable({
id: `${info.channel.id}-message-listener-${info.id}`, id: `${info.channel.id}-message-listener-${info.id}`,

View File

@ -6,7 +6,6 @@ export interface SendMessageToChannel {
<Message>(channel: MessageChannel<Message>, message: Message): void; <Message>(channel: MessageChannel<Message>, message: Message): void;
} }
export const sendMessageToChannelInjectionToken = export const sendMessageToChannelInjectionToken = getInjectionToken<SendMessageToChannel>({
getInjectionToken<SendMessageToChannel>({ id: "send-message-to-message-channel",
id: "send-message-to-message-channel", });
});

View File

@ -6,7 +6,7 @@ import type {
} from "./request-channel-listener-injection-token"; } from "./request-channel-listener-injection-token";
export type EnlistRequestChannelListener = ( export type EnlistRequestChannelListener = (
listener: RequestChannelListener<RequestChannel<unknown, unknown>> listener: RequestChannelListener<RequestChannel<unknown, unknown>>,
) => () => void; ) => () => void;
export const enlistRequestChannelListenerInjectionToken = export const enlistRequestChannelListenerInjectionToken =

View File

@ -1,7 +1,7 @@
import type { RequestChannel } from "./request-channel-listener-injection-token"; import type { RequestChannel } from "./request-channel-listener-injection-token";
export const getRequestChannel = <Request, Response>( export const getRequestChannel = <Request, Response>(
id: string id: string,
): RequestChannel<Request, Response> => ({ ): RequestChannel<Request, Response> => ({
id, id,
}); });

View File

@ -29,7 +29,7 @@ export const requestChannelListenerInjectionToken = getInjectionToken<
export interface GetRequestChannelListenerInjectableInfo< export interface GetRequestChannelListenerInjectableInfo<
Channel extends RequestChannel<Request, Response>, Channel extends RequestChannel<Request, Response>,
Request, Request,
Response Response,
> { > {
id: string; id: string;
channel: Channel; channel: Channel;
@ -39,9 +39,9 @@ export interface GetRequestChannelListenerInjectableInfo<
export const getRequestChannelListenerInjectable = < export const getRequestChannelListenerInjectable = <
Channel extends RequestChannel<Request, Response>, Channel extends RequestChannel<Request, Response>,
Request, Request,
Response Response,
>( >(
info: GetRequestChannelListenerInjectableInfo<Channel, Request, Response> info: GetRequestChannelListenerInjectableInfo<Channel, Request, Response>,
) => ) =>
getInjectable({ getInjectable({
id: `${info.channel.id}-request-listener-${info.id}`, id: `${info.channel.id}-request-listener-${info.id}`,

View File

@ -4,7 +4,7 @@ import type { RequestChannel } from "./request-channel-listener-injection-token"
export interface RequestFromChannel { export interface RequestFromChannel {
<Request, Response>( <Request, Response>(
channel: RequestChannel<Request, Response>, channel: RequestChannel<Request, Response>,
request: Request request: Request,
): Promise<Response>; ): Promise<Response>;
<Response>(channel: RequestChannel<void, Response>): Promise<Response>; <Response>(channel: RequestChannel<void, Response>): Promise<Response>;
} }
@ -16,7 +16,6 @@ export type ChannelRequester<Channel> = Channel extends RequestChannel<
? (req: Request) => Promise<Awaited<Response>> ? (req: Request) => Promise<Awaited<Response>>
: never; : never;
export const requestFromChannelInjectionToken = export const requestFromChannelInjectionToken = getInjectionToken<RequestFromChannel>({
getInjectionToken<RequestFromChannel>({ id: "request-from-request-channel",
id: "request-from-request-channel", });
});

View File

@ -12,9 +12,7 @@ export const messagingFeatureForUnitTesting = getFeature({
di, di,
targetModule: module, targetModule: module,
getRequireContexts: () => [ getRequireContexts: () => [require.context("./", true, /\.injectable\.(ts|tsx)$/)],
require.context("./", true, /\.injectable\.(ts|tsx)$/),
],
}); });
}, },
}); });

View File

@ -1,8 +1,4 @@
import { import { createContainer, DiContainer, Injectable } from "@ogre-tools/injectable";
createContainer,
DiContainer,
Injectable,
} from "@ogre-tools/injectable";
import asyncFn, { AsyncFnMock } from "@async-fn/jest"; import asyncFn, { AsyncFnMock } from "@async-fn/jest";
import { registerFeature } from "@k8slens/feature-core/src/register-feature"; import { registerFeature } from "@k8slens/feature-core/src/register-feature";
import { import {
@ -24,407 +20,357 @@ import { getRequestChannel } from "../../actual/request/get-request-channel";
import { startApplicationInjectionToken } from "@k8slens/application"; import { startApplicationInjectionToken } from "@k8slens/application";
import { messagingFeatureForUnitTesting } from "../feature"; import { messagingFeatureForUnitTesting } from "../feature";
[{ scenarioIsAsync: true }, { scenarioIsAsync: false }].forEach(
({ scenarioIsAsync }) =>
describe(`get-message-bridge-fake, given running as ${
scenarioIsAsync ? "async" : "sync"
}`, () => {
let messageBridgeFake: any;
beforeEach(() => {
messageBridgeFake = getMessageBridgeFake();
});
describe("given multiple DIs are involved", () => {
let someDi1: DiContainer;
let someDi2: DiContainer;
let someDiWithoutListeners: DiContainer;
beforeEach(async () => {
someDi1 = createContainer("some-di-1");
someDi2 = createContainer("some-di-2");
someDiWithoutListeners = createContainer("some-di-3");
registerMobX(someDi1);
registerMobX(someDi2);
registerMobX(someDiWithoutListeners);
runInAction(() => {
registerFeature(someDi1, messagingFeatureForUnitTesting);
registerFeature(someDi2, messagingFeatureForUnitTesting);
registerFeature(
someDiWithoutListeners,
messagingFeatureForUnitTesting
);
});
messageBridgeFake.involve(someDi1, someDi2, someDiWithoutListeners);
if (scenarioIsAsync) {
messageBridgeFake.setAsync(scenarioIsAsync);
}
await Promise.all([
someDi1.inject(startApplicationInjectionToken)(),
someDi2.inject(startApplicationInjectionToken)(),
someDiWithoutListeners.inject(startApplicationInjectionToken)(),
]);
});
describe("given there are message listeners", () => {
let someHandler1MockInDi1: jest.Mock;
let someHandler1MockInDi2: jest.Mock;
let someHandler2MockInDi2: jest.Mock;
let someListener1InDi2: Injectable<unknown, unknown>;
beforeEach(() => {
someHandler1MockInDi1 = jest.fn();
someHandler1MockInDi2 = jest.fn();
someHandler2MockInDi2 = jest.fn();
const someListener1InDi1 = getMessageChannelListenerInjectable({
id: "some-listener-in-di-1",
channel: someMessageChannel,
getHandler: () => someHandler1MockInDi1,
});
someListener1InDi2 = getMessageChannelListenerInjectable({
id: "some-listener-in-di-2",
channel: someMessageChannel,
getHandler: () => someHandler1MockInDi2,
});
const someListener2InDi2 = getMessageChannelListenerInjectable({
id: "some-listener-2-in-di-2",
channel: someMessageChannel,
getHandler: () => someHandler2MockInDi2,
});
runInAction(() => {
someDi1.register(someListener1InDi1);
someDi2.register(someListener1InDi2);
someDi2.register(someListener2InDi2);
});
});
describe("given there is a listener in di-2 that responds to a message with a message", () => {
beforeEach(() => {
const someResponder = getMessageChannelListenerInjectable({
id: "some-responder-di-2",
channel: someMessageChannel,
getHandler: (di) => {
const sendMessage = di.inject(
sendMessageToChannelInjectionToken
);
return (message) => {
sendMessage(
someMessageChannel,
`some-response-to: ${message}`
);
};
},
});
runInAction(() => {
someDi2.register(someResponder);
});
});
describe("given a message is sent in di-1", () => {
beforeEach(() => {
const sendMessageToChannelFromDi1 = someDi1.inject(
sendMessageToChannelInjectionToken
);
sendMessageToChannelFromDi1(someMessageChannel, "some-message");
});
describe(
scenarioIsAsync
? "when all message steps are propagated using a wrapper"
: "immediately",
() => {
let someWrapper: jest.Mock;
beforeEach((done) => {
someWrapper = jest.fn((propagation) => propagation());
if (scenarioIsAsync) {
messageBridgeFake
.messagePropagationRecursive(someWrapper)
.then(done);
} else {
done();
}
});
it("the response gets handled in di-1", () => {
expect(someHandler1MockInDi1).toHaveBeenCalledWith(
"some-response-to: some-message"
);
});
scenarioIsAsync &&
it("the wrapper gets called with the both propagations", () => {
expect(someWrapper).toHaveBeenCalledTimes(2);
});
}
);
describe(
scenarioIsAsync
? "when all message steps are propagated not using a wrapper"
: "immediately",
() => {
beforeEach((done) => {
if (scenarioIsAsync) {
messageBridgeFake
.messagePropagationRecursive()
.then(done);
} else {
done();
}
});
it("the response gets handled in di-1", () => {
expect(someHandler1MockInDi1).toHaveBeenCalledWith(
"some-response-to: some-message"
);
});
}
);
});
});
describe("when sending message in a DI", () => {
beforeEach(() => {
const sendMessageToChannelFromDi1 = someDi1.inject(
sendMessageToChannelInjectionToken
);
sendMessageToChannelFromDi1(someMessageChannel, "some-message");
});
it("listener in sending DI does not handle the message", () => {
expect(someHandler1MockInDi1).not.toHaveBeenCalled();
});
scenarioIsAsync &&
it("listeners in other than sending DIs do not handle the message yet", () => {
expect(someHandler1MockInDi2).not.toHaveBeenCalled();
expect(someHandler2MockInDi2).not.toHaveBeenCalled();
});
describe(
scenarioIsAsync ? "when messages are propagated" : "immediately",
() => {
beforeEach((done) => {
if (scenarioIsAsync) {
messageBridgeFake.messagePropagation().then(done);
} else {
done();
}
});
it("listeners in other than sending DIs handle the message", () => {
expect(someHandler1MockInDi2).toHaveBeenCalledWith(
"some-message"
);
expect(someHandler2MockInDi2).toHaveBeenCalledWith(
"some-message"
);
});
}
);
scenarioIsAsync &&
describe("when messages are propagated using a wrapper, such as act() in react testing lib", () => {
let someWrapper: jest.Mock;
beforeEach(async () => {
someWrapper = jest.fn((observation) => observation());
await messageBridgeFake.messagePropagation(someWrapper);
});
it("the wrapper gets called with the related propagation", async () => {
expect(someWrapper).toHaveBeenCalledTimes(1);
});
it("listeners still handle the message", () => {
expect(someHandler1MockInDi2).toHaveBeenCalledWith(
"some-message"
);
expect(someHandler2MockInDi2).toHaveBeenCalledWith(
"some-message"
);
});
});
});
it("given a listener is deregistered, when sending message, deregistered listener does not handle the message", () => {
runInAction(() => {
someDi2.deregister(someListener1InDi2);
});
const sendMessageToChannelFromDi1 = someDi1.inject(
sendMessageToChannelInjectionToken
);
someHandler1MockInDi2.mockClear();
sendMessageToChannelFromDi1(someMessageChannel, "irrelevant");
expect(someHandler1MockInDi2).not.toHaveBeenCalled();
});
});
describe("given there are request listeners", () => {
let someHandler1MockInDi1: AsyncFnMock<
(message: string) => Promise<number>
>;
let someHandler1MockInDi2: AsyncFnMock<
(message: string) => Promise<number>
>;
let someListener1InDi2: Injectable<unknown, unknown>;
let actualPromise: Promise<number>;
beforeEach(() => {
someHandler1MockInDi1 = asyncFn();
someHandler1MockInDi2 = asyncFn();
const someListener1InDi1 = getRequestChannelListenerInjectable({
id: "some-request-listener-in-di-1",
channel: someOtherRequestChannel,
getHandler: () => someHandler1MockInDi1,
});
someListener1InDi2 = getRequestChannelListenerInjectable({
id: "some-request-listener-in-di-2",
channel: someRequestChannel,
getHandler: () => someHandler1MockInDi2,
});
runInAction(() => {
someDi1.register(someListener1InDi1);
someDi2.register(someListener1InDi2);
});
});
describe("when requesting from a channel in a DI", () => {
beforeEach(() => {
const requestFromChannelFromDi1 = someDi1.inject(
requestFromChannelInjectionToken
);
actualPromise = requestFromChannelFromDi1(
someRequestChannel,
"some-request"
);
});
it("listener in requesting DI does not handle the request", () => {
expect(someHandler1MockInDi1).not.toHaveBeenCalled();
});
it("the listener in other than requesting DIs handle the request", () => {
expect(someHandler1MockInDi2).toHaveBeenCalledWith(
"some-request"
);
});
it("does not resolve yet", async () => {
const promiseStatus = await getPromiseStatus(actualPromise);
expect(promiseStatus.fulfilled).toBe(false);
});
it("when handle resolves, resolves with response", async () => {
await someHandler1MockInDi2.resolve(42);
const actual = await actualPromise;
expect(actual).toBe(42);
});
});
it("given a listener is deregistered, when requesting, deregistered listener does not handle the request", () => {
runInAction(() => {
someDi2.deregister(someListener1InDi2);
});
const sendMessageToChannelFromDi1 = someDi1.inject(
sendMessageToChannelInjectionToken
);
someHandler1MockInDi2.mockClear();
sendMessageToChannelFromDi1(someMessageChannel, "irrelevant");
expect(someHandler1MockInDi2).not.toHaveBeenCalled();
});
it("given there are multiple listeners between different DIs for same channel, when requesting, throws", () => {
const someConflictingListenerInjectable =
getRequestChannelListenerInjectable({
id: "conflicting-listener",
channel: someRequestChannel,
getHandler: () => () => 84,
});
runInAction(() => {
someDi1.register(someConflictingListenerInjectable);
});
const requestFromChannelFromDi2 = someDi2.inject(
requestFromChannelInjectionToken
);
return expect(() =>
requestFromChannelFromDi2(someRequestChannel, "irrelevant")
).rejects.toThrow(
'Tried to make a request but multiple listeners were discovered for channel "some-request-channel" in multiple DIs.'
);
});
it("when requesting from channel without listener, throws", () => {
const requestFromChannel = someDi1.inject(
requestFromChannelInjectionToken
);
return expect(() =>
requestFromChannel(
someRequestChannelWithoutListeners,
"irrelevant"
)
).rejects.toThrow(
'Tried to make a request but no listeners for channel "some-request-channel-without-listeners" was discovered in any DIs'
);
});
});
});
})
);
type SomeMessageChannel = MessageChannel<string>; type SomeMessageChannel = MessageChannel<string>;
type SomeRequestChannel = RequestChannel<string, number>; type SomeRequestChannel = RequestChannel<string, number>;
const someMessageChannel: SomeMessageChannel = getMessageChannel( const someMessageChannel: SomeMessageChannel = getMessageChannel("some-message-channel");
"some-message-channel" const someRequestChannel: SomeRequestChannel = getRequestChannel("some-request-channel");
);
const someRequestChannel: SomeRequestChannel = getRequestChannel(
"some-request-channel"
);
const someOtherRequestChannel: SomeRequestChannel = { const someOtherRequestChannel: SomeRequestChannel = {
id: "some-other-request-channel", id: "some-other-request-channel",
}; };
const someRequestChannelWithoutListeners: SomeRequestChannel = { const someRequestChannelWithoutListeners: SomeRequestChannel = {
id: "some-request-channel-without-listeners", id: "some-request-channel-without-listeners",
}; };
[{ scenarioIsAsync: true }, { scenarioIsAsync: false }].forEach(({ scenarioIsAsync }) =>
describe(`get-message-bridge-fake, given running as ${
scenarioIsAsync ? "async" : "sync"
}`, () => {
let messageBridgeFake: any;
beforeEach(() => {
messageBridgeFake = getMessageBridgeFake();
});
describe("given multiple DIs are involved", () => {
let someDi1: DiContainer;
let someDi2: DiContainer;
let someDiWithoutListeners: DiContainer;
beforeEach(async () => {
someDi1 = createContainer("some-di-1");
someDi2 = createContainer("some-di-2");
someDiWithoutListeners = createContainer("some-di-3");
registerMobX(someDi1);
registerMobX(someDi2);
registerMobX(someDiWithoutListeners);
runInAction(() => {
registerFeature(someDi1, messagingFeatureForUnitTesting);
registerFeature(someDi2, messagingFeatureForUnitTesting);
registerFeature(someDiWithoutListeners, messagingFeatureForUnitTesting);
});
messageBridgeFake.involve(someDi1, someDi2, someDiWithoutListeners);
if (scenarioIsAsync) {
messageBridgeFake.setAsync(scenarioIsAsync);
}
await Promise.all([
someDi1.inject(startApplicationInjectionToken)(),
someDi2.inject(startApplicationInjectionToken)(),
someDiWithoutListeners.inject(startApplicationInjectionToken)(),
]);
});
describe("given there are message listeners", () => {
let someHandler1MockInDi1: jest.Mock;
let someHandler1MockInDi2: jest.Mock;
let someHandler2MockInDi2: jest.Mock;
let someListener1InDi2: Injectable<unknown, unknown>;
beforeEach(() => {
someHandler1MockInDi1 = jest.fn();
someHandler1MockInDi2 = jest.fn();
someHandler2MockInDi2 = jest.fn();
const someListener1InDi1 = getMessageChannelListenerInjectable({
id: "some-listener-in-di-1",
channel: someMessageChannel,
getHandler: () => someHandler1MockInDi1,
});
someListener1InDi2 = getMessageChannelListenerInjectable({
id: "some-listener-in-di-2",
channel: someMessageChannel,
getHandler: () => someHandler1MockInDi2,
});
const someListener2InDi2 = getMessageChannelListenerInjectable({
id: "some-listener-2-in-di-2",
channel: someMessageChannel,
getHandler: () => someHandler2MockInDi2,
});
runInAction(() => {
someDi1.register(someListener1InDi1);
someDi2.register(someListener1InDi2);
someDi2.register(someListener2InDi2);
});
});
describe("given there is a listener in di-2 that responds to a message with a message", () => {
beforeEach(() => {
const someResponder = getMessageChannelListenerInjectable({
id: "some-responder-di-2",
channel: someMessageChannel,
getHandler: (di) => {
const sendMessage = di.inject(sendMessageToChannelInjectionToken);
return (message) => {
sendMessage(someMessageChannel, `some-response-to: ${message}`);
};
},
});
runInAction(() => {
someDi2.register(someResponder);
});
});
describe("given a message is sent in di-1", () => {
beforeEach(() => {
const sendMessageToChannelFromDi1 = someDi1.inject(
sendMessageToChannelInjectionToken,
);
sendMessageToChannelFromDi1(someMessageChannel, "some-message");
});
const scenarioTitle = scenarioIsAsync
? "when all message steps are propagated using a wrapper"
: "immediately";
// eslint-disable-next-line jest/valid-title
describe(scenarioTitle, () => {
let someWrapper: jest.Mock;
beforeEach((done) => {
someWrapper = jest.fn((propagation) => propagation());
if (scenarioIsAsync) {
messageBridgeFake.messagePropagationRecursive(someWrapper).then(done);
} else {
done();
}
});
it("the response gets handled in di-1", () => {
expect(someHandler1MockInDi1).toHaveBeenCalledWith(
"some-response-to: some-message",
);
});
scenarioIsAsync &&
it("the wrapper gets called with the both propagations", () => {
expect(someWrapper).toHaveBeenCalledTimes(2);
});
});
const scenarioName: string = scenarioIsAsync
? "when all message steps are propagated not using a wrapper"
: "immediately";
// eslint-disable-next-line jest/valid-title
describe(scenarioName, () => {
beforeEach((done) => {
if (scenarioIsAsync) {
messageBridgeFake.messagePropagationRecursive().then(done);
} else {
done();
}
});
it("the response gets handled in di-1", () => {
expect(someHandler1MockInDi1).toHaveBeenCalledWith(
"some-response-to: some-message",
);
});
});
});
});
describe("when sending message in a DI", () => {
beforeEach(() => {
const sendMessageToChannelFromDi1 = someDi1.inject(sendMessageToChannelInjectionToken);
sendMessageToChannelFromDi1(someMessageChannel, "some-message");
});
it("listener in sending DI does not handle the message", () => {
expect(someHandler1MockInDi1).not.toHaveBeenCalled();
});
scenarioIsAsync &&
it("listeners in other than sending DIs do not handle the message yet", () => {
expect(someHandler1MockInDi2).not.toHaveBeenCalled();
expect(someHandler2MockInDi2).not.toHaveBeenCalled();
});
const scenarioName = scenarioIsAsync ? "when messages are propagated" : "immediately";
// eslint-disable-next-line jest/valid-title
describe(scenarioName, () => {
beforeEach((done) => {
if (scenarioIsAsync) {
messageBridgeFake.messagePropagation().then(done);
} else {
done();
}
});
it("listeners in other than sending DIs handle the message", () => {
expect(someHandler1MockInDi2).toHaveBeenCalledWith("some-message");
expect(someHandler2MockInDi2).toHaveBeenCalledWith("some-message");
});
});
scenarioIsAsync &&
describe("when messages are propagated using a wrapper, such as act() in react testing lib", () => {
let someWrapper: jest.Mock;
beforeEach(async () => {
someWrapper = jest.fn((observation) => observation());
await messageBridgeFake.messagePropagation(someWrapper);
});
it("the wrapper gets called with the related propagation", async () => {
expect(someWrapper).toHaveBeenCalledTimes(1);
});
it("listeners still handle the message", () => {
expect(someHandler1MockInDi2).toHaveBeenCalledWith("some-message");
expect(someHandler2MockInDi2).toHaveBeenCalledWith("some-message");
});
});
});
it("given a listener is deregistered, when sending message, deregistered listener does not handle the message", () => {
runInAction(() => {
someDi2.deregister(someListener1InDi2);
});
const sendMessageToChannelFromDi1 = someDi1.inject(sendMessageToChannelInjectionToken);
someHandler1MockInDi2.mockClear();
sendMessageToChannelFromDi1(someMessageChannel, "irrelevant");
expect(someHandler1MockInDi2).not.toHaveBeenCalled();
});
});
describe("given there are request listeners", () => {
let someHandler1MockInDi1: AsyncFnMock<(message: string) => Promise<number>>;
let someHandler1MockInDi2: AsyncFnMock<(message: string) => Promise<number>>;
let someListener1InDi2: Injectable<unknown, unknown>;
let actualPromise: Promise<number>;
beforeEach(() => {
someHandler1MockInDi1 = asyncFn();
someHandler1MockInDi2 = asyncFn();
const someListener1InDi1 = getRequestChannelListenerInjectable({
id: "some-request-listener-in-di-1",
channel: someOtherRequestChannel,
getHandler: () => someHandler1MockInDi1,
});
someListener1InDi2 = getRequestChannelListenerInjectable({
id: "some-request-listener-in-di-2",
channel: someRequestChannel,
getHandler: () => someHandler1MockInDi2,
});
runInAction(() => {
someDi1.register(someListener1InDi1);
someDi2.register(someListener1InDi2);
});
});
describe("when requesting from a channel in a DI", () => {
beforeEach(() => {
const requestFromChannelFromDi1 = someDi1.inject(requestFromChannelInjectionToken);
actualPromise = requestFromChannelFromDi1(someRequestChannel, "some-request");
});
it("listener in requesting DI does not handle the request", () => {
expect(someHandler1MockInDi1).not.toHaveBeenCalled();
});
it("the listener in other than requesting DIs handle the request", () => {
expect(someHandler1MockInDi2).toHaveBeenCalledWith("some-request");
});
it("does not resolve yet", async () => {
const promiseStatus = await getPromiseStatus(actualPromise);
expect(promiseStatus.fulfilled).toBe(false);
});
it("when handle resolves, resolves with response", async () => {
await someHandler1MockInDi2.resolve(42);
const actual = await actualPromise;
expect(actual).toBe(42);
});
});
it("given a listener is deregistered, when requesting, deregistered listener does not handle the request", () => {
runInAction(() => {
someDi2.deregister(someListener1InDi2);
});
const sendMessageToChannelFromDi1 = someDi1.inject(sendMessageToChannelInjectionToken);
someHandler1MockInDi2.mockClear();
sendMessageToChannelFromDi1(someMessageChannel, "irrelevant");
expect(someHandler1MockInDi2).not.toHaveBeenCalled();
});
it("given there are multiple listeners between different DIs for same channel, when requesting, throws", () => {
const someConflictingListenerInjectable = getRequestChannelListenerInjectable({
id: "conflicting-listener",
channel: someRequestChannel,
getHandler: () => () => 84,
});
runInAction(() => {
someDi1.register(someConflictingListenerInjectable);
});
const requestFromChannelFromDi2 = someDi2.inject(requestFromChannelInjectionToken);
return expect(() =>
requestFromChannelFromDi2(someRequestChannel, "irrelevant"),
).rejects.toThrow(
'Tried to make a request but multiple listeners were discovered for channel "some-request-channel" in multiple DIs.',
);
});
it("when requesting from channel without listener, throws", () => {
const requestFromChannel = someDi1.inject(requestFromChannelInjectionToken);
return expect(() =>
requestFromChannel(someRequestChannelWithoutListeners, "irrelevant"),
).rejects.toThrow(
'Tried to make a request but no listeners for channel "some-request-channel-without-listeners" was discovered in any DIs',
);
});
});
});
}),
);

View File

@ -20,6 +20,126 @@ export type MessageBridgeFake = {
setAsync: (value: boolean) => void; setAsync: (value: boolean) => void;
}; };
const overrideMessaging = ({
di,
messageListenersByDi,
messagePropagationBuffer,
getAsyncModeStatus,
}: {
di: DiContainer;
messageListenersByDi: Map<DiContainer, Map<string, Set<MessageChannelHandler<Channel>>>>;
messagePropagationBuffer: Set<{ resolve: () => Promise<void> }>;
getAsyncModeStatus: () => boolean;
}) => {
const messageHandlersByChannel = new Map<string, Set<MessageChannelHandler<Channel>>>();
messageListenersByDi.set(di, messageHandlersByChannel);
di.override(sendMessageToChannelInjectionToken, () => (channel, message) => {
const allOtherDis = [...messageListenersByDi.keys()].filter((x) => x !== di);
allOtherDis.forEach((otherDi) => {
const listeners = messageListenersByDi.get(otherDi);
const handlersForChannel = listeners?.get(channel.id);
if (!handlersForChannel) {
return;
}
if (getAsyncModeStatus()) {
const resolvableHandlePromise = asyncFn();
resolvableHandlePromise().then(() => {
handlersForChannel.forEach((handler) => handler(message));
});
messagePropagationBuffer.add(resolvableHandlePromise);
} else {
handlersForChannel.forEach((handler) => handler(message));
}
});
});
di.override(enlistMessageChannelListenerInjectionToken, () => (listener) => {
if (!messageHandlersByChannel.has(listener.channel.id)) {
messageHandlersByChannel.set(listener.channel.id, new Set());
}
const handlerSet = messageHandlersByChannel.get(listener.channel.id);
handlerSet?.add(listener.handler);
return () => {
handlerSet?.delete(listener.handler);
};
});
};
const overrideRequesting = ({
di,
requestListenersByDi,
}: {
di: DiContainer;
requestListenersByDi: Map<DiContainer, Map<string, Set<MessageChannelHandler<Channel>>>>;
}) => {
const requestHandlersByChannel = new Map<string, Set<RequestChannelHandler<Channel>>>();
requestListenersByDi.set(di, requestHandlersByChannel);
di.override(
requestFromChannelInjectionToken,
() =>
(async (channel, request) =>
pipeline(
[...requestListenersByDi.values()],
map((listenersByChannel) => listenersByChannel?.get(channel.id)),
filter((x) => !!x),
(channelSpecificListeners) => {
if (channelSpecificListeners.length === 0) {
throw new Error(
`Tried to make a request but no listeners for channel "${channel.id}" was discovered in any DIs`,
);
}
if (channelSpecificListeners.length > 1) {
throw new Error(
`Tried to make a request but multiple listeners were discovered for channel "${channel.id}" in multiple DIs.`,
);
}
const listeners = channelSpecificListeners[0];
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const [handler] = listeners!;
return handler;
},
async (handler) => handler(request),
)) as RequestFromChannel,
);
di.override(enlistRequestChannelListenerInjectionToken, () => (listener) => {
if (!requestHandlersByChannel.has(listener.channel.id)) {
requestHandlersByChannel.set(listener.channel.id, new Set());
}
const handlerSet = requestHandlersByChannel.get(listener.channel.id);
handlerSet?.add(listener.handler);
return () => {
handlerSet?.delete(listener.handler);
};
});
};
export const getMessageBridgeFake = (): MessageBridgeFake => { export const getMessageBridgeFake = (): MessageBridgeFake => {
const messageListenersByDi = new Map< const messageListenersByDi = new Map<
DiContainer, DiContainer,
@ -33,16 +153,15 @@ export const getMessageBridgeFake = (): MessageBridgeFake => {
const messagePropagationBuffer = new Set<AsyncFnMock<() => void>>(); const messagePropagationBuffer = new Set<AsyncFnMock<() => void>>();
const messagePropagation = async ( const messagePropagation = async (wrapper: (callback: any) => any = (callback) => callback()) => {
wrapper: (callback: any) => any = (callback) => callback()
) => {
const oldMessages = [...messagePropagationBuffer.values()]; const oldMessages = [...messagePropagationBuffer.values()];
messagePropagationBuffer.clear(); messagePropagationBuffer.clear();
await Promise.all(oldMessages.map((x) => wrapper(x.resolve))); await Promise.all(oldMessages.map((x) => wrapper(x.resolve)));
}; };
const messagePropagationRecursive = async ( const messagePropagationRecursive = async (
wrapper: (callback: any) => any = (callback) => callback() wrapper: (callback: any) => any = (callback) => callback(),
) => { ) => {
while (messagePropagationBuffer.size) { while (messagePropagationBuffer.size) {
await messagePropagation(wrapper); await messagePropagation(wrapper);
@ -75,136 +194,3 @@ export const getMessageBridgeFake = (): MessageBridgeFake => {
}, },
}; };
}; };
const overrideMessaging = ({
di,
messageListenersByDi,
messagePropagationBuffer,
getAsyncModeStatus,
}: {
di: DiContainer;
messageListenersByDi: Map<
DiContainer,
Map<string, Set<MessageChannelHandler<Channel>>>
>;
messagePropagationBuffer: Set<{ resolve: () => Promise<void> }>;
getAsyncModeStatus: () => boolean;
}) => {
const messageHandlersByChannel = new Map<
string,
Set<MessageChannelHandler<Channel>>
>();
messageListenersByDi.set(di, messageHandlersByChannel);
di.override(sendMessageToChannelInjectionToken, () => (channel, message) => {
const allOtherDis = [...messageListenersByDi.keys()].filter(
(x) => x !== di
);
allOtherDis.forEach((otherDi) => {
const listeners = messageListenersByDi.get(otherDi);
const handlersForChannel = listeners!.get(channel.id);
if (!handlersForChannel) {
return;
}
if (getAsyncModeStatus()) {
const resolvableHandlePromise = asyncFn();
resolvableHandlePromise().then(() => {
handlersForChannel.forEach((handler) => handler(message));
});
messagePropagationBuffer.add(resolvableHandlePromise);
} else {
handlersForChannel.forEach((handler) => handler(message));
}
});
});
di.override(enlistMessageChannelListenerInjectionToken, () => (listener) => {
if (!messageHandlersByChannel.has(listener.channel.id)) {
messageHandlersByChannel.set(listener.channel.id, new Set());
}
const handlerSet = messageHandlersByChannel.get(listener.channel.id);
handlerSet!.add(listener.handler);
return () => {
handlerSet!.delete(listener.handler);
};
});
};
const overrideRequesting = ({
di,
requestListenersByDi,
}: {
di: DiContainer;
requestListenersByDi: Map<
DiContainer,
Map<string, Set<MessageChannelHandler<Channel>>>
>;
}) => {
const requestHandlersByChannel = new Map<
string,
Set<RequestChannelHandler<Channel>>
>();
requestListenersByDi.set(di, requestHandlersByChannel);
di.override(
requestFromChannelInjectionToken,
() =>
(async (channel, request) =>
await pipeline(
[...requestListenersByDi.values()],
map((listenersByChannel) => listenersByChannel!.get(channel.id)),
filter((x) => !!x),
(channelSpecificListeners) => {
if (channelSpecificListeners.length === 0) {
throw new Error(
`Tried to make a request but no listeners for channel "${channel.id}" was discovered in any DIs`
);
}
if (channelSpecificListeners.length > 1) {
throw new Error(
`Tried to make a request but multiple listeners were discovered for channel "${channel.id}" in multiple DIs.`
);
}
const listeners = channelSpecificListeners[0];
const [handler] = listeners!;
return handler;
},
async (handler) => await handler(request)
)) as RequestFromChannel
);
di.override(enlistRequestChannelListenerInjectionToken, () => (listener) => {
if (!requestHandlersByChannel.has(listener.channel.id)) {
requestHandlersByChannel.set(listener.channel.id, new Set());
}
const handlerSet = requestHandlersByChannel.get(listener.channel.id);
handlerSet!.add(listener.handler);
return () => {
handlerSet!.delete(listener.handler);
};
});
};

View File

@ -1,8 +1,4 @@
import { import { createContainer, DiContainer, Injectable } from "@ogre-tools/injectable";
createContainer,
DiContainer,
Injectable,
} from "@ogre-tools/injectable";
import { registerFeature } from "@k8slens/feature-core"; import { registerFeature } from "@k8slens/feature-core";
import { registerMobX } from "@ogre-tools/injectable-extension-for-mobx"; import { registerMobX } from "@ogre-tools/injectable-extension-for-mobx";
@ -86,7 +82,7 @@ describe("listening-of-messages", () => {
await startApplication(); await startApplication();
}); });
it("it enlists a listener for the channel", () => { it("enlists a listener for the channel", () => {
expect(enlistMessageChannelListenerMock).toHaveBeenCalledWith({ expect(enlistMessageChannelListenerMock).toHaveBeenCalledWith({
id: "some-channel-id-message-listener-some-listener", id: "some-channel-id-message-listener-some-listener",
channel: someChannel, channel: someChannel,

View File

@ -90,7 +90,7 @@ describe("listening-of-requests", () => {
await startApplication(); await startApplication();
}); });
it("it enlists a listener for the channel", () => { it("enlists a listener for the channel", () => {
expect(enlistRequestChannelListenerMock).toHaveBeenCalledWith({ expect(enlistRequestChannelListenerMock).toHaveBeenCalledWith({
id: "some-channel-id-request-listener-some-listener", id: "some-channel-id-request-listener-some-listener",
channel: someChannel, channel: someChannel,

View File

@ -1,4 +1,4 @@
{ {
"extends": "@k8slens/typescript/config/base.json", "extends": "@k8slens/typescript/config/base.json",
"include": ["**/*.ts"] "include": ["**/*.ts", "**/*.tsx"]
} }

View File

@ -1,2 +1 @@
module.exports = module.exports = require("@k8slens/jest").monorepoPackageConfig(__dirname).configForNode;
require("@k8slens/jest").monorepoPackageConfig(__dirname).configForNode;

View File

@ -29,9 +29,7 @@ describe("enlist message channel listener in main", () => {
di.override(ipcMainInjectable, () => ipcMainStub); di.override(ipcMainInjectable, () => ipcMainStub);
enlistMessageChannelListener = di.inject( enlistMessageChannelListener = di.inject(enlistMessageChannelListenerInjectionToken);
enlistMessageChannelListenerInjectionToken
);
}); });
describe("when called", () => { describe("when called", () => {
@ -53,10 +51,7 @@ describe("enlist message channel listener in main", () => {
}); });
it("registers the listener", () => { it("registers the listener", () => {
expect(onMock).toHaveBeenCalledWith( expect(onMock).toHaveBeenCalledWith("some-channel-id", expect.any(Function));
"some-channel-id",
expect.any(Function)
);
}); });
it("does not de-register the listener yet", () => { it("does not de-register the listener yet", () => {
@ -75,10 +70,7 @@ describe("enlist message channel listener in main", () => {
it("when disposing the listener, de-registers the listener", () => { it("when disposing the listener, de-registers the listener", () => {
disposer(); disposer();
expect(offMock).toHaveBeenCalledWith( expect(offMock).toHaveBeenCalledWith("some-channel-id", expect.any(Function));
"some-channel-id",
expect.any(Function)
);
}); });
}); });

View File

@ -1,16 +1,11 @@
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import type { IpcMainInvokeEvent } from "electron"; import type { IpcMainInvokeEvent } from "electron";
import ipcMainInjectable from "../ipc-main/ipc-main.injectable"; import ipcMainInjectable from "../ipc-main/ipc-main.injectable";
import type { import type { RequestChannel, RequestChannelListener } from "@k8slens/messaging";
RequestChannel,
RequestChannelListener,
} from "@k8slens/messaging";
import { enlistRequestChannelListenerInjectionToken } from "@k8slens/messaging"; import { enlistRequestChannelListenerInjectionToken } from "@k8slens/messaging";
export type EnlistRequestChannelListener = < export type EnlistRequestChannelListener = <TChannel extends RequestChannel<unknown, unknown>>(
TChannel extends RequestChannel<unknown, unknown> listener: RequestChannelListener<TChannel>,
>(
listener: RequestChannelListener<TChannel>
) => () => void; ) => () => void;
const enlistRequestChannelListenerInjectable = getInjectable({ const enlistRequestChannelListenerInjectable = getInjectable({
@ -20,8 +15,7 @@ const enlistRequestChannelListenerInjectable = getInjectable({
const ipcMain = di.inject(ipcMainInjectable); const ipcMain = di.inject(ipcMainInjectable);
return ({ channel, handler }) => { return ({ channel, handler }) => {
const nativeHandleCallback = (_: IpcMainInvokeEvent, request: unknown) => const nativeHandleCallback = (_: IpcMainInvokeEvent, request: unknown) => handler(request);
handler(request);
ipcMain.handle(channel.id, nativeHandleCallback); ipcMain.handle(channel.id, nativeHandleCallback);

View File

@ -37,9 +37,7 @@ describe("enlist request channel listener in main", () => {
di.override(ipcMainInjectable, () => ipcMainStub); di.override(ipcMainInjectable, () => ipcMainStub);
enlistRequestChannelListener = di.inject( enlistRequestChannelListener = di.inject(enlistRequestChannelListenerInjectable);
enlistRequestChannelListenerInjectable
);
}); });
describe("when called", () => { describe("when called", () => {
@ -61,10 +59,7 @@ describe("enlist request channel listener in main", () => {
}); });
it("registers the listener", () => { it("registers the listener", () => {
expect(handleMock).toHaveBeenCalledWith( expect(handleMock).toHaveBeenCalledWith("some-channel-id", expect.any(Function));
"some-channel-id",
expect.any(Function)
);
}); });
it("does not de-register the listener yet", () => { it("does not de-register the listener yet", () => {
@ -75,10 +70,7 @@ describe("enlist request channel listener in main", () => {
let actualPromise: Promise<any>; let actualPromise: Promise<any>;
beforeEach(() => { beforeEach(() => {
actualPromise = handleMock.mock.calls[0][1]( actualPromise = handleMock.mock.calls[0][1]({} as IpcMainInvokeEvent, "some-request");
{} as IpcMainInvokeEvent,
"some-request"
);
}); });
it("calls the handler with the request", () => { it("calls the handler with the request", () => {
@ -105,10 +97,7 @@ describe("enlist request channel listener in main", () => {
it("when disposing the listener, de-registers the listener", () => { it("when disposing the listener, de-registers the listener", () => {
disposer(); disposer();
expect(offMock).toHaveBeenCalledWith( expect(offMock).toHaveBeenCalledWith("some-channel-id", expect.any(Function));
"some-channel-id",
expect.any(Function)
);
}); });
}); });

View File

@ -9,9 +9,7 @@ export const messagingFeatureForMain = getFeature({
di, di,
targetModule: module, targetModule: module,
getRequireContexts: () => [ getRequireContexts: () => [require.context("./", true, /\.injectable\.(ts|tsx)$/)],
require.context("./", true, /\.injectable\.(ts|tsx)$/),
],
}); });
}, },
}); });

View File

@ -1,2 +1 @@
module.exports = module.exports = require("@k8slens/jest").monorepoPackageConfig(__dirname).configForNode;
require("@k8slens/jest").monorepoPackageConfig(__dirname).configForNode;

View File

@ -9,9 +9,7 @@ export const messagingFeatureForRenderer = getFeature({
di, di,
targetModule: module, targetModule: module,
getRequireContexts: () => [ getRequireContexts: () => [require.context("./", true, /\.injectable\.(ts|tsx)$/)],
require.context("./", true, /\.injectable\.(ts|tsx)$/),
],
}); });
}, },
}); });

View File

@ -29,9 +29,7 @@ describe("enlist message channel listener in renderer", () => {
di.override(ipcRendererInjectable, () => ipcRendererStub); di.override(ipcRendererInjectable, () => ipcRendererStub);
enlistMessageChannelListener = di.inject( enlistMessageChannelListener = di.inject(enlistMessageChannelListenerInjectionToken);
enlistMessageChannelListenerInjectionToken
);
}); });
describe("when called", () => { describe("when called", () => {
@ -53,10 +51,7 @@ describe("enlist message channel listener in renderer", () => {
}); });
it("registers the listener", () => { it("registers the listener", () => {
expect(onMock).toHaveBeenCalledWith( expect(onMock).toHaveBeenCalledWith("some-channel-id", expect.any(Function));
"some-channel-id",
expect.any(Function)
);
}); });
it("does not de-register the listener yet", () => { it("does not de-register the listener yet", () => {
@ -75,10 +70,7 @@ describe("enlist message channel listener in renderer", () => {
it("when disposing the listener, de-registers the listener", () => { it("when disposing the listener, de-registers the listener", () => {
disposer(); disposer();
expect(offMock).toHaveBeenCalledWith( expect(offMock).toHaveBeenCalledWith("some-channel-id", expect.any(Function));
"some-channel-id",
expect.any(Function)
);
}); });
}); });

View File

@ -9,8 +9,7 @@ const requestFromChannelInjectable = getInjectable({
instantiate: (di) => { instantiate: (di) => {
const invokeIpc = di.inject(invokeIpcInjectable); const invokeIpc = di.inject(invokeIpcInjectable);
return ((channel, request) => return ((channel, request) => invokeIpc(channel.id, request)) as RequestFromChannel;
invokeIpc(channel.id, request)) as RequestFromChannel;
}, },
injectionToken: requestFromChannelInjectionToken, injectionToken: requestFromChannelInjectionToken,

View File

@ -34,10 +34,7 @@ describe("request-from-channel", () => {
}); });
it("invokes ipcRenderer of Electron", () => { it("invokes ipcRenderer of Electron", () => {
expect(invokeIpcMock).toHaveBeenCalledWith( expect(invokeIpcMock).toHaveBeenCalledWith("some-channel-id", "some-request-payload");
"some-channel-id",
"some-request-payload"
);
}); });
it("when invoke resolves with response, resolves with said response", async () => { it("when invoke resolves with response, resolves with said response", async () => {

View File

@ -1,9 +1,6 @@
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import sendToIpcInjectable from "./send-to-ipc.injectable"; import sendToIpcInjectable from "./send-to-ipc.injectable";
import { import { SendMessageToChannel, sendMessageToChannelInjectionToken } from "@k8slens/messaging";
SendMessageToChannel,
sendMessageToChannelInjectionToken,
} from "@k8slens/messaging";
const messageToChannelInjectable = getInjectable({ const messageToChannelInjectable = getInjectable({
id: "message-to-channel", id: "message-to-channel",

View File

@ -22,9 +22,7 @@ describe("message-from-channel", () => {
describe("when called", () => { describe("when called", () => {
beforeEach(() => { beforeEach(() => {
const sendMessageToChannel = di.inject( const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
sendMessageToChannelInjectionToken
);
const someChannel: MessageChannel<number> = { const someChannel: MessageChannel<number> = {
id: "some-channel-id", id: "some-channel-id",