Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vdua committed Dec 18, 2024
1 parent 9ecdb90 commit 810eea4
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions test/it/focus.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
window.called = [];
// and navigator.sendBeacon has been replaced with
// a call to fakeSendBeacon
window.fakeSendBeacon = function (url, payload) {
window.fakeSendBeacon = async function (url, payload) {
// if payload is a string, we assume it's a JSON string
let payloadObject;
if (typeof payload === 'string') {
window.called.push(JSON.parse(payload));
payloadObject = JSON.parse(payload);
} else {
// it's a blob
payload.text().then((text) => {
window.called.push(JSON.parse(text));
});
const data = await payload.text();
payloadObject = JSON.parse(data);
}
if (payloadObject.checkpoint === 'click') {
window.called.push(payloadObject);
}
};

Expand Down Expand Up @@ -65,19 +68,29 @@

runTests(async () => {
describe('Test Focus Tracking', () => {

before(async () => {
// wait for enabling form events tracking
await window.wait(2000);
const form = document.querySelector('form');
form.addEventListener('submit', (event) => {
event.preventDefault();
});
})

beforeEach(async () => {
window.called = [];
});

it('can observe focus on form fields', async () => {
// wait for the adding focus event tracking
// wait for enabling form events tracking
await window.wait(2000);

const form = document.querySelector('form');
form.querySelector('input[type="text"]').focus();
await window.wait(100);

assert(window.called.some((call) => call.checkpoint === 'click'), 'click checkpoint missing');
assert(window.called.length === 1, 'no click checkpoint');
assert(window.called[0].source === `form input[type='text']`,
`source ${window.called[0].source} is not form input[type='text']`);
});

it('can not observe focus on elements outside of a form', async () => {
Expand All @@ -93,15 +106,15 @@
editableElementOutsideForm.focus();
await window.wait(100);

assert(window.called.every((call) => call.checkpoint !== 'click'), 'click checkpoint is present');
assert(window.called.length === 0, 'click checkpoint is present');
});

it('can not observe focus on non editable focusable elements', async () => {
const focusableElementInForm = document.querySelector('#focusable-element-inside-form');
focusableElementInForm.focus();
await window.wait(100);

assert(window.called.every((call) => call.checkpoint !== 'click'), 'click checkpoint is present');
assert(window.called.length === 0, 'click checkpoint is present');
});


Expand All @@ -110,10 +123,12 @@
editableElement.focus();
await window.wait(100);

assert(window.called.some((call) => call.checkpoint === 'click'), 'click checkpoint missing');
assert(window.called.length === 1, 'click checkpoint missing');
assert(window.called[0].source === `form #editable-element-inside-form`,
`source ${window.called[0].source} is not form #editable-element-inside-form`);
});

it('should send only one click checkpoint on click of a field', async () => {
it('would send two click checkpoint on click of a field', async () => {
const textInput = document.querySelector('form input[type="text"]');
const rect = textInput.getBoundingClientRect();
await sendMouse({
Expand All @@ -126,11 +141,18 @@

await window.wait(100);

const clicks = window.called.filter((call) => call.checkpoint === 'click');
assert(clicks.length === 1, `click is called ${clicks.length} (Expected 1) times on a field`);
assert(window.called.length === 2, `click is called ${window.called.length} (Expected 2) times on a field`);
assert(window.called[0].source === `form input[type='text']`,
`source ${window.called[0].source} is not form input[type='text']`);
assert(window.called[1].source === `form input[type='text']`,
`source ${window.called[1].source} is not form input[type='text']`);
});

it('should send only one click checkpoint on click of a button', async () => {
it('would send two click checkpoint on click of a button', async function test() {
if (navigator.userAgent.includes('WebKit') && !navigator.userAgent.includes('Chrome')) {
// webkit does not focus on buttons/radio-buttons/checkboxes upon click
this.skip();
}
const submitButton = document.querySelector('button');
const rect = submitButton.getBoundingClientRect();
await sendMouse({
Expand All @@ -142,8 +164,12 @@
});

await window.wait(100);
const clicks = window.called.filter((call) => call.checkpoint === 'click');
assert(clicks.length === 1, `click is called ${clicks.length} (Expected 1) times on a button`);
assert(window.called.length === 2, `click is called ${window.called.length} (Expected <=2) times on a button`);
assert(window.called[0].source === `form button[type='submit']`,
`source ${window.called[0].source} is not form button[type='submit']`);
assert(window.called[1].checkpoint === 'click', 'checkpoint is not focus');
assert(window.called[1].source === `form button[type='submit']`,
`source ${window.called[1].source} is not form button[type='submit']`);
});
});
});
Expand Down

0 comments on commit 810eea4

Please sign in to comment.