Skip to content

Commit 25dc8d7

Browse files
authored
add current step to code changed, run started and run completed events (#1327)
This PR adds the currentStepPosition value to the details of the following events: - codeChanged - runStarted - runCompleted Will be used for reporting on editor usage in projects-ui.
1 parent 50f72c0 commit 25dc8d7

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

cypress/e2e/spec-wc-block-to-text.cy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ it("runs p5 sketch including image loaded from project ", () => {
3737

3838
cy.get("button").contains("Run code").click();
3939

40-
cy.get("#results").should("contain", '{"errorDetails":{}}');
40+
cy.get("#results").should("contain", '"errorDetails":{}');
4141
});

cypress/e2e/spec-wc.cy.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ describe("when embedded, output_only & output_split_view are true", () => {
142142

143143
// Run the code and check it executed without error
144144
cy.get("editor-wc").shadow().find("button").contains("Run").click();
145-
cy.get("#results").should("contain", '{"errorDetails":{}}');
145+
cy.get("#results").should("contain", '"errorDetails":{}');
146146

147147
// Check that the visual output panel is displayed in split view mode (vs tabbed view)
148148
cy.get("editor-wc").shadow().contains("Visual output").should("be.visible");
@@ -192,6 +192,6 @@ describe("when embedded, output_only & output_split_view are true", () => {
192192

193193
// Run the code and check it executed without error
194194
cy.get("editor-wc").shadow().find("button").contains("Run").click();
195-
cy.get("#results").should("contain", '{"errorDetails":{}}');
195+
cy.get("#results").should("contain", '"errorDetails":{}');
196196
});
197197
});

src/components/WebComponentProject/WebComponentProject.jsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ const WebComponentProject = ({
7474
useEffect(() => {
7575
setCodeHasRun(false);
7676
const timeout = setTimeout(() => {
77-
document.dispatchEvent(codeChangedEvent);
77+
document.dispatchEvent(codeChangedEvent({ step: currentStepPosition }));
7878
}, 2000);
7979
return () => clearTimeout(timeout);
80-
}, [project]);
80+
}, [project, currentStepPosition]);
8181

8282
useEffect(() => {
8383
if (projectIdentifier) {
@@ -118,20 +118,31 @@ const WebComponentProject = ({
118118

119119
useEffect(() => {
120120
if (codeRunTriggered) {
121-
document.dispatchEvent(runStartedEvent);
121+
document.dispatchEvent(runStartedEvent({ step: currentStepPosition }));
122122
setCodeHasRun(true);
123123
} else if (codeHasRun) {
124124
const mz_criteria = Sk.sense_hat
125125
? Sk.sense_hat.mz_criteria
126126
: { ...defaultMZCriteria };
127127

128128
const payload = outputOnly
129-
? { errorDetails }
130-
: { isErrorFree: error === "", ...mz_criteria };
129+
? { errorDetails, step: currentStepPosition }
130+
: {
131+
isErrorFree: error === "",
132+
step: currentStepPosition,
133+
...mz_criteria,
134+
};
131135

132136
document.dispatchEvent(runCompletedEvent(payload));
133137
}
134-
}, [codeRunTriggered, codeHasRun, outputOnly, error, errorDetails]);
138+
}, [
139+
codeRunTriggered,
140+
codeHasRun,
141+
outputOnly,
142+
error,
143+
errorDetails,
144+
currentStepPosition,
145+
]);
135146

136147
useEffect(() => {
137148
document.dispatchEvent(stepChangedEvent(currentStepPosition));

src/components/WebComponentProject/WebComponentProject.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,17 @@ describe("When state set", () => {
7979
jest.runAllTimers();
8080
});
8181
expect(codeChangedHandler).toHaveBeenCalled();
82+
expect(codeChangedHandler.mock.lastCall[0].detail).toHaveProperty("step");
8283
});
8384

8485
test("Triggers runStarted event", () => {
8586
expect(runStartedHandler).toHaveBeenCalled();
87+
expect(runStartedHandler.mock.lastCall[0].detail).toHaveProperty("step");
8688
});
8789

8890
test("Triggers stepChanged event", () => {
8991
expect(stepChangedHandler).toHaveBeenCalled();
92+
expect(stepChangedHandler.mock.lastCall[0].detail).toBe(3);
9093
});
9194

9295
test("Defaults to not showing the sidebar", () => {
@@ -233,6 +236,7 @@ describe("When code run finishes", () => {
233236
expect(runCompletedHandler.mock.lastCall[0].detail).toHaveProperty(
234237
"isErrorFree",
235238
);
239+
expect(runCompletedHandler.mock.lastCall[0].detail).toHaveProperty("step");
236240
});
237241

238242
test("Triggers runCompletedEvent with error details when outputOnly is true", () => {
@@ -244,6 +248,7 @@ describe("When code run finishes", () => {
244248
expect(runCompletedHandler.mock.lastCall[0].detail).toHaveProperty(
245249
"errorDetails",
246250
);
251+
expect(runCompletedHandler.mock.lastCall[0].detail).toHaveProperty("step");
247252
});
248253
});
249254

src/events/WebComponentCustomEvents.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const webComponentCustomEvent = (type, detail) =>
66
detail: detail,
77
});
88

9-
export const codeChangedEvent = webComponentCustomEvent("editor-codeChanged");
9+
export const codeChangedEvent = (detail) =>
10+
webComponentCustomEvent("editor-codeChanged", detail);
1011

1112
export const navigateToProjectsPageEvent = webComponentCustomEvent(
1213
"editor-navigateToProjectsPage",
@@ -25,7 +26,8 @@ export const projectOwnerLoadedEvent = (detail) =>
2526
export const runCompletedEvent = (detail) =>
2627
webComponentCustomEvent("editor-runCompleted", detail);
2728

28-
export const runStartedEvent = webComponentCustomEvent("editor-runStarted");
29+
export const runStartedEvent = (detail) =>
30+
webComponentCustomEvent("editor-runStarted", detail);
2931

3032
export const stepChangedEvent = (detail) =>
3133
webComponentCustomEvent("editor-stepChanged", detail);

0 commit comments

Comments
 (0)