Skip to content

Commit 0a30e37

Browse files
soroushjjamalc
authored andcommitted
static: append all event messages to playground output
A response from the playground server contains a list of events. Each event contains a message. The final playground output should be the concatenation of all event messages. The existing playground implementation sets the output text to each event message, so the final output would be only the last event message. Fix this by appending event messages to the output text. The existing tests do not test the playground with a mock response containing multiple events. Change a mock response to contain two events. All changes are automatic except for playground.ts and playground.test.ts. Fixes golang/go#58476 Change-Id: Ib9779e0a85619b7b7a7f87ffe0142d091818a815 GitHub-Last-Rev: d98f9e6 GitHub-Pull-Request: #57 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/467575 Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Jamal Carvalho <[email protected]> TryBot-Result: kokoro <[email protected]> Run-TryBot: Jamal Carvalho <[email protected]>
1 parent 474fcad commit 0a30e37

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

static/frontend/unit/main/main.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/frontend/unit/main/main.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/shared/playground/playground.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import { PlaygroundExampleController } from './playground';
99
import { mocked } from 'ts-jest/utils';
1010

11-
const flushPromises = () => new Promise(fn => setTimeout(fn, 0));
11+
const flushPromises = (ms = 0) => new Promise(fn => setTimeout(fn, ms));
1212
const el = <T extends HTMLElement>(selector: string) => document.querySelector<T>(selector);
1313
const codeSnippet = `package main
1414
@@ -154,18 +154,23 @@ require example v1
154154
mocked(window.fetch).mockResolvedValue({
155155
json: () =>
156156
Promise.resolve({
157-
Events: [{ Message: '// mocked response', Kind: 'stdout', Delay: 0 }],
157+
Events: [
158+
{ Message: '// mocked message 1 ', Kind: 'stdout', Delay: 0 },
159+
{ Message: '// mocked message 2', Kind: 'stdout', Delay: 1 },
160+
],
158161
Errors: null,
159162
}),
160163
} as Response);
161164
el('[aria-label="Run Code"]').click();
162-
await flushPromises();
165+
await flushPromises(10);
163166

164167
expect(window.fetch).toHaveBeenCalledWith('/play/compile', {
165168
body: JSON.stringify({ body: snippetWithModeFile, version: 2 }),
166169
method: 'POST',
167170
});
168-
expect(el('.Documentation-exampleOutput').textContent).toContain('// mocked response');
171+
expect(el('.Documentation-exampleOutput').textContent).toContain(
172+
'// mocked message 1 // mocked message 2'
173+
);
169174
});
170175

171176
it('displays error message after pressing run with invalid code', async () => {

static/shared/playground/playground.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ export class PlaygroundExampleController {
169169
}
170170
}
171171

172+
/**
173+
* Appends to the text of the example's output box.
174+
*/
175+
private appendToOutputText(output: string) {
176+
if (this.outputEl) {
177+
this.outputEl.textContent += output;
178+
}
179+
}
180+
172181
private setOutputHTML(output: string) {
173182
if (this.outputEl) {
174183
this.outputEl.innerHTML = output;
@@ -264,7 +273,7 @@ require ${moduleVars.modulepath} ${moduleVars.version}
264273
.then(async ({ Events, Errors }) => {
265274
this.setOutputText(Errors || '');
266275
for (const e of Events || []) {
267-
this.setOutputText(e.Message);
276+
this.appendToOutputText(e.Message);
268277
await new Promise(resolve => setTimeout(resolve, e.Delay / 1000000));
269278
}
270279
})

tests/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ See the [command](https://pkg.go.dev/golang.org/x/website/cmd/screentest) and
7878
more information.
7979

8080
### Running the tests
81+
8182
In order to run the tests, run this command from the root of the repository:
8283

8384
```

0 commit comments

Comments
 (0)