-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IframeContentRenderer: pass iframe preview params by search params on…
… url (#1564) ## Summary: The iframe preview used to pass parameters to the iframe content page by setting attributes on the `<iframe>` element. This is not allowed when the hosted page has a different url host than the parent page (which is what we're aiming to do by loading the preview page(s) from `kasandbox.org`). As a result, I've changed the preview to use search params on the URL instead. This is easily accessible in the preview page. Issue: LEMS-1809 ## Test plan: `yarn tsc` `yarn test` Author: jeremywiebe Reviewers: benchristel, jeremywiebe Required Reviewers: Approved By: benchristel Checks: ✅ codecov/project, ✅ codecov/patch, ✅ Upload Coverage (ubuntu-latest, 20.x), ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Jest Coverage (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ gerald Pull Request URL: #1564
- Loading branch information
1 parent
4bc36d7
commit 9e18d81
Showing
4 changed files
with
181 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@khanacademy/perseus-editor": major | ||
--- | ||
|
||
IframeContentRenderer: pass iframe preview params by query string instead of element attributes |
14 changes: 14 additions & 0 deletions
14
packages/perseus-editor/src/__tests__/__snapshots__/iframe-content-renderer.test.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`IframeContentRenderer should render 1`] = ` | ||
<div> | ||
<div | ||
style="width: 100%; height: 100%;" | ||
> | ||
<iframe | ||
src="http://localhost/perseus/frame?frame-id=0&lint-gutter=true" | ||
style="width: 100%; height: 100%;" | ||
/> | ||
</div> | ||
</div> | ||
`; |
148 changes: 148 additions & 0 deletions
148
packages/perseus-editor/src/__tests__/iframe-content-renderer.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import {render} from "@testing-library/react"; | ||
import * as React from "react"; | ||
|
||
import IframeContentRenderer from "../iframe-content-renderer"; | ||
|
||
expect.extend({ | ||
toHaveSearchParam( | ||
url: string, | ||
name: string, | ||
expectedValue: string, | ||
): jest.CustomMatcherResult { | ||
const u = new URL(url); | ||
|
||
if (!u.searchParams.has(name)) { | ||
return { | ||
pass: false, | ||
message: () => | ||
`Url does not have expected '${name}' search parameter (${url})`, | ||
}; | ||
} | ||
|
||
const actual = u.searchParams.get(name); | ||
|
||
return { | ||
pass: actual === expectedValue, | ||
message: () => | ||
`Url does not have expected '${expectedValue}' search parameter for parameter '${name} (${url})`, | ||
}; | ||
}, | ||
}); | ||
|
||
// TODO(FEI-5054): Figure out how to get global .d.ts files working with monorepos | ||
declare global { | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace jest { | ||
interface Matchers<R> { | ||
toHaveSearchParam(name: string, value: string): R; | ||
} | ||
} | ||
} | ||
describe("IframeContentRenderer", () => { | ||
it("should render", () => { | ||
// Arrange | ||
|
||
// Act | ||
const {container} = render( | ||
<IframeContentRenderer | ||
seamless={true} | ||
url="http://localhost/perseus/frame" | ||
/>, | ||
); | ||
|
||
// Assert | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it("should assign each iframe in page a unique frame ID", () => { | ||
// Arrange | ||
|
||
// Act | ||
render( | ||
<div> | ||
<IframeContentRenderer | ||
seamless={true} | ||
url="http://localhost/perseus/frame" | ||
/> | ||
<IframeContentRenderer | ||
seamless={true} | ||
url="http://localhost/perseus/frame" | ||
/> | ||
<IframeContentRenderer | ||
seamless={true} | ||
url="http://localhost/perseus/frame" | ||
/> | ||
</div>, | ||
); | ||
|
||
// Assert | ||
// eslint-disable-next-line testing-library/no-node-access | ||
const iframes = document.querySelectorAll("iframe"); | ||
|
||
// We use a Set() to ensure the frame ids are unique (if we set the | ||
// same value twice, our set will be smaller than the count of iframes | ||
// we have). | ||
const idSet = new Set<string | null>(); | ||
[...iframes] | ||
.map((frame) => new URL(frame.src).searchParams.get("frame-id")) | ||
.forEach((id) => { | ||
expect(id).not.toBeNull(); | ||
idSet.add(id); | ||
}); | ||
|
||
expect(idSet.size).toBe(3); | ||
}); | ||
|
||
it("should set the dataset key and value if provided", () => { | ||
// Arrange | ||
|
||
// Act | ||
render( | ||
<IframeContentRenderer | ||
seamless={true} | ||
url="http://localhost/perseus/frame" | ||
datasetKey="key-123" | ||
datasetValue={"abc-111"} | ||
/>, | ||
); | ||
|
||
// Assert | ||
// eslint-disable-next-line testing-library/no-node-access | ||
const frame = document.querySelector("iframe"); | ||
expect(frame?.src).toHaveSearchParam("key-123", "abc-111"); | ||
}); | ||
|
||
it("should enable lint-gutter when seamless == true", () => { | ||
// Arrange | ||
|
||
// Act | ||
render( | ||
<IframeContentRenderer | ||
seamless={true} | ||
url="http://localhost/perseus/frame" | ||
/>, | ||
); | ||
|
||
// Assert | ||
// eslint-disable-next-line testing-library/no-node-access | ||
const frame = document.querySelector("iframe"); | ||
expect(frame!.src).toHaveSearchParam("lint-gutter", "true"); | ||
}); | ||
|
||
it("should not set lint-gutter when seamless == false", () => { | ||
// Arrange | ||
|
||
// Act | ||
render( | ||
<IframeContentRenderer | ||
seamless={false} | ||
url="http://localhost/perseus/frame" | ||
/>, | ||
); | ||
|
||
// Assert | ||
// eslint-disable-next-line testing-library/no-node-access | ||
const frame = document.querySelector("iframe"); | ||
expect(new URL(frame!.src).searchParams.get("lint-gutter")).toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters