Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#9413): update dynamic url widget to use MutationObserver #9414

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions tests/integration/cht-form/default/dynamic-url-widget.wdio-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const mockConfig = require('../mock-config');
const commonEnketoPage = require('@page-objects/default/enketo/common-enketo.wdio.page');
const genericForm = require('@page-objects/default/enketo/generic-form.wdio.page');

describe('cht-form web component - Dynamic URL Widget', () => {
it('supports displaying a dynamic markdown link in a note', async () => {
const searchQuery = 'why is Java best';

await mockConfig.loadForm('default', 'test', 'dynamic-url-widget');
await commonEnketoPage.setInputValue('Enter your search query', searchQuery);
await genericForm.nextPage();

expect(await commonEnketoPage.isElementDisplayed('label', `Click the link to search: ${searchQuery}`)).to.be.true;
const linkValue = await(await $('a.dynamic-url')).getAttribute('href');
expect(linkValue).to.equal(`http://google.com?q=${searchQuery}`);
});
});
Binary file not shown.
39 changes: 39 additions & 0 deletions tests/integration/cht-form/default/forms/dynamic-url-widget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:orx="http://openrosa.org/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<h:head>
<h:title>Dynamic URL</h:title>
<model>
<itext>
<translation lang="en">
<text id="/dynamic-url-widget/query_text:label">
<value>Enter your search query</value>
</text>
<text id="/dynamic-url-widget/search_link:label">
<value>Click the link to search: [<output value=" /dynamic-url-widget/query_text "/>](http://google.com?q=<output value=" /dynamic-url-widget/query_text "/>)</value>
</text>
</translation>
</itext>
<instance>
<dynamic-url-widget delimiter="#" id="dynamic-url-widget" prefix="J1!dynamic-url-widget!" version="2024-09-06 00:00:00">
<query_text/>
<search_link/>
<meta tag="hidden">
<instanceID/>
</meta>
</dynamic-url-widget>
</instance>
<instance id="contact-summary"/>
<bind nodeset="/dynamic-url-widget/query_text" type="string"/>
<bind nodeset="/dynamic-url-widget/search_link" readonly="true()" type="string"/>
<bind calculate="concat('uuid:', uuid())" nodeset="/dynamic-url-widget/meta/instanceID" readonly="true()" type="string"/>
</model>
</h:head>
<h:body class="pages">
<input ref="/dynamic-url-widget/query_text">
<label ref="jr:itext('/dynamic-url-widget/query_text:label')"/>
</input>
<input ref="/dynamic-url-widget/search_link">
<label ref="jr:itext('/dynamic-url-widget/search_link:label')"/>
</input>
</h:body>
</h:html>
15 changes: 14 additions & 1 deletion webapp/src/js/enketo/widgets/dynamic-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,20 @@ class DynamicUrlWidget extends Widget {
const urlElement = currentElement.find('.url');
const setHref = () => currentElement.attr('href', urlElement.text());
setHref();
urlElement.on('DOMSubtreeModified', setHref);

const observer = new MutationObserver(mutationList => {
mutationList.forEach(mutation => {
if (mutation.type === 'childList' || mutation.type === 'characterData') {
jkuester marked this conversation as resolved.
Show resolved Hide resolved
setHref();
}
});
});

observer.observe(urlElement[0], {
childList: true, // Monitor direct child nodes
characterData: true, // Monitor text content changes
subtree: true // Monitor all descendants
});
}
}

Expand Down
7 changes: 5 additions & 2 deletions webapp/tests/karma/js/enketo/widgets/dynamic-url.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ describe('Enketo: Dynamic URL Widget', () => {
expect($(DynamicUrlWidget.selector).attr('href')).to.equal('http://google.com?q=');
});

it('should update the href for the dynamically generated URL when the dynamic value changes', () => {
it('should update the href for the dynamically generated URL when the dynamic value changes', (done) => {
buildHtml('helloWorld');

new DynamicUrlWidget($(DynamicUrlWidget.selector)[0]);
const dynamic = 'worldHello';
$('.url span').text(dynamic);

expect($(DynamicUrlWidget.selector).attr('href')).to.equal(`http://google.com?q=${dynamic}`);
setTimeout(() => {
expect($(DynamicUrlWidget.selector).attr('href')).to.equal(`http://google.com?q=${dynamic}`);
done();
}, 0);
Comment on lines +50 to +53
Copy link
Contributor

Choose a reason for hiding this comment

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

So just for the record (for anyone that is looking into this in the future) the normal Angular way of testing async UI updates via fakeAsync does not seem to work here. And from reading online this seems to be expected (I guess the MutationObserver falls outside of what Angular has control of). In general the consensus seems to be against trying too hard to unit test MutationObserver behavior. However, since this works (at least for now) I say we ship it as is! 👍

});

it('should not modify elements besides dynamic-url links', () => {
Expand Down
Loading