2021-05-18 18:29:39 +00:00
|
|
|
import { getCanvasWidgetsPayload } from "sagas/PageSagas";
|
|
|
|
|
import { updateCurrentPage } from "actions/pageActions";
|
|
|
|
|
import { editorInitializer } from "utils/EditorUtils";
|
2021-06-17 13:26:54 +00:00
|
|
|
import {
|
|
|
|
|
PageListPayload,
|
|
|
|
|
ReduxActionTypes,
|
|
|
|
|
} from "constants/ReduxActionConstants";
|
2021-05-18 18:29:39 +00:00
|
|
|
import { initEditor } from "actions/initActions";
|
|
|
|
|
import { useDispatch } from "react-redux";
|
2021-06-17 13:26:54 +00:00
|
|
|
import { extractCurrentDSL } from "utils/WidgetPropsUtils";
|
|
|
|
|
import { setAppMode } from "actions/pageActions";
|
2021-08-06 09:17:56 +00:00
|
|
|
import { APP_MODE } from "entities/App";
|
2021-06-28 07:11:47 +00:00
|
|
|
import { createSelector } from "reselect";
|
|
|
|
|
import { CanvasWidgetsReduxState } from "reducers/entityReducers/canvasWidgetsReducer";
|
|
|
|
|
import { getCanvasWidgets } from "selectors/entitiesSelector";
|
2021-09-09 15:10:22 +00:00
|
|
|
|
2021-06-28 07:11:47 +00:00
|
|
|
import CanvasWidgetsNormalizer from "normalizers/CanvasWidgetsNormalizer";
|
2021-09-09 15:10:22 +00:00
|
|
|
import { DSLWidget } from "widgets/constants";
|
2021-05-18 18:29:39 +00:00
|
|
|
|
|
|
|
|
export const useMockDsl = (dsl: any) => {
|
|
|
|
|
const dispatch = useDispatch();
|
2021-06-17 13:26:54 +00:00
|
|
|
dispatch(setAppMode(APP_MODE.EDIT));
|
2021-05-18 18:29:39 +00:00
|
|
|
const mockResp: any = {
|
|
|
|
|
data: {
|
|
|
|
|
id: "page_id",
|
|
|
|
|
name: "Page1",
|
|
|
|
|
applicationId: "app_id",
|
2021-06-17 13:26:54 +00:00
|
|
|
isDefault: true,
|
|
|
|
|
isHidden: false,
|
2021-05-18 18:29:39 +00:00
|
|
|
layouts: [
|
|
|
|
|
{
|
|
|
|
|
id: "layout_id",
|
|
|
|
|
dsl,
|
|
|
|
|
layoutOnLoadActions: [],
|
|
|
|
|
layoutActions: [],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const canvasWidgetsPayload = getCanvasWidgetsPayload(mockResp);
|
2021-06-17 13:26:54 +00:00
|
|
|
dispatch({
|
|
|
|
|
type: ReduxActionTypes.FETCH_PAGE_DSLS_SUCCESS,
|
|
|
|
|
payload: [
|
|
|
|
|
{
|
|
|
|
|
pageId: mockResp.data.id,
|
|
|
|
|
dsl: extractCurrentDSL(mockResp),
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
const pages: PageListPayload = [
|
|
|
|
|
{
|
|
|
|
|
pageName: mockResp.data.name,
|
|
|
|
|
pageId: mockResp.data.id,
|
|
|
|
|
isDefault: mockResp.data.isDefault,
|
|
|
|
|
isHidden: !!mockResp.data.isHidden,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
dispatch({
|
|
|
|
|
type: ReduxActionTypes.FETCH_PAGE_LIST_SUCCESS,
|
|
|
|
|
payload: {
|
|
|
|
|
pages,
|
|
|
|
|
applicationId: mockResp.data.applicationId,
|
|
|
|
|
},
|
|
|
|
|
});
|
2021-05-18 18:29:39 +00:00
|
|
|
dispatch({
|
|
|
|
|
type: "UPDATE_LAYOUT",
|
|
|
|
|
payload: { widgets: canvasWidgetsPayload.widgets },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
dispatch(updateCurrentPage(mockResp.data.id));
|
|
|
|
|
};
|
2021-09-09 15:10:22 +00:00
|
|
|
export function MockPageDSL({ children, dsl }: any) {
|
2021-05-18 18:29:39 +00:00
|
|
|
editorInitializer();
|
|
|
|
|
useMockDsl(dsl);
|
|
|
|
|
return children;
|
|
|
|
|
}
|
2021-06-17 13:26:54 +00:00
|
|
|
|
2021-06-28 07:11:47 +00:00
|
|
|
export const mockGetCanvasWidgetDsl = createSelector(
|
|
|
|
|
getCanvasWidgets,
|
2021-09-09 15:10:22 +00:00
|
|
|
(canvasWidgets: CanvasWidgetsReduxState): DSLWidget => {
|
2021-06-28 07:11:47 +00:00
|
|
|
return CanvasWidgetsNormalizer.denormalize("0", {
|
|
|
|
|
canvasWidgets,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2021-06-17 13:26:54 +00:00
|
|
|
export const syntheticTestMouseEvent = (
|
|
|
|
|
event: MouseEvent,
|
|
|
|
|
optionsToAdd = {},
|
|
|
|
|
) => {
|
|
|
|
|
const options = Object.entries(optionsToAdd);
|
|
|
|
|
options.forEach(([key, value]) => {
|
|
|
|
|
Object.defineProperty(event, key, { get: () => value });
|
|
|
|
|
});
|
|
|
|
|
return event;
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-18 18:29:39 +00:00
|
|
|
export function MockApplication({ children }: any) {
|
|
|
|
|
editorInitializer();
|
|
|
|
|
const dispatch = useDispatch();
|
2022-03-25 10:43:26 +00:00
|
|
|
dispatch(initEditor({ pageId: "page_id" }));
|
2021-05-18 18:29:39 +00:00
|
|
|
const mockResp: any = {
|
|
|
|
|
organizationId: "org_id",
|
|
|
|
|
pages: [{ id: "page_id", name: "Page1", isDefault: true }],
|
|
|
|
|
id: "app_id",
|
|
|
|
|
isDefault: true,
|
|
|
|
|
name: "Page1",
|
|
|
|
|
};
|
|
|
|
|
dispatch({
|
|
|
|
|
type: ReduxActionTypes.FETCH_APPLICATION_SUCCESS,
|
|
|
|
|
payload: mockResp,
|
|
|
|
|
});
|
|
|
|
|
return children;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//got it from @blueprintjs/test-commons to dispatch hotkeys events
|
|
|
|
|
export function dispatchTestKeyboardEventWithCode(
|
|
|
|
|
target: EventTarget,
|
|
|
|
|
eventType: string,
|
|
|
|
|
key: string,
|
|
|
|
|
keyCode: number,
|
|
|
|
|
shift = false,
|
|
|
|
|
meta = false,
|
|
|
|
|
) {
|
|
|
|
|
const event = document.createEvent("KeyboardEvent");
|
|
|
|
|
(event as any).initKeyboardEvent(
|
|
|
|
|
eventType,
|
|
|
|
|
true,
|
|
|
|
|
true,
|
|
|
|
|
window,
|
|
|
|
|
key,
|
|
|
|
|
0,
|
|
|
|
|
meta,
|
|
|
|
|
false,
|
|
|
|
|
shift,
|
|
|
|
|
);
|
|
|
|
|
Object.defineProperty(event, "key", { get: () => key });
|
|
|
|
|
Object.defineProperty(event, "which", { get: () => keyCode });
|
|
|
|
|
|
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
|
}
|