Skip to content

Commit

Permalink
[FIX] Fix ask modal not being shown after editing program (#4459)
Browse files Browse the repository at this point in the history
**Description**

I replaced all of the references to the variable `askPromptShown` to reference the variable in the editor object. This caused that when you didn't answer an ask the variable wasn't being set correctly. I also added a couple of test for the ask modal.

**Fixes #4458 **

**How to test**
The cypress should suffice, but you can also try it going to level 1 and typing the program:
 ```
print Hello
ask hello
 ```
then execute that program and write in the editor box without answering the input:
print hello

Now when you run the program again the input should be shown normally.
**Checklist**
Done? Check if you have it all in place using this list: (mark with x if done)

- [ ] Contains one of the PR categories in the name
- [ ] Describes changes in the format above
- [ ] Links to an existing issue or discussion
- [ ] Has a "How to test" section

If you're unsure about any of these, don't hesitate to ask. We're here to help!
  • Loading branch information
jpelay authored Sep 7, 2023
1 parent 1b5cc19 commit 40fac09
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 32 deletions.
13 changes: 4 additions & 9 deletions static/js/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ let last_code: string;
*/
let pygameRunning = false;

/**
* Represents whether there's an open 'ask' prompt
*/
let askPromptOpen = false;

// Many bits of code all over this file need this information globally.
// Not great but it'll do for now until we refactor this file some more.
let theAdventures: Record<string, Adventure> = {};
Expand Down Expand Up @@ -331,7 +326,7 @@ export function stopit() {
}
}

askPromptOpen = false;
theGlobalEditor.askPromptOpen = false;
}

function clearOutput() {
Expand All @@ -358,7 +353,7 @@ export async function runit(level: number, lang: string, disabled_prompt: string
// if the user changes tabs while we're waiting for a response
const adventureName = currentTab;

if (askPromptOpen) {
if (theGlobalEditor.askPromptOpen) {
// If there is no message -> don't show a prompt
if (disabled_prompt) {
return modal.notifyError(disabled_prompt);
Expand Down Expand Up @@ -1013,7 +1008,7 @@ export function runPythonProgram(this: any, code: string, sourceMap: any, hasTur
}

return new Promise(function(ok) {
askPromptOpen = true;
theGlobalEditor.askPromptOpen = true;

const input = $('#ask-modal input[type="text"]');
$('#ask-modal .caption').text(prompt);
Expand All @@ -1025,7 +1020,7 @@ export function runPythonProgram(this: any, code: string, sourceMap: any, hasTur
input.focus();
}, 0);
$('#ask-modal form').one('submit', function(event) {
askPromptOpen = false;
theGlobalEditor.askPromptOpen = false;
event.preventDefault();
$('#ask-modal').hide();

Expand Down
44 changes: 22 additions & 22 deletions static/js/appbundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion static/js/appbundle.js.map

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions tests/cypress/e2e/hedy_page/editor_box.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,45 @@ describe('Is able to type in the editor box', () => {
});
}
});

describe('Test editor box functionality', () => {
beforeEach(() => {
cy.visit(`${Cypress.env('hedy_page')}#default`);
// click on textaread to get focus
cy.get('#editor > .ace_scroller > .ace_content').click();
// empty textarea
cy.focused().clear();
});

it('Ask modal should hold input and the answer should be shown in output', () => {
cy.get('#editor').type('print Hello world\nask Hello!\necho');
cy.get('#editor > .ace_scroller > .ace_content').should('contain.text', 'print Hello worldask Hello!echo');
cy.get('#runit').click();
cy.get('#output').should('contain.text', 'Hello world');
cy.get('#ask-modal').should('be.visible');
cy.get('#ask-modal > form > div > input[type="text"]').type('Hedy!');
cy.get('#ask-modal > form > div > input[type="submit"]').click();
cy.get('#output').should('contain.text', 'Hedy!');
});

it('Ask modal shpuld be shown even when editing the program after clicking run and not answering the modal', () => {
// First we write and run the program and leave the ask modal unanswered
cy.get('#editor').type('print Hello world\nask Hello!');
// the \n is not shown as a charecter when you get the text
cy.get('#editor > .ace_scroller > .ace_content').should('contain.text', 'print Hello worldask Hello!');
cy.get('#runit').click();
cy.get('#output').should('contain.text', 'Hello world');
cy.get('#ask-modal').should('be.visible');

// Now we edit the program and the ask modal should be hidden
cy.get('#editor > .ace_scroller > .ace_content').click();
cy.focused().clear();
cy.get('#editor').type('print Hello world\nask Hello!');
cy.get('#ask-modal').should('not.be.visible');

// Running program again and it should show the modal
cy.get('#runit').click();
cy.get('#output').should('contain.text', 'Hello world');
cy.get('#ask-modal').should('be.visible');
});
});

0 comments on commit 40fac09

Please sign in to comment.