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

test advanced checkpoints #230

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 test/it/block.plain.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
Copy link
Contributor

Choose a reason for hiding this comment

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

Move file into a fixtures subfolder to separate test files and resources (same for image)

<img src="/test/it/fire.jpg" height="100" width="100">
I'm a fragment
</div>
Binary file added test/it/fire.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
127 changes: 127 additions & 0 deletions test/it/loadblock.test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<html>

<head>
<title>Test Runner</title>
<script>
// we load from localhost, and have the ability to
// change the scripts that are being served. Check the
// web-test-runner.config.js file for details
window.RUM_BASE = window.origin;
// we log what's being sent to the "server"
window.called = [];
// and navigator.sendBeacon has been replaced with
// a call to fakeSendBeacon
window.fakeSendBeacon = function (url, payload) {
// if payload is a string, we assume it's a JSON string
if (typeof payload === 'string') {
window.called.push(JSON.parse(payload));
} else {
// it's a blob
payload.text().then((text) => {
window.called.push(JSON.parse(text));
});
}
};
</script>
<script defer type="text/javascript" src="/.rum/@adobe/helix-rum-js@^2/dist/rum-standalone.js"></script>
</head>

<body>
<div class="block" data-block-status="loaded">
The first block
<img src="/test/it/fire.jpg" height="200" width="200">
</div>
<div class="block" data-block-status="loaded">
<form action="javascript:false;" method="POST">
<input type="text" name="name" value="John Doe">
<input type="submit" value="Submit">
</form>
<script>
document.querySelector('form').addEventListener('submit', (e) => {
e.preventDefault();
// turn the contents uppercase
e.target.name.value = e.target.name.value.toUpperCase();
});
</script>
</div>
<script type="module">
import { runTests } from '@web/test-runner-mocha';
import { sendMouse, setViewport } from '@web/test-runner-commands';
import { assert } from '@esm-bundle/chai';

runTests(async () => {
describe('Test Block Loading', () => {
it('Can observe blocks that enter the viewport', async () => {

await setViewport({ width: 800, height: 600 });

await sendMouse({ type: 'click', position: [100, 100] });

await new Promise((resolve) => {
setTimeout(resolve, 0);
});

// let's start injecting "blocks", "forms", and "media"
// into the page

const block = document.createElement('div');
block.className = 'block';

const contents = await fetch('/test/it/block.plain.html');
block.innerHTML = await contents.text();

block.dataset.blockStatus = 'loaded';

document.body.appendChild(block);


await new Promise((resolve) => {
setTimeout(resolve, 100);
});

await sendMouse({ type: 'click', position: [10, 10] });

assert(window.called.some((call) => call.checkpoint === 'viewmedia'), 'viewmedia checkpoint missing');
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bad practice: the tests share the same execution context, i.e. they can interfere between each others. This one can for example trigger a missingresource you do not check here. If implementation for next test is broken (i.e. fetch does not fire a missingresouce, test will be successful but implementation is wrong.

Several todos:

  • before / after each test, clean up the called stack
  • in each test, test for exact equality - if order is random, at least test for window.called.length === 4

If this is not doable in one file, split into multiple files

assert(window.called.some((call) => call.checkpoint === 'viewblock'), 'viewblock checkpoint missing');
assert(window.called.some((call) => call.checkpoint === 'loadresource'), 'loadresource checkpoint missing');
assert(window.called.some((call) => call.checkpoint === 'click'), 'click checkpoint missing');
});

it('Can track missing resources', async function test() {
// if user agent is webkit, we skip this test
if (navigator.userAgent.includes('WebKit') && !navigator.userAgent.includes('Chrome')) {
this.skip();
}
// same for firefox
if (navigator.userAgent.includes('Firefox')) {
this.skip();
}

const placeholders = await fetch('/test/it/placeholders.json');

await new Promise((resolve) => {
setTimeout(resolve, 100);
});

assert(window.called.some((call) => call.checkpoint === 'missingresource'), 'missingresource checkpoint missing');
Copy link
Contributor

Choose a reason for hiding this comment

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

You are testing missingresource in a loadblock test file. Either rename the file or move to dedicated test file.

});
});

it('Can track form submit events', async function test() {
const form = document.querySelector('form');

await new Promise((resolve) => {
setTimeout(resolve, 2000);
});

// find the form field
form.querySelector('input[type="submit"]').click();

await new Promise((resolve) => {
setTimeout(resolve, 2000);
});
assert(window.called.some((call) => call.checkpoint === 'formsubmit'), 'formsubmit checkpoint missing');
Copy link
Contributor

Choose a reason for hiding this comment

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

Same, testing formsubmit in loadblockfile...

});
});
</script>
</body>
Loading