FIX: Update raw and cooked immediate after edit (PR #13387)

Cooked field used to be updated immediately, but raw was updated by other means. The post stream was refreshed twice, once after saving and another one after the whole process almost finished.

GitHub

That is very confusing. async / await is supposed to make asynchronous code less complicated. Any chances this can be done in a simpler way?

This pull request has been mentioned on Discourse Meta. There might be relevant details there:

This needs a cleanup step to ensure no state leaks between test cases

Sorry @riking / @ZogStriP , making the executive call of merging this, @nbianca can you follow up with a subsequent PR?

Unfortunately, this level of complexity is needed here because this test ensures that the post is staged while the request is in flight. I had a choice to make, do it by modifying our non-test code to let me “pause” requests or do this, which is not as complicated as it looks. I think the first solution was a lot more complex.

What this code does is to have “two threads”, one for the UI and one for the request and use two promises to signal each other. The assertions will not run only a request is in flight and the post is “staged”. The request will not finish until all assertions ran.

Step by step execution:

  • [UI] go to topic, open edit composer, fill in
  • [UI] create promise 1
  • [UI] press click on “Edit Button” and wait for promise 1
  • [REQ] pretender was called, it means that the request started - resolve promise 1 and wait for promise 2
  • [UI] continue for assertions
  • [UI] resolve promise 2
  • [REQ] return request response
  • [UI] visit another page to wait for all promises and finish

@riking, what state can leak in between the tests? The last visit call ensures that all promises and requests were resolved.

I’m referring to adding window.resolveLastPromise = null; somewhere. This is relevant if an assert fails somewhere in the middle, and the unfired window.resolveLastPromise will leak into the other tests.