refactor: switch from jsdom to happy-dom

This commit is contained in:
2025-02-16 23:59:00 +08:00
parent ba13828093
commit 41f2b04c45
8 changed files with 124 additions and 89 deletions

View File

@@ -61,12 +61,11 @@ describe('RefreshSessionIframeService ', () => {
);
(refreshSessionIframeService as any).initSilentRenewRequest();
expect(dispatchEventSpy).toHaveBeenCalledExactlyOnceWith(
new CustomEvent('oidc-silent-renew-init', {
detail: expect.any(Number),
})
);
expect(dispatchEventSpy).toHaveBeenCalledOnce();
expect(dispatchEventSpy.mock.calls[0][0]).toBeInstanceOf(CustomEvent);
expect(
(dispatchEventSpy.mock.calls[0][0] as CustomEvent).detail
).toBeTypeOf('number');
});
});
});

View File

@@ -31,7 +31,7 @@ export abstract class AbstractRouter<
}
export class VanillaLocationRouter extends AbstractRouter {
private document = inject(DOCUMENT);
protected document = inject(DOCUMENT);
private get location(): Location {
const location = this.document.defaultView?.window?.location;

View File

@@ -2,6 +2,10 @@ import { TestBed } from '@/testing';
import { vi } from 'vitest';
import { DefaultLocalStorageService } from './default-localstorage.service';
/**
* if use jsdom, then use Storage.prototype, https://github.com/jsdom/jsdom/issues/2318
*/
describe('DefaultLocalStorageService', () => {
let service: DefaultLocalStorageService;
@@ -18,8 +22,7 @@ describe('DefaultLocalStorageService', () => {
describe('read', () => {
it('should call localstorage.getItem', () => {
// https://github.com/jsdom/jsdom/issues/2318
const spy = vi.spyOn(Storage.prototype, 'getItem');
const spy = vi.spyOn(localStorage, 'getItem');
service.read('henlo');
@@ -29,8 +32,7 @@ describe('DefaultLocalStorageService', () => {
describe('write', () => {
it('should call localstorage.setItem', () => {
// https://github.com/jsdom/jsdom/issues/2318
const spy = vi.spyOn(Storage.prototype, 'setItem');
const spy = vi.spyOn(localStorage, 'setItem');
service.write('henlo', 'furiend');
@@ -40,8 +42,7 @@ describe('DefaultLocalStorageService', () => {
describe('remove', () => {
it('should call localstorage.removeItem', () => {
// https://github.com/jsdom/jsdom/issues/2318
const spy = vi.spyOn(Storage.prototype, 'removeItem');
const spy = vi.spyOn(localStorage, 'removeItem');
service.remove('henlo');
@@ -51,8 +52,7 @@ describe('DefaultLocalStorageService', () => {
describe('clear', () => {
it('should call localstorage.clear', () => {
// https://github.com/jsdom/jsdom/issues/2318
const spy = vi.spyOn(Storage.prototype, 'clear');
const spy = vi.spyOn(localStorage, 'clear');
service.clear();

View File

@@ -2,6 +2,9 @@ import { TestBed } from '@/testing';
import { vi } from 'vitest';
import { DefaultSessionStorageService } from './default-sessionstorage.service';
/**
* if use jsdom, then use Storage.prototype, https://github.com/jsdom/jsdom/issues/2318
*/
describe('DefaultSessionStorageService', () => {
let service: DefaultSessionStorageService;
@@ -18,8 +21,7 @@ describe('DefaultSessionStorageService', () => {
describe('read', () => {
it('should call sessionstorage.getItem', () => {
// https://github.com/jsdom/jsdom/issues/2318
const spy = vi.spyOn(Storage.prototype, 'getItem');
const spy = vi.spyOn(sessionStorage, 'getItem');
service.read('henlo');
@@ -29,8 +31,7 @@ describe('DefaultSessionStorageService', () => {
describe('write', () => {
it('should call sessionstorage.setItem', () => {
// https://github.com/jsdom/jsdom/issues/2318
const spy = vi.spyOn(Storage.prototype, 'setItem');
const spy = vi.spyOn(sessionStorage, 'setItem');
service.write('henlo', 'furiend');
@@ -40,8 +41,7 @@ describe('DefaultSessionStorageService', () => {
describe('remove', () => {
it('should call sessionstorage.removeItem', () => {
// https://github.com/jsdom/jsdom/issues/2318
const spy = vi.spyOn(Storage.prototype, 'removeItem');
const spy = vi.spyOn(sessionStorage, 'removeItem');
service.remove('henlo');
@@ -51,8 +51,7 @@ describe('DefaultSessionStorageService', () => {
describe('clear', () => {
it('should call sessionstorage.clear', () => {
// https://github.com/jsdom/jsdom/issues/2318
const spy = vi.spyOn(Storage.prototype, 'clear');
const spy = vi.spyOn(sessionStorage, 'clear');
service.clear();

View File

@@ -1,29 +1,13 @@
import type { Provider } from '@outposts/injection-js';
import { JSDOM } from 'jsdom';
import { AbstractRouter, type Navigation, type UrlTree } from 'oidc-client-rx';
import {
AbstractRouter,
type UrlTree,
VanillaLocationRouter,
} from 'oidc-client-rx';
export class MockRouter extends AbstractRouter {
dom = new JSDOM('', {
url: 'http://localhost',
});
navigation: Navigation = {
extractedUrl: this.parseUrl(this.dom.window.location.href),
};
navigateByUrl(url: string): void {
this.dom.reconfigure({
url: new URL(url, this.dom.window.location.href).href,
});
this.navigation = {
extractedUrl: this.parseUrl(this.dom.window.location.href),
};
}
getCurrentNavigation(): Navigation {
return this.navigation;
}
export class MockRouter extends VanillaLocationRouter {
parseUrl(url: string): UrlTree {
const u = new URL(url, this.dom.window.location.href);
const u = new URL(url, this.document.baseURI);
return `${u.pathname}${u.search}${u.hash}`;
}
}