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: predefine loading of sampleRUM.enhance() #105

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions src/block-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* governing permissions and limitations under the License.
*/

import { sampleRUM } from '@adobe/helix-rum-js';
import { loadCSS, wrapTextNodes } from './dom-utils.js';

/**
Expand Down Expand Up @@ -177,5 +178,8 @@ export async function loadSections(element) {
for (let i = 0; i < sections.length; i += 1) {
// eslint-disable-next-line no-await-in-loop
await loadSection(sections[i]);
if (i === 0 && sampleRUM.enhance) {
sampleRUM.enhance();
}
}
}
64 changes: 64 additions & 0 deletions test/block-loader/loadBlocks.withrum.test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!DOCTYPE html>
<html>
<body>
<main>
<div class="section">
<div class="wrapper">
<div class="block block1" data-block-name="simpleblock"><p>This is the first block</p></div>
</div>
<div class="wrapper">
<div class="block block2" data-block-name="asyncblock"><p>This is the second block</p></div>
</div>
</div>
<div class="section">
<div class="wrapper">
<div class="block block3" data-block-name="simpleblock"><p>This is the third block</p></div>
</div>
<div class="wrapper">
<div class="block block4" data-block-name="asyncblock"><p>This is the fourth block</p></div>
</div>
</div>
</main>

<script type="module">
/* eslint-env mocha */
import { runTests } from '@web/test-runner-mocha';
import { expect } from '@esm-bundle/chai';
import { sampleRUM } from '@adobe/helix-rum-js';
import { loadSections } from '../../src/block-loader.js';

window.hlx = {};

runTests(() => {
beforeEach(() => {
const usp = new URLSearchParams(window.location.search);
usp.append('rum', 'on');
window.history.replaceState({}, '', `${window.location.pathname}?${usp.toString()}`);
});

afterEach(() => {
const usp = new URLSearchParams(window.location.search);
usp.delete('rum');
window.history.replaceState({}, '', `${window.location.pathname}?${usp.toString()}`);
// eslint-disable-next-line no-underscore-dangle
window.hlx.rum = undefined;

const enhancer = document.querySelector('script[src*="rum-enhancer"]');
if (enhancer) {
enhancer.remove();
}
});

it('loadBlocks - loads all blocks', async () => {
window.hlx.codeBasePath = '/test/fixtures';
sampleRUM();

await loadSections(document.querySelector('main'));

// loadSections should have run "sampleRUM.enhance()"
expect(document.querySelector('script[src*="rum-enhancer"]')).to.exist;
});
});
</script>
</body>
</html>