Files
edx-platform/lms/static/completion/js/spec/ViewedEvent_spec.js
Syed Ali Abbas Zaidi 8480dbc228 chore: apply amnesty on existing not fixable issues (#32215)
* fix: eslint operator-linebreak issue

* fix: eslint quotes issue

* fix: react jsx indent and props issues

* fix: eslint trailing spaces issues

* fix: eslint line around directives issue

* fix: eslint semi rule

* fix: eslint newline per chain rule

* fix: eslint space infix ops rule

* fix: eslint space-in-parens issue

* fix: eslint space before function paren issue

* fix: eslint space before blocks issue

* fix: eslint arrow body style issue

* fix: eslint dot-location issue

* fix: eslint quotes issue

* fix: eslint quote props issue

* fix: eslint operator assignment issue

* fix: eslint new line after import issue

* fix: indent issues

* fix: operator assignment issue

* fix: all autofixable eslint issues

* fix: all react related fixable issues

* fix: autofixable eslint issues

* chore: remove all template literals

* fix: remaining autofixable issues

* chore: apply amnesty on all existing issues

* fix: failing xss-lint issues

* refactor: apply amnesty on remaining issues

* refactor: apply amnesty on new issues

* fix: remove file level suppressions

* refactor: apply amnesty on new issues
2023-08-07 19:13:19 +05:00

110 lines
4.1 KiB
JavaScript

import { ElementViewing, ViewedEventTracker } from '../ViewedEvent';
describe('ViewedTracker', () => {
let existingHTML;
beforeEach(() => {
existingHTML = document.body.innerHTML;
});
afterEach(() => {
document.body.innerHTML = existingHTML;
});
it('calls the handlers when an element is viewed', () => {
document.body.innerHTML = '<div id="d1"></div><div id="d2"></div><div id="d3"></div>';
const tracker = new ViewedEventTracker();
// eslint-disable-next-line no-restricted-syntax
for (const element of document.getElementsByTagName('div')) {
tracker.addElement(element, 1000);
}
// eslint-disable-next-line no-undef
const handlerSpy = jasmine.createSpy('handlerSpy');
tracker.addHandler(handlerSpy);
const elvIter = tracker.elementViewings.values();
// Pick two elements, and mock them so that one has met the criteria to be viewed,
// and the other hasn't.
const viewed = elvIter.next().value;
// eslint-disable-next-line no-undef
spyOn(viewed, 'areViewedCriteriaMet').and.returnValue(true);
viewed.checkIfViewed();
expect(handlerSpy).toHaveBeenCalledWith(viewed.el, {
elementHasBeenViewed: true,
});
const unviewed = elvIter.next().value;
// eslint-disable-next-line no-undef
spyOn(unviewed, 'areViewedCriteriaMet').and.returnValue(false);
unviewed.checkIfViewed();
// eslint-disable-next-line no-undef
expect(handlerSpy).not.toHaveBeenCalledWith(unviewed.el, jasmine.anything());
});
});
describe('ElementViewing', () => {
beforeEach(() => {
// eslint-disable-next-line no-undef
jasmine.clock().install();
});
afterEach(() => {
// eslint-disable-next-line no-undef
jasmine.clock().uninstall();
});
it('calls checkIfViewed when enough time has elapsed', () => {
const viewing = new ElementViewing({}, 500, () => {});
// eslint-disable-next-line no-undef
spyOn(viewing, 'checkIfViewed').and.callThrough();
viewing.seenForMs = 250;
viewing.handleVisible();
// eslint-disable-next-line no-undef
jasmine.clock().tick(249);
expect(viewing.checkIfViewed).not.toHaveBeenCalled();
// eslint-disable-next-line no-undef
jasmine.clock().tick(1);
expect(viewing.checkIfViewed).toHaveBeenCalled();
});
it('has been viewed after the specified number of milliseconds', () => {
const viewing = new ElementViewing({}, 500, () => {});
viewing.seenForMs = 250;
// eslint-disable-next-line no-undef
spyOn(Date, 'now').and.returnValue(750);
viewing.handleVisible();
viewing.markTopSeen();
viewing.markBottomSeen();
Date.now.and.returnValue(999);
viewing.checkIfViewed();
expect(viewing.hasBeenViewed).toBeFalsy();
Date.now.and.returnValue(1000);
// eslint-disable-next-line no-undef
jasmine.clock().tick(250);
expect(viewing.hasBeenViewed).toBeTruthy();
});
it('has not been viewed if the bottom has not been seen', () => {
const viewing = new ElementViewing(undefined, 500, () => {});
viewing.markTopSeen();
viewing.seenForMs = 500;
expect(viewing.areViewedCriteriaMet()).toBeFalsy();
viewing.checkIfViewed();
expect(viewing.hasBeenViewed).toBeFalsy();
});
it('has not been viewed if the top has not been seen', () => {
const viewing = new ElementViewing(undefined, 500, () => {});
viewing.markBottomSeen();
viewing.seenForMs = 500;
expect(viewing.areViewedCriteriaMet()).toBeFalsy();
viewing.checkIfViewed();
expect(viewing.hasBeenViewed).toBeFalsy();
});
it('does not update time seen if lastSeen is undefined', () => {
const viewing = new ElementViewing(undefined, 500, () => {});
viewing.becameVisibleAt = undefined;
expect(viewing.becameVisibleAt).toBeUndefined();
viewing.handleVisible();
expect(viewing.becameVisibleAt).not.toBeUndefined();
});
});