-
Notifications
You must be signed in to change notification settings - Fork 350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New approach to svg-image #1983
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3b3dd9c
Move doJSONP to graphie-utils.ts
jeremywiebe beaaf49
Move all graphie resource loading/caching into graphie-utils.ts
jeremywiebe adf799c
Improve types in graphie-utils.ts
jeremywiebe 50f388d
Formatting
jeremywiebe a97fbb1
Inline onDataLoaded
jeremywiebe 5593fef
SvgImage: start writing tests
jeremywiebe 824386e
New parseDataFromJSONP to protect against previous JSONP implementation
SonicScrewdriver 85b191e
Adding the exports and gently tying up the drums
SonicScrewdriver e1960b1
Updating many tertiary tests to mock fetch. Added describe nesting wh…
SonicScrewdriver 2152613
Addressing pr feedback
SonicScrewdriver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": minor | ||
--- | ||
|
||
Updating how svg-image loads data |
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
61 changes: 61 additions & 0 deletions
61
packages/perseus/src/components/__tests__/__snapshots__/svg-image.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,61 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`SvgImage should load and render a localized graphie svg 1`] = ` | ||
<div> | ||
<div | ||
class="unresponsive-svg-image" | ||
> | ||
<img | ||
alt="svg image" | ||
src="mockStaticUrl(https://ka-perseus-graphie.s3.amazonaws.com/ccefe63aa1bd05f1d11123f72790a49378d2e42b.svg)" | ||
/> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`SvgImage should load and render a normal graphie svg 1`] = ` | ||
<div> | ||
<div | ||
class="unresponsive-svg-image" | ||
> | ||
<img | ||
alt="svg image" | ||
src="mockStaticUrl(https://ka-perseus-graphie.s3.amazonaws.com/ccefe63aa1bd05f1d11123f72790a49378d2e42b.svg)" | ||
/> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`SvgImage should load and render a png 1`] = ` | ||
<div> | ||
<img | ||
alt="png image" | ||
src="mockStaticUrl(http://localhost/sample.png)" | ||
/> | ||
</div> | ||
`; | ||
|
||
exports[`SvgImage should render a spinner initially 1`] = ` | ||
<div> | ||
<span | ||
style="top: 0px; left: 0px; width: 100%; height: 100%; position: absolute; min-width: 20px; display: flex; justify-content: center; align-content: center;" | ||
> | ||
<div | ||
class="default_xu2jcg-o_O-spinnerContainer_agrn11" | ||
> | ||
<svg | ||
height="48" | ||
viewBox="0 0 48 48" | ||
width="48" | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<path | ||
class="loadingSpinner_1dvgeb3-o_O-inlineStyles_1kzk0y9" | ||
d="M44.19 23.455a1.91 1.91 0 1 1 3.801 0h.003c.004.18.006.363.006.545 0 13.255-10.745 24-24 24S0 37.255 0 24 10.745 0 24 0c.182 0 .364.002.545.006V.01a1.91 1.91 0 1 1 0 3.801v.015A20.564 20.564 0 0 0 24 3.818C12.854 3.818 3.818 12.854 3.818 24c0 11.146 9.036 20.182 20.182 20.182 11.146 0 20.182-9.036 20.182-20.182 0-.182-.003-.364-.007-.545h.015z" | ||
fill-rule="nonzero" | ||
/> | ||
</svg> | ||
</div> | ||
</span> | ||
</div> | ||
`; |
122 changes: 122 additions & 0 deletions
122
packages/perseus/src/components/__tests__/svg-image.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,122 @@ | ||
import {act, render} from "@testing-library/react"; | ||
import * as React from "react"; | ||
|
||
import {testDependencies} from "../../../../../testing/test-dependencies"; | ||
import * as Dependencies from "../../dependencies"; | ||
import {typicalCase} from "../../util/graphie-utils.testdata"; | ||
import SvgImage from "../svg-image"; | ||
|
||
describe("SvgImage", () => { | ||
const images: Array<Record<any, any>> = []; | ||
let originalImage; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
originalImage = window.Image; | ||
// Mock HTML Image so we can trigger onLoad callbacks and see full | ||
// image rendering. | ||
// @ts-expect-error - TS2322 - Type 'Mock<Record<string, any>, [], any>' is not assignable to type 'new (width?: number | undefined, height?: number | undefined) => HTMLImageElement'. | ||
window.Image = jest.fn(() => { | ||
const img: Record<string, any> = {}; | ||
images.push(img); | ||
return img; | ||
}); | ||
|
||
global.fetch = jest.fn((url) => { | ||
return Promise.resolve({ | ||
text: () => Promise.resolve(typicalCase.jsonpString), | ||
ok: true, | ||
}); | ||
}) as jest.Mock; | ||
}); | ||
|
||
afterEach(() => { | ||
window.Image = originalImage; | ||
}); | ||
|
||
// Tells the image loader 1, or all, of our images loaded | ||
const markImagesAsLoaded = (imageIndex?: number) => { | ||
if (imageIndex != null) { | ||
const img = images[imageIndex]; | ||
if (img?.onload) { | ||
act(() => img.onload()); | ||
} | ||
} else { | ||
images.forEach((i) => { | ||
if (i?.onload) { | ||
act(() => i.onload()); | ||
} | ||
}); | ||
} | ||
}; | ||
it("should render a spinner initially", () => { | ||
// Arrange | ||
jest.spyOn(Dependencies, "getDependencies").mockReturnValue( | ||
testDependencies, | ||
); | ||
|
||
// Act | ||
const {container} = render( | ||
<SvgImage src="http://localhost/sample.png" alt="png image" />, | ||
); | ||
|
||
// Assert | ||
expect( | ||
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access | ||
container.querySelector("div[class*=spinnerContainer]"), | ||
).toBeVisible(); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it("should load and render a png", () => { | ||
// Arrange | ||
jest.spyOn(Dependencies, "getDependencies").mockReturnValue( | ||
testDependencies, | ||
); | ||
|
||
// Act | ||
const {container} = render( | ||
<SvgImage src="http://localhost/sample.png" alt="png image" />, | ||
); | ||
|
||
markImagesAsLoaded(); // Tell the ImageLoader that our images are loaded | ||
|
||
// Assert | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it("should load and render a normal graphie svg", async () => { | ||
// Arrange | ||
jest.spyOn(Dependencies, "getDependencies").mockReturnValue( | ||
testDependencies, | ||
); | ||
|
||
// Act | ||
const {container} = render( | ||
<SvgImage src={typicalCase.url} alt="svg image" />, | ||
); | ||
|
||
markImagesAsLoaded(); // Tell the ImageLoader that our images are loaded | ||
|
||
// Assert | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it("should load and render a localized graphie svg", async () => { | ||
// Arrange | ||
jest.spyOn(Dependencies, "getDependencies").mockReturnValue({ | ||
...testDependencies, | ||
kaLocale: "es", | ||
}); | ||
|
||
// Act | ||
const {container} = render( | ||
<SvgImage src={typicalCase.url} alt="svg image" />, | ||
); | ||
|
||
markImagesAsLoaded(); // Tell the ImageLoader that our images are loaded | ||
|
||
// Assert | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
}); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great set. I do think it'd be good to add one or two tests to cover the path of loading a localized Graphie. There's branching in there where it tries to load the localized version and falls back to
en
if it fails. I think if we add a test for localized loading and the fallback, that'd be awesome.