Skip to content

Rewrite UpdateContent as an ES Module#5709

Merged
kr8n3r merged 6 commits intomainfrom
rewrite-update-content-as-esm
Apr 15, 2026
Merged

Rewrite UpdateContent as an ES Module#5709
kr8n3r merged 6 commits intomainfrom
rewrite-update-content-as-esm

Conversation

@kr8n3r
Copy link
Copy Markdown
Contributor

@kr8n3r kr8n3r commented Dec 12, 2025

What

We want to migrate all of our legacy Javascript files to ESM, so this is the latest in the series of conversions.

[Trello ticket](https://trello.com/c/Us5BzeMd/1576-modernise-our-javascript-updatecontent

Rewrite follows the same conventions we previously used for RadioSelect, FocusBanner, Collapsible checkboxes, ColourPreview, FileUpload, Autofocus, PreviewPane, CopyToClipboard and others.

It replaces jQuery methods with vanilla Javascript, switching to Fetch API instead of AJAX.

Screenshot 2025-12-12 at 13 34 51

In this PR we:

  • rewrite the module as ES Module
  • update the tests to be ES Module
  • remove old module JS file
  • remove old module from the legacy bundle build

How to review

  1. best reviewed by commit
  2. run locally and test on pages with
  • single resource like "Daily email limit" /service-settings/daily-message-limit/email or filtered notifications page
  • multiple resources, like dashboard page
  1. use devtools network speed throttling to test that fetch backoff works as expected

@kr8n3r kr8n3r changed the title Rewrite UpdateContent as as ES Module Rewrite UpdateContent as an ES Module Dec 12, 2025
@kr8n3r kr8n3r marked this pull request as ready for review December 12, 2025 18:24
@kr8n3r kr8n3r force-pushed the rewrite-update-content-as-esm branch 2 times, most recently from 740f5e1 to 93be960 Compare December 12, 2025 18:43
@tombye tombye self-assigned this Dec 16, 2025
@kr8n3r kr8n3r force-pushed the rewrite-update-content-as-esm branch from c5f3cb2 to 69f5412 Compare December 18, 2025 17:02
@kr8n3r kr8n3r force-pushed the rewrite-update-content-as-esm branch 4 times, most recently from 5829e0a to 4d8126d Compare April 13, 2026 18:10
kr8n3r added 4 commits April 14, 2026 09:24
This is a rewrite following our conventions to
Class instance.

It replaces jQuery methods with vanilla Javascript,
switching to Fetch API instead of Ajax.

Additions are using try..catch block and
'getState` function which get the current state
of resouces stored in `resourceState` object.
Import and instantiate new UpdateContent module
Remove Morphom import and assignment as UpdateContent
now imports it directly.
In UpdateContent we have replace the jQuery
event fire with vanilla JS which have differing
structure, so we need to udpate this event listener
to fired event get's picked up.
This updates tests with the new module.
What tests test has not been changed.

The change is the way tests are executed and
time in them advanced.

Most of our test have an immediate response
so for them we have mock response and
mock of the fetch implementation.

For test where we test a response that takes
longer we mock a delay response in
`mockResponseDelay` and override the
default implementation.

To working with the Promise nature of fetch, we
advance time with `jest.advanceTimersByTimeAsync`
https://jestjs.io/docs/jest-object#jestadvancetimersbytimeasyncmstorun
that allows any scheduled promise callbacks
to execute before running the timers.
Note: `advanceTimersByTimeAsync` is available as
of 29.5 as of Jest and 'modern' fake timers have
been the default since 27.0.
@kr8n3r kr8n3r force-pushed the rewrite-update-content-as-esm branch from 4d8126d to f9f9f84 Compare April 14, 2026 08:24
@kr8n3r kr8n3r force-pushed the rewrite-update-content-as-esm branch from f9f9f84 to b164684 Compare April 14, 2026 08:43
Copy link
Copy Markdown
Contributor

@tombye tombye left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spotted a few leftover Promise.resolve()s from when we had to clear up promises in the js tests.

Otherwise, this is great. I particularly liked:

  • use of Set for state.renderers, to ensure it only includes unique render functions
  • use of DomParser for HTML-to-DOM-node
  • jest.advanceTimersByTimeAsync letting us to test our asynchronous code in a clear, synchronous format without hacks like extra Promise.resolve()s to tidy up

Comment thread tests/javascripts/update-content.test.mjs Outdated
Comment thread tests/javascripts/update-content.test.mjs Outdated
Co-authored-by: Tom Byers <tombaromba@gmail.com>
@kr8n3r kr8n3r force-pushed the rewrite-update-content-as-esm branch from 27fa0f0 to 589d552 Compare April 15, 2026 13:56
Copy link
Copy Markdown
Contributor

@tombye tombye left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making the changes. This now looks good to go 👍🏻

@kr8n3r kr8n3r merged commit e06a82f into main Apr 15, 2026
8 checks passed
@kr8n3r kr8n3r deleted the rewrite-update-content-as-esm branch April 15, 2026 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants