Skip to content

Commit

Permalink
feat: add tests for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-deriv committed Oct 27, 2023
1 parent 96ab259 commit ac08604
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/modules/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export function getElementPosition(
/**
* Get the added offsetTop prop of each offsetParent up to the body
*/
function getAddedOffsetTop(element?: HTMLElement | null): number {
export function getAddedOffsetTop(element?: HTMLElement | null): number {
if (element instanceof HTMLElement) {
if (element.offsetParent instanceof HTMLElement) {
return getAddedOffsetTop(element.offsetParent) + element.offsetTop;
Expand All @@ -211,7 +211,7 @@ function getAddedOffsetTop(element?: HTMLElement | null): number {
/**
* Get the scrollTop position
*/
export function getScrollTo(element: HTMLElement, offset: number, skipFix: boolean): number {
export function getScrollTo(element: HTMLElement | null, offset: number, skipFix: boolean): number {
if (!element) {
return 0;
}
Expand Down
106 changes: 106 additions & 0 deletions test/modules/dom.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { getAddedOffsetTop, getScrollTo } from '~/modules/dom';

let element: HTMLElement | null;
let parentElement: HTMLElement | null;

describe('modules/dom', () => {
describe('getAddedOffsetTop', () => {
beforeEach(() => {
element = document.createElement('div');
parentElement = document.createElement('div');
});

it('should return 0 if element is null', () => {
const result = getAddedOffsetTop(null);

expect(result).toBe(0);
});

it('should return 0 if element has offsetTop equals to 0', () => {
const result = getAddedOffsetTop(element);

expect(result).toBe(0);
});

it('should return 10 if element has offsetTop equals to 10', () => {
Object.defineProperty(element, 'offsetTop', {
configurable: true,
value: 10,
});

const result = getAddedOffsetTop(element);

expect(result).toBe(10);
});

it('should return 50 if element has offsetTop equals to 20 and parentElement has offsetTop equals to 30', () => {
Object.defineProperty(element, 'offsetTop', {
configurable: true,
value: 20,
});
Object.defineProperty(parentElement, 'offsetTop', {
configurable: true,
value: 30,
});
Object.defineProperty(element, 'offsetParent', {
configurable: true,
value: parentElement,
});

const result = getAddedOffsetTop(element);

expect(result).toBe(50);
});
});

describe('getScrollTo', () => {
beforeEach(() => {
element = document.createElement('div');
parentElement = document.createElement('div');
document.body.appendChild(parentElement);
parentElement.appendChild(element);
});

it('should return 0 if element is null', () => {
const result = getScrollTo(null, 0, false);

expect(result).toBe(0);
});

it('should return 0 if element has offsetTop equals to 0', () => {
const result = getScrollTo(element, 0, false);

expect(result).toBe(0);
});

it('should return 20 if element has offsetTop equals to 20 and offset is 0', () => {
Object.defineProperty(element, 'offsetTop', {
configurable: true,
value: 20,
});

const result = getScrollTo(element, 0, false);

expect(result).toBe(20);
});

it('should return 50 if element has offsetTop equals to 20, parentElement has ofsetTop equalss to 30 and offset is 0', () => {
Object.defineProperty(element, 'offsetTop', {
configurable: true,
value: 20,
});
Object.defineProperty(parentElement, 'offsetTop', {
configurable: true,
value: 30,
});
Object.defineProperty(element, 'offsetParent', {
configurable: true,
value: parentElement,
});

const result = getScrollTo(element, 0, false);

expect(result).toBe(50);
});
});
});

0 comments on commit ac08604

Please sign in to comment.