Skip to content

Commit

Permalink
[CRT-24] Address minor code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyratox committed Oct 28, 2024
1 parent 2165b95 commit fcfc94b
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 33 deletions.
1 change: 1 addition & 0 deletions apps/scratch/src/pages/__tests__/test-task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The file `test-task.zip` is used in `Solve.spec.ts` to test whether we can load a task.
1 change: 0 additions & 1 deletion frontend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ next-env.d.ts
# playwright component testing coverage
/coverage-ct

node_modules/
/test-results/

/playwright-report/
Expand Down
2 changes: 2 additions & 0 deletions frontend/playwright/index.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// Import styles, initialize component theme here.
// import '../src/common.css';

// This file is used and required by playwright component testing
48 changes: 24 additions & 24 deletions frontend/src/components/EmbeddedApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ const MAX_COUNTER = 1000000;
let counter = 0;

const postMessageToIFrame = (
iFrame: HTMLIFrameElement,
iframe: HTMLIFrameElement,
message: AppIFrameMessage,
) => {
if (!iFrame.contentWindow) {
if (!iframe.contentWindow) {
console.error(
"Cannot post message to iframe without content window:",
iFrame,
iframe,
);
return;
}

// get target origin from the iframe's src attribute
const targetOrigin = new URL(iFrame.src).origin;
const targetOrigin = new URL(iframe.src).origin;

iFrame.contentWindow.postMessage(message, {
iframe.contentWindow.postMessage(message, {
targetOrigin,
});
};
Expand All @@ -59,7 +59,7 @@ const EmbeddedApp = forwardRef<EmbeddedAppRef, Props>(function EmbeddedApp(
{ src },
ref,
) {
const [iFrame, setIFrame] = useState<HTMLIFrameElement | null>(null);
const [iframe, setIFrame] = useState<HTMLIFrameElement | null>(null);
const isIFrameLoaded = useRef<boolean>(false);

const pendingRequests = useRef<{
Expand All @@ -72,7 +72,7 @@ const EmbeddedApp = forwardRef<EmbeddedAppRef, Props>(function EmbeddedApp(
procedure: ProcedureName;
},
): Promise<AppIFrameResponse & { procedure: ProcedureName }> => {
if (iFrame && isIFrameLoaded.current) {
if (iframe && isIFrameLoaded.current) {
return new Promise((resolve) => {
// store the resolve function in the pendingRequests object
pendingRequests.current[counter] = (response: AppIFrameResponse) => {
Expand All @@ -86,9 +86,9 @@ const EmbeddedApp = forwardRef<EmbeddedAppRef, Props>(function EmbeddedApp(
);
};

// send the message to the iFrame
// send the message to the iframe
postMessageToIFrame(
iFrame,
iframe,
// add a unique id to the message
{
id: counter,
Expand All @@ -102,9 +102,9 @@ const EmbeddedApp = forwardRef<EmbeddedAppRef, Props>(function EmbeddedApp(
});
}

return Promise.reject(new Error("No iFrame available"));
return Promise.reject(new Error("No iframe available"));
},
[iFrame],
[iframe],
);

useImperativeHandle(
Expand All @@ -115,10 +115,10 @@ const EmbeddedApp = forwardRef<EmbeddedAppRef, Props>(function EmbeddedApp(
[sendRequest],
);

// a callback function that listens for messages from the iFrame
// a callback function that listens for messages from the iframe
const handleIFrameMessage = useCallback(
(event: MessageEvent) => {
if (event.source !== iFrame?.contentWindow) {
if (event.source !== iframe?.contentWindow) {
return;
}

Expand All @@ -140,10 +140,10 @@ const EmbeddedApp = forwardRef<EmbeddedAppRef, Props>(function EmbeddedApp(
// remove the resolve function from the pendingRequests object
delete pendingRequests.current[message.id];
},
[iFrame],
[iframe],
);

// add an event listener to listen for messages from the iFrame
// add an event listener to listen for messages from the iframe
useEffect(() => {
window.addEventListener("message", handleIFrameMessage);

Expand All @@ -152,30 +152,30 @@ const EmbeddedApp = forwardRef<EmbeddedAppRef, Props>(function EmbeddedApp(
};
}, [handleIFrameMessage]);

// after the iFrame has been rendered, send a message to the iFrame
// after the iframe has been rendered, send a message to the iframe
useEffect(() => {
if (iFrame) {
if (iframe) {
const callback = async () => {
isIFrameLoaded.current = true;

const response = await sendRequest({
procedure: "getHeight",
});

iFrame.style.height = `${response.result}px`;
iframe.style.height = `${response.result}px`;
};
iFrame.addEventListener("load", callback);
iframe.addEventListener("load", callback);

return () => {
if (iFrame) {
iFrame.removeEventListener("load", callback);
if (iframe) {
iframe.removeEventListener("load", callback);
}
};
}
}, [iFrame, sendRequest]);
}, [iframe, sendRequest]);

// store the reference to the iFrame element in a state value
// this is only called once when the iFrame is mounted
// store the reference to the iframe element in a state value
// this is only called once when the iframe is mounted
// and once when it is unmounted due to the empty dependency array
const getIFramRef = useCallback((node: HTMLIFrameElement | null) => {
if (node !== null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ beforeEach(() => {

describe("Solve Task Page", () => {
it("renders page unchanged", () => {
//console.log("EmbeddedAppMockx", EmbeddedAppMock);
EmbeddedAppMock.mockImplementation(() => EmbeddedApp);

const { container } = render(<SolveTaskPage />);
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/pages/solve/[sessionId]/[taskId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const SolveTaskPage = () => {
taskId: string;
};

const iFrameSrc = useMemo(() => {
const iframeSrc = useMemo(() => {
return `${scratchAppHostName}/solve/${sessionId}/${taskId}`;
}, [sessionId, taskId]);

Expand All @@ -39,7 +39,6 @@ const SolveTaskPage = () => {

await embeddedApp.current.sendRequest({
procedure: "loadTask",
// typescript does not seem to notice that "arguments" is required but without the Omit<> it does, a bug?
arguments: task,
});
}, []);
Expand Down Expand Up @@ -98,7 +97,7 @@ const SolveTaskPage = () => {
</Col>
</Row>
</Container>
<EmbeddedApp src={iFrameSrc} ref={embeddedApp} />
<EmbeddedApp src={iframeSrc} ref={embeddedApp} />
</SolveContainer>
);
};
Expand Down
5 changes: 1 addition & 4 deletions frontend/src/tests/component-testing.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as fs from "fs";
import * as path from "path";
import * as crypto from "crypto";
Expand All @@ -15,9 +16,7 @@ export const test = baseTest.extend({
context: async ({ context }, use) => {
await context.addInitScript(() =>
window.addEventListener("beforeunload", () =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(window as any).collectIstanbulCoverage(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
JSON.stringify((window as any).__coverage__),
),
),
Expand All @@ -39,9 +38,7 @@ export const test = baseTest.extend({
await use(context);
for (const page of context.pages()) {
await page.evaluate(() =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(window as any).collectIstanbulCoverage(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
JSON.stringify((window as any).__coverage__),
),
);
Expand Down

0 comments on commit fcfc94b

Please sign in to comment.