Skip to content

Commit c8cdd2c

Browse files
committed
added new tests
1 parent 0f86ae5 commit c8cdd2c

File tree

6 files changed

+137
-5
lines changed

6 files changed

+137
-5
lines changed

.github/workflows/FUNDING.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# These are supported funding model platforms
2+
3+
github: p333ter # Váš GitHub username
4+
patreon: # Replace with a single Patreon username
5+
open_collective: # Replace with a single Open Collective username
6+
ko_fi: # Replace with a single Ko-fi username
7+
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9+
liberapay: # Replace with a single Liberapay username
10+
issuehunt: # Replace with a single IssueHunt username
11+
otechie: # Replace with a single Otechie username
12+
custom: # Replace with a single custom sponsorship URL

.github/workflows/coverage.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# .github/workflows/coverage.yml
2+
name: Coverage
3+
on: [push, pull_request]
4+
jobs:
5+
coverage:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v4
9+
- uses: actions/setup-node@v4
10+
with:
11+
node-version: '18.18.0'
12+
- run: npm ci
13+
- run: npm test
14+
- name: Coveralls
15+
uses: coverallsapp/github-action@v2
16+
with:
17+
github-token: ${{ secrets.GITHUB_TOKEN }}

.npmignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Development files
2+
coverage/
3+
example/
4+
src/__tests__/
5+
jest.config.js
6+
rollup.config.js
7+
tsconfig.json
8+
.github/
9+
.gitignore
10+
11+
# Editor files
12+
.vscode/
13+
.idea/
14+
*.sublime-project
15+
*.sublime-workspace
16+
17+
# Test files
18+
**/*.test.ts
19+
**/*.test.tsx
20+
**/*.spec.ts
21+
**/*.spec.tsx

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# @ppasmik/react-scroll-trigger
22

3-
[![npm](https://img.shields.io/npm/v/@ppasmik/react-scroll-trigger?style=flat-square)](https://www.npmjs.com/package/@ppasmik/react-scroll-trigger)
4-
[![NPM](https://img.shields.io/npm/l/@ppasmik/react-scroll-trigger?style=flat-square)](https://www.npmjs.com/package/@ppasmik/react-scroll-trigger)
5-
[![npm](https://img.shields.io/npm/dt/@ppasmik/react-scroll-trigger?style=flat-square)](https://www.npmjs.com/package/@ppasmik/react-scroll-trigger)
3+
[![npm version](https://img.shields.io/npm/v/@ppasmik/react-scroll-trigger?style=flat-square)](https://www.npmjs.com/package/@ppasmik/react-scroll-trigger)
4+
[![NPM license](https://img.shields.io/npm/l/@ppasmik/react-scroll-trigger?style=flat-square)](https://www.npmjs.com/package/@ppasmik/react-scroll-trigger)
5+
[![npm downloads](https://img.shields.io/npm/dm/@ppasmik/react-scroll-trigger?style=flat-square)](https://www.npmjs.com/package/@ppasmik/react-scroll-trigger)
6+
[![Coverage Status](https://img.shields.io/coveralls/github/p333ter/react-scroll-trigger?style=flat-square)](https://coveralls.io/github/p333ter/react-scroll-trigger)
7+
[![Build Status](https://img.shields.io/github/actions/workflow/status/p333ter/react-scroll-trigger/publish.yml?style=flat-square)](https://github.com/p333ter/react-scroll-trigger/actions)
68

79
A modern, TypeScript-based React component for monitoring scroll events and triggering callbacks when elements enter, exit, or progress through the viewport. This is a rewritten and modernized version of the original react-scroll-trigger package.
810

@@ -87,15 +89,17 @@ Standard React props (className, style, etc.) are also supported and will be pas
8789
## Technical Details
8890

8991
The component uses React hooks for efficient state management:
92+
9093
- `useRef` to track the DOM element position
9194
- `useState` for viewport visibility and scroll tracking
9295
- `useEffect` for handling scroll and resize events with proper cleanup
9396

9497
Visibility detection:
98+
9599
- Uses `getBoundingClientRect()` for accurate element position calculation
96100
- Progress is calculated based on element's position relative to viewport:
97101
```ts
98-
progress = 1 - elementRect.bottom / (viewportEnd + elementRect.height)
102+
progress = 1 - elementRect.bottom / (viewportEnd + elementRect.height);
99103
```
100104
- Velocity is derived from scroll position changes over time
101105
- All calculations are throttled (default 100ms) to optimize performance

jest.config.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,14 @@
99
'^.+\\.(ts|tsx)$': 'ts-jest',
1010
},
1111
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
12-
watchPathIgnorePatterns: ['/example/'],
12+
collectCoverage: true,
13+
collectCoverageFrom: [
14+
'src/**/*.{ts,tsx}',
15+
'!src/**/*.test.{ts,tsx}',
16+
'!src/setupTests.ts',
17+
],
18+
coverageReporters: ['json', 'lcov', 'text', 'clover'],
19+
testEnvironmentOptions: {
20+
url: 'http://localhost',
21+
},
1322
};

src/tests/ScrollTrigger.test.tsx

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,72 @@ describe('ScrollTrigger', () => {
4343
expect(onEnter).toHaveBeenCalled();
4444
});
4545
});
46+
47+
describe('ScrollTrigger', () => {
48+
const mockIntersectionObserver = () => {
49+
const mock = jest.fn();
50+
mock.mockReturnValue({
51+
observe: () => null,
52+
unobserve: () => null,
53+
disconnect: () => null,
54+
});
55+
window.IntersectionObserver = mock;
56+
};
57+
58+
beforeEach(() => {
59+
mockIntersectionObserver();
60+
});
61+
62+
it('renders children correctly', () => {
63+
const { getByText } = render(
64+
<ScrollTrigger>
65+
<div>Test content</div>
66+
</ScrollTrigger>
67+
);
68+
69+
expect(getByText('Test content')).toBeInTheDocument();
70+
});
71+
72+
it('calls onEnter when element enters viewport', () => {
73+
const onEnter = jest.fn();
74+
render(
75+
<ScrollTrigger onEnter={onEnter}>
76+
<div>Test content</div>
77+
</ScrollTrigger>
78+
);
79+
80+
// Simulate scroll
81+
act(() => {
82+
fireEvent.scroll(window);
83+
});
84+
85+
expect(onEnter).toHaveBeenCalled();
86+
});
87+
88+
it('respects custom component prop', () => {
89+
const { container } = render(
90+
<ScrollTrigger component="section">
91+
<div>Test content</div>
92+
</ScrollTrigger>
93+
);
94+
95+
const section = container.querySelector('section');
96+
expect(section).toBeInTheDocument();
97+
expect(section?.textContent).toBe('Test content');
98+
});
99+
100+
it('calls onProgress while scrolling', () => {
101+
const onProgress = jest.fn();
102+
render(
103+
<ScrollTrigger onProgress={onProgress}>
104+
<div>Test content</div>
105+
</ScrollTrigger>
106+
);
107+
108+
act(() => {
109+
fireEvent.scroll(window);
110+
});
111+
112+
expect(onProgress).toHaveBeenCalled();
113+
});
114+
});

0 commit comments

Comments
 (0)