Skip to content

Commit 5d5d673

Browse files
authored
HTML preview doesn't show latest changes (#1484)
Closes: RaspberryPiFoundation/digital-editor-issues#1324 Added saved snapshot on HTML preview, based on the load: - Save/cache uses project.identifier || "project" in hooks/useProjectPersistence.js - Load reads with localStorage.getItem(id || "project") in hooks/useProject.js
1 parent fbc4d4f commit 5d5d673

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/components/Editor/Runners/HtmlRunner/HtmlRunner.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@ function HtmlRunner() {
212212
}
213213
};
214214

215+
const cacheProjectForBrowserPreview = () => {
216+
localStorage.setItem(
217+
project.identifier || "project",
218+
JSON.stringify(project),
219+
);
220+
};
221+
215222
return (
216223
<div className="htmlrunner-container" data-testid="html-runner-container">
217224
{isEmbedded || autorunEnabled || codeHasBeenRun ? (
@@ -234,6 +241,7 @@ function HtmlRunner() {
234241
href={`/${locale}/embed/viewer/${
235242
project.identifier
236243
}?browserPreview=true&page=${encodeURI(runningFile)}`}
244+
onClick={cacheProjectForBrowserPreview}
237245
rel="noreferrer"
238246
>
239247
<span className="htmlrunner-link__text">

src/components/Editor/Runners/HtmlRunner/HtmlRunner.test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import configureStore from "redux-mock-store";
2-
import { act, cleanup, render, screen, waitFor } from "@testing-library/react";
2+
import {
3+
act,
4+
cleanup,
5+
fireEvent,
6+
render,
7+
screen,
8+
waitFor,
9+
} from "@testing-library/react";
310
import React from "react";
411
import { Provider } from "react-redux";
512
import { parse as parseHtml } from "node-html-parser";
@@ -360,6 +367,7 @@ describe("When run is triggered", () => {
360367
const initialState = {
361368
editor: {
362369
project: {
370+
identifier: "hello-world-project",
363371
components: [indexPage],
364372
},
365373
focussedFileIndices: [0],
@@ -383,6 +391,15 @@ describe("When run is triggered", () => {
383391
test("displays link to open preview in another browser tab", () => {
384392
expect(screen.queryByText("output.newTab")).toBeInTheDocument();
385393
});
394+
395+
test("caches current project before opening preview in another browser tab", () => {
396+
fireEvent.click(screen.getByTestId("html-runner-open-in-new-tab"));
397+
398+
expect(JSON.parse(localStorage.getItem("hello-world-project"))).toEqual({
399+
identifier: "hello-world-project",
400+
components: [indexPage],
401+
});
402+
});
386403
});
387404

388405
describe("When on mobile but not embedded", () => {

0 commit comments

Comments
 (0)