Skip to content

Commit 027bbf4

Browse files
committed
test: getExpandedStyle tests
1 parent 958d978 commit 027bbf4

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

src/components/ParallaxBanner/components/ParallaxBannerLayer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const ParallaxBannerLayer = (
2828
} = rest;
2929

3030
const imageStyle = getImageStyle(props);
31-
const expandedStyle = getExpandedStyle(expanded, props);
31+
const expandedStyle = expanded ? getExpandedStyle(props) : {};
3232
const parallax = useParallax<HTMLDivElement>({
3333
targetElement: props.targetElement,
3434
shouldDisableScalingTranslations: true,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { BannerLayer } from '../types';
2+
import { getExpandedStyle } from './getExpandedStyle';
3+
4+
describe('given getExpandedStyle', () => {
5+
describe.each([
6+
[{ translateY: [0, 100] } as BannerLayer, { top: '-500px', bottom: '0px' }],
7+
[{ translateY: [0, 0] } as BannerLayer, { top: '0px', bottom: '0px' }],
8+
[
9+
{ translateY: [10, -20] } as BannerLayer,
10+
{ top: '-100px', bottom: '-50px' },
11+
],
12+
[
13+
{ translateY: ['-100%', '100%'] } as BannerLayer,
14+
{ top: '-500px', bottom: '-500px' },
15+
],
16+
[
17+
{ translateY: ['-50%', '200%'] } as BannerLayer,
18+
{ top: '-1000px', bottom: '-250px' },
19+
],
20+
[
21+
{ translateY: ['10px', '-20px'] } as BannerLayer,
22+
{ top: '-20px', bottom: '-10px' },
23+
],
24+
[
25+
{ translateY: ['-10px', '20px'] } as BannerLayer,
26+
{ top: '-20px', bottom: '-10px' },
27+
],
28+
[{ speed: -20 } as BannerLayer, { top: '-200px', bottom: '-200px' }],
29+
[{ speed: 10 } as BannerLayer, { top: '-100px', bottom: '-100px' }],
30+
])('when props %s are given', (props, expected) => {
31+
test('then it returns expected', () => {
32+
const div = document.createElement('div');
33+
div.getBoundingClientRect = jest.fn(() => ({ height: 500 } as DOMRect));
34+
const targetElement = div;
35+
expect(getExpandedStyle({ targetElement, ...props })).toEqual(expected);
36+
});
37+
});
38+
});
Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
import { parseValueAndUnit } from 'parallax-controller';
22
import { BannerLayer } from '../types';
33

4-
export function getExpandedStyle(expanded: boolean, layer: BannerLayer) {
5-
if (!expanded) {
6-
return {};
7-
}
4+
const FALLBACK_RECT = {
5+
height: 0,
6+
};
7+
8+
export function getExpandedStyle(layer: BannerLayer) {
89
if (Array.isArray(layer.translateY)) {
910
const translateYStart = parseValueAndUnit(layer.translateY[0]);
1011
const translateYEnd = parseValueAndUnit(layer.translateY[1]);
12+
1113
if (translateYStart.unit === 'px' && translateYEnd.unit === 'px') {
1214
return {
1315
top: `${Math.abs(translateYEnd.value) * -1}px`,
1416
bottom: `${Math.abs(translateYStart.value) * -1}px`,
1517
};
1618
}
19+
20+
if (translateYStart.unit === '%' && translateYEnd.unit === '%') {
21+
const clientRect =
22+
layer.targetElement?.getBoundingClientRect() || FALLBACK_RECT;
23+
const top = Math.abs(clientRect.height * 0.01 * translateYEnd.value) * -1;
24+
const bottom =
25+
Math.abs(clientRect.height * 0.01 * translateYStart.value) * -1;
26+
return {
27+
top: `${top}px`,
28+
bottom: `${bottom}px`,
29+
};
30+
}
1731
}
1832
if (layer.speed) {
1933
const speed = layer.speed || 0;
@@ -23,6 +37,5 @@ export function getExpandedStyle(expanded: boolean, layer: BannerLayer) {
2337
bottom: Math.abs(speed) * 10 * -1 + 'px',
2438
};
2539
}
26-
2740
return {};
2841
}

0 commit comments

Comments
 (0)