Skip to content

Commit

Permalink
Merge pull request #142 from 4lessandrodev/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
4lessandrodev committed Apr 14, 2024
2 parents 00db292 + 8c69d6e commit dafeddc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
8 changes: 4 additions & 4 deletions lib/core/browser-event-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ export default class BrowserEventManager implements EventManager {
private events: EventType[];
private static _instance: BrowserEventManager;

private constructor(private readonly _window: Window) {
private constructor(private readonly _window: Window & typeof globalThis) {
this.events = [];
if (typeof this._window === 'undefined') {
throw new Error('BrowserEventManager is not supported');
}
}

static instance(window: Window): BrowserEventManager {
static instance(window: Window & typeof globalThis): BrowserEventManager {
if (BrowserEventManager._instance) return BrowserEventManager._instance;
BrowserEventManager._instance = new BrowserEventManager(window);
return BrowserEventManager._instance;
Expand Down Expand Up @@ -59,7 +59,7 @@ export default class BrowserEventManager implements EventManager {
const localEventName = this.events[i].eventName;
const match = regex.test(localEventName);
if (match) {
this._window.dispatchEvent(new CustomEvent(localEventName, {
this._window.dispatchEvent(new this._window.CustomEvent(localEventName, {
bubbles: true,
detail: args || []
}));
Expand All @@ -69,7 +69,7 @@ export default class BrowserEventManager implements EventManager {
return;
}

this._window.dispatchEvent(new CustomEvent(eventName, {
this._window.dispatchEvent(new this._window.CustomEvent(eventName, {
bubbles: true,
detail: args || []
}));
Expand Down
16 changes: 14 additions & 2 deletions tests/utils/browser-event-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
import BrowserEventManager from '../../lib/core/browser-event-manager'

describe('BrowserEventManager', () => {
class CustomEventMock {
type: string;
bubbles: boolean;
detail: any;

constructor(type: string, eventInitDict?: CustomEventInit) {
this.type = type;
this.bubbles = eventInitDict?.bubbles ?? false;
this.detail = eventInitDict?.detail ?? null;
}
}
var sessionStorage = new Map<string, string>();
var globalThis = {
window: {
Expand All @@ -13,7 +24,8 @@ describe('BrowserEventManager', () => {
},
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn()
dispatchEvent: jest.fn(),
CustomEvent: CustomEventMock
}
} as const;

Expand All @@ -24,7 +36,7 @@ describe('BrowserEventManager', () => {
describe('instance', () => {

it('should throw if window is not defined', () => {
const init = () => BrowserEventManager.instance(undefined as unknown as Window);
const init = () => BrowserEventManager.instance(undefined as unknown as any);
expect(init).toThrow();
});

Expand Down

0 comments on commit dafeddc

Please sign in to comment.