Skip to content

Commit

Permalink
test: add DialogManager tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinCupela committed Sep 9, 2024
1 parent 8d1c98a commit 9f071e4
Show file tree
Hide file tree
Showing 3 changed files with 386 additions and 21 deletions.
56 changes: 37 additions & 19 deletions src/components/Dialog/DialogsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ type DialogId = string;

export type GetOrCreateParams = {
id: DialogId;
isOpen?: boolean;
};

export type Dialog = {
Expand All @@ -15,7 +14,7 @@ export type Dialog = {
toggleSingle: () => void;
};

type DialogEvent = { type: 'close' | 'open' | 'openCountChange' };
type DialogEvent = { type: 'close' | 'open' };

const dialogsManagerEvents = ['openCountChange'] as const;
type DialogsManagerEvent = { type: typeof dialogsManagerEvents[number] };
Expand Down Expand Up @@ -46,15 +45,15 @@ export class DialogsManager {
this.id = id ?? new Date().getTime().toString();
}

getOrCreate({ id, isOpen = false }: GetOrCreateParams) {
getOrCreate({ id }: GetOrCreateParams) {
let dialog = this.dialogs[id];
if (!dialog) {
dialog = {
close: () => {
this.close(id);
},
id,
isOpen,
isOpen: false,
open: () => {
this.open({ id });
},
Expand Down Expand Up @@ -88,10 +87,11 @@ export class DialogsManager {
if (!id) return noop;

if (!this.dialogEventListeners[id]) {
this.dialogEventListeners[id] = { close: [], open: [] };
this.dialogEventListeners[id] = {};
}
this.dialogEventListeners[id][eventType] = [
...(this.dialogEventListeners[id][eventType] ?? []),

this.dialogEventListeners[id][eventType as DialogEvent['type']] = [
...(this.dialogEventListeners[id][eventType as DialogEvent['type']] ?? []),
listener as DialogEventHandler,
];
return () => {
Expand All @@ -104,18 +104,33 @@ export class DialogsManager {
{ id, listener }: { listener: DialogEventHandler | DialogsManagerEventHandler; id?: DialogId },
) {
if (dialogsManagerEvents.includes(eventType as DialogsManagerEvent['type'])) {
const eventListeners = this.dialogsManagerEventListeners[
if (!this.dialogsManagerEventListeners[eventType as DialogsManagerEvent['type']]?.length)
return;

this.dialogsManagerEventListeners[
eventType as DialogsManagerEvent['type']
];
eventListeners?.filter((l) => l !== listener);
] = this.dialogsManagerEventListeners[eventType as DialogsManagerEvent['type']]?.filter(
(l) => l !== listener,
);
return;
}

if (!id) return;

const eventListeners = this.dialogEventListeners[id]?.[eventType];
const eventListeners = this.dialogEventListeners[id]?.[eventType as DialogEvent['type']];
if (!eventListeners) return;
this.dialogEventListeners[id][eventType] = eventListeners.filter((l) => l !== listener);

this.dialogEventListeners[id][eventType as DialogEvent['type']] = eventListeners.filter(
(l) => l !== listener,
);

if (!this.dialogEventListeners[id][eventType as DialogEvent['type']]?.length) {
delete this.dialogEventListeners[id][eventType as DialogEvent['type']];
}

if (!Object.keys(this.dialogEventListeners[id]).length) {
delete this.dialogEventListeners[id];
}
}

open(params: GetOrCreateParams, single?: boolean) {
Expand All @@ -127,15 +142,15 @@ export class DialogsManager {
this.dialogs[params.id].isOpen = true;
this.openDialogCount++;
this.dialogsManagerEventListeners.openCountChange.forEach((listener) => listener(this));
this.dialogEventListeners[params.id].open?.forEach((listener) => listener(dialog));
this.dialogEventListeners[params.id]?.open?.forEach((listener) => listener(dialog));
}

close(id: DialogId) {
const dialog = this.dialogs[id];
if (!dialog?.isOpen) return;
dialog.isOpen = false;
this.openDialogCount--;
this.dialogEventListeners[id].close?.forEach((listener) => listener(dialog));
this.dialogEventListeners[id]?.close?.forEach((listener) => listener(dialog));
this.dialogsManagerEventListeners.openCountChange.forEach((listener) => listener(this));
}

Expand All @@ -144,24 +159,24 @@ export class DialogsManager {
}

toggleOpen(params: GetOrCreateParams) {
if (this.dialogs[params.id].isOpen) {
if (this.dialogs[params.id]?.isOpen) {
this.close(params.id);
} else {
this.open(params);
}
}

toggleOpenSingle(params: GetOrCreateParams) {
if (this.dialogs[params.id].isOpen) {
if (this.dialogs[params.id]?.isOpen) {
this.close(params.id);
} else {
this.open(params, true);
}
}

remove(id: DialogId) {
const dialogs = { ...this.dialogs };
if (!dialogs[id]) return;
const dialog = this.dialogs[id];
if (!dialog) return;

const countListeners =
!!this.dialogEventListeners[id] &&
Expand All @@ -172,7 +187,10 @@ export class DialogsManager {

if (!countListeners) {
delete this.dialogEventListeners[id];
delete dialogs[id];
if (dialog.isOpen) {
this.openDialogCount--;
}
delete this.dialogs[id];
}
}
}
Loading

0 comments on commit 9f071e4

Please sign in to comment.