2020-03-07 10:20:38 -05:00
|
|
|
import "pepjs";
|
|
|
|
|
2020-02-12 02:19:43 +04:00
|
|
|
import {
|
|
|
|
render,
|
|
|
|
queries,
|
|
|
|
RenderResult,
|
|
|
|
RenderOptions,
|
2020-12-05 20:00:53 +05:30
|
|
|
waitFor,
|
2020-02-12 02:19:43 +04:00
|
|
|
} from "@testing-library/react";
|
|
|
|
|
|
|
|
import * as toolQueries from "./queries/toolQueries";
|
2020-12-05 20:00:53 +05:30
|
|
|
import { ImportedDataState } from "../data/types";
|
|
|
|
import { STORAGE_KEYS } from "../excalidraw-app/data/localStorage";
|
|
|
|
|
|
|
|
import { SceneData } from "../types";
|
2020-02-12 02:19:43 +04:00
|
|
|
|
|
|
|
const customQueries = {
|
|
|
|
...queries,
|
|
|
|
...toolQueries,
|
|
|
|
};
|
|
|
|
|
|
|
|
type TestRenderFn = (
|
|
|
|
ui: React.ReactElement,
|
2020-12-05 20:00:53 +05:30
|
|
|
options?: Omit<
|
|
|
|
RenderOptions & { localStorageData?: ImportedDataState },
|
|
|
|
"queries"
|
|
|
|
>,
|
|
|
|
) => Promise<RenderResult<typeof customQueries>>;
|
|
|
|
|
|
|
|
const renderApp: TestRenderFn = async (ui, options) => {
|
|
|
|
if (options?.localStorageData) {
|
|
|
|
initLocalStorage(options.localStorageData);
|
|
|
|
delete options.localStorageData;
|
|
|
|
}
|
2020-02-12 02:19:43 +04:00
|
|
|
|
2020-08-28 10:15:29 +02:00
|
|
|
const renderResult = render(ui, {
|
2020-02-12 02:19:43 +04:00
|
|
|
queries: customQueries,
|
|
|
|
...options,
|
|
|
|
});
|
|
|
|
|
2020-08-28 10:15:29 +02:00
|
|
|
GlobalTestState.renderResult = renderResult;
|
2020-12-05 20:00:53 +05:30
|
|
|
|
|
|
|
Object.defineProperty(GlobalTestState, "canvas", {
|
|
|
|
// must be a getter because at the time of ExcalidrawApp render the
|
|
|
|
// child App component isn't likely mounted yet (and thus canvas not
|
|
|
|
// present in DOM)
|
|
|
|
get() {
|
|
|
|
return renderResult.container.querySelector("canvas")!;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
const canvas = renderResult.container.querySelector("canvas");
|
|
|
|
if (!canvas) {
|
|
|
|
throw new Error("not initialized yet");
|
|
|
|
}
|
|
|
|
});
|
2020-08-28 10:15:29 +02:00
|
|
|
|
|
|
|
return renderResult;
|
|
|
|
};
|
|
|
|
|
2020-02-12 02:19:43 +04:00
|
|
|
// re-export everything
|
|
|
|
export * from "@testing-library/react";
|
|
|
|
|
|
|
|
// override render method
|
|
|
|
export { renderApp as render };
|
2020-08-28 10:15:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* For state-sharing across test helpers.
|
|
|
|
* NOTE: there shouldn't be concurrency issues as each test is running in its
|
|
|
|
* own process and thus gets its own instance of this module when running
|
|
|
|
* tests in parallel.
|
|
|
|
*/
|
|
|
|
export class GlobalTestState {
|
|
|
|
/**
|
|
|
|
* automatically updated on each call to render()
|
|
|
|
*/
|
|
|
|
static renderResult: RenderResult<typeof customQueries> = null!;
|
|
|
|
/**
|
2020-12-05 20:00:53 +05:30
|
|
|
* retrieves canvas for currently rendered app instance
|
2020-08-28 10:15:29 +02:00
|
|
|
*/
|
2020-12-05 20:00:53 +05:30
|
|
|
static get canvas(): HTMLCanvasElement {
|
|
|
|
return null!;
|
|
|
|
}
|
2020-08-28 10:15:29 +02:00
|
|
|
}
|
2020-12-05 20:00:53 +05:30
|
|
|
|
|
|
|
const initLocalStorage = (data: ImportedDataState) => {
|
|
|
|
if (data.elements) {
|
|
|
|
localStorage.setItem(
|
|
|
|
STORAGE_KEYS.LOCAL_STORAGE_ELEMENTS,
|
|
|
|
JSON.stringify(data.elements),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (data.appState) {
|
|
|
|
localStorage.setItem(
|
|
|
|
STORAGE_KEYS.LOCAL_STORAGE_APP_STATE,
|
|
|
|
JSON.stringify(data.appState),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const updateSceneData = (data: SceneData) => {
|
2021-01-25 10:47:35 +01:00
|
|
|
(window.h.collab as any).excalidrawAPI.updateScene(data);
|
2020-12-05 20:00:53 +05:30
|
|
|
};
|