Skip to content

Commit 6c1c1d7

Browse files
committed
Added tests
1 parent b55eb79 commit 6c1c1d7

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

.vscode/launch.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Debug Jest Tests",
6+
"type": "node",
7+
"request": "launch",
8+
"runtimeArgs": [
9+
"--inspect-brk",
10+
"${workspaceRoot}/node_modules/jest/bin/jest.js",
11+
"--runInBand"
12+
],
13+
"console": "integratedTerminal",
14+
"internalConsoleOptions": "neverOpen"
15+
}
16+
]
17+
}

jest.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
transform: { '^.+\\.ts?$': 'ts-jest' },
3+
testEnvironment: 'jsdom',
4+
testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$',
5+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
6+
verbose: true,
7+
};

tests/index.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { HoverBox } from '../src/Index';
2+
3+
jest.useFakeTimers();
4+
5+
describe('testing index file', () => {
6+
let boxEl: HTMLDivElement, hoverBox: HoverBox;
7+
8+
boxEl = document.createElement('div');
9+
10+
boxEl.classList.add('test');
11+
boxEl = document.body.insertAdjacentElement(
12+
'beforeend',
13+
boxEl,
14+
) as HTMLInputElement;
15+
16+
// Add HoverData (which would be revealed)
17+
const div = document.createElement('div');
18+
div.classList.add('HoverData');
19+
div.innerHTML = 'Test data';
20+
div.hidden = true;
21+
boxEl.appendChild(div);
22+
23+
describe('Post-setup checks', () => {
24+
hoverBox = new HoverBox('.test', {
25+
transitionDelay: 0,
26+
transitionDuration: 0,
27+
});
28+
29+
it('has added element', () => {
30+
expect(boxEl).not.toBeNull();
31+
});
32+
33+
it('has created hoverBox', () => {
34+
expect(hoverBox).toBeDefined();
35+
});
36+
});
37+
38+
test('Initial state = stopped', () =>
39+
// @ts-ignore
40+
expect(hoverBox._listening).toBe(true));
41+
42+
describe('Event', () => {
43+
const eventFired = boxEl.dispatchEvent(new Event('mouseenter'));
44+
45+
jest.advanceTimersByTime(1);
46+
47+
it('should fire', () => expect(eventFired).toBe(true));
48+
});
49+
50+
describe('After event - hoverContainer', () => {
51+
const hoverCont = document.getElementById(boxEl.dataset.hoverBoxId!);
52+
53+
it('exists', () => expect(hoverCont).toBeDefined());
54+
55+
it('is on display', () =>
56+
expect(hoverCont?.style.display).toBe('block'));
57+
58+
// Jest doesn't have a renderer so just make sure it's defined (always 0px)
59+
it('has position', () => expect(hoverCont?.style.top).toBeDefined());
60+
});
61+
});

0 commit comments

Comments
 (0)