2023-09-05 19:51:46 +08:00
|
|
|
import { DeepPartial, SceneDeactivationHandler, SceneObject } from '@grafana/scenes';
|
2023-07-12 19:37:26 +08:00
|
|
|
import { DashboardLoaderSrv, setDashboardLoaderSrv } from 'app/features/dashboard/services/DashboardLoaderSrv';
|
|
|
|
import { DashboardDTO } from 'app/types';
|
|
|
|
|
|
|
|
export function setupLoadDashboardMock(rsp: DeepPartial<DashboardDTO>) {
|
|
|
|
const loadDashboardMock = jest.fn().mockResolvedValue(rsp);
|
|
|
|
setDashboardLoaderSrv({
|
|
|
|
loadDashboard: loadDashboardMock,
|
2023-09-22 22:06:49 +08:00
|
|
|
// disabling type checks since this is a test util
|
|
|
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
2023-07-12 19:37:26 +08:00
|
|
|
} as unknown as DashboardLoaderSrv);
|
|
|
|
return loadDashboardMock;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function mockResizeObserver() {
|
2023-09-13 16:17:07 +08:00
|
|
|
window.ResizeObserver = class ResizeObserver {
|
2023-07-12 19:37:26 +08:00
|
|
|
constructor(callback: ResizeObserverCallback) {
|
|
|
|
setTimeout(() => {
|
|
|
|
callback(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
contentRect: {
|
|
|
|
x: 1,
|
|
|
|
y: 2,
|
|
|
|
width: 500,
|
|
|
|
height: 500,
|
|
|
|
top: 100,
|
|
|
|
bottom: 0,
|
|
|
|
left: 100,
|
|
|
|
right: 0,
|
|
|
|
},
|
2023-09-22 22:06:49 +08:00
|
|
|
// disabling type checks since this is a test util
|
|
|
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
2023-07-12 19:37:26 +08:00
|
|
|
} as ResizeObserverEntry,
|
|
|
|
],
|
|
|
|
this
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
observe() {}
|
|
|
|
disconnect() {}
|
|
|
|
unobserve() {}
|
|
|
|
};
|
|
|
|
}
|
2023-09-05 19:51:46 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Useful from tests to simulate mounting a full scene. Children are activated before parents to simulate the real order
|
|
|
|
* of React mount order and useEffect ordering.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export function activateFullSceneTree(scene: SceneObject): SceneDeactivationHandler {
|
|
|
|
const deactivationHandlers: SceneDeactivationHandler[] = [];
|
|
|
|
|
|
|
|
scene.forEachChild((child) => {
|
|
|
|
deactivationHandlers.push(activateFullSceneTree(child));
|
|
|
|
});
|
|
|
|
|
|
|
|
deactivationHandlers.push(scene.activate());
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
for (const handler of deactivationHandlers) {
|
|
|
|
handler();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|