Skip to content

Commit 6035f50

Browse files
committed
fix: tests
1 parent e64f547 commit 6035f50

File tree

9 files changed

+95
-43
lines changed

9 files changed

+95
-43
lines changed

jest-setup.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1+
// Import jest-dom to extend expect matchers
12
import '@testing-library/jest-dom';
23

3-
// Declare global for TypeScript
4-
declare const global: any;
5-
64
// Mock ResizeObserver for tests
7-
global.ResizeObserver = jest.fn().mockImplementation(() => ({
5+
(global as any).ResizeObserver = jest.fn().mockImplementation(() => ({
86
observe: jest.fn(),
97
unobserve: jest.fn(),
108
disconnect: jest.fn(),

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
"@storybook/react": "^6.4.20",
126126
"@testing-library/jest-dom": "^5.16.1",
127127
"@testing-library/react": "^12.1.2",
128+
"@testing-library/react-hooks": "^8.0.1",
128129
"@types/jest": "^27.0.3",
129130
"@types/react": "^17.0.35",
130131
"@types/react-dom": "^17.0.11",

pnpm-lock.yaml

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/Parallax/index.test.tsx

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ describe('given the <Parallax> component', () => {
108108
wrapper: Wrapper,
109109
});
110110
expect(asFragment()).toMatchSnapshot();
111-
expect(controller.createElement).toBeCalledWith({
111+
expect(controller.createElement).toHaveBeenCalledWith({
112112
el: expect.any(HTMLElement),
113113
props,
114114
});
@@ -126,7 +126,7 @@ describe('given the <Parallax> component', () => {
126126
<Parallax translateY={[-100, 100]} />
127127
</MockProvider>
128128
);
129-
expect(controller.createElement).toBeCalledWith({
129+
expect(controller.createElement).toHaveBeenCalledWith({
130130
el: expect.any(HTMLElement),
131131
props: { translateY: [-100, 100] },
132132
});
@@ -147,7 +147,7 @@ describe('given the <Parallax> component', () => {
147147
);
148148
const element = controller.getElements()[0];
149149
unmount();
150-
expect(controller.removeElementById).toBeCalledWith(element.id);
150+
expect(controller.removeElementById).toHaveBeenCalledWith(element.id);
151151
});
152152

153153
it('then it updates an element in the controller when receiving relevant new props', () => {
@@ -178,11 +178,14 @@ describe('given the <Parallax> component', () => {
178178

179179
const element = controller.getElements()[0];
180180

181-
expect(controller.updateElementPropsById).toBeCalledWith(element.id, {
182-
disabled: false,
183-
translateX: [100, -100],
184-
translateY: [-100, 100],
185-
});
181+
expect(controller.updateElementPropsById).toHaveBeenCalledWith(
182+
element.id,
183+
{
184+
disabled: false,
185+
translateX: [100, -100],
186+
translateY: [-100, 100],
187+
}
188+
);
186189

187190
const newProps = {
188191
disabled: false,
@@ -198,11 +201,14 @@ describe('given the <Parallax> component', () => {
198201
/>
199202
);
200203

201-
expect(controller.updateElementPropsById).toBeCalledWith(element.id, {
202-
disabled: false,
203-
translateX: [-40, -60],
204-
translateY: [10, 80],
205-
});
204+
expect(controller.updateElementPropsById).toHaveBeenCalledWith(
205+
element.id,
206+
{
207+
disabled: false,
208+
translateX: [-40, -60],
209+
translateY: [10, 80],
210+
}
211+
);
206212

207213
// only update with valid props
208214
rerender(
@@ -254,12 +260,15 @@ describe('given the <Parallax> component', () => {
254260

255261
const element = controller.getElements()[0];
256262

257-
expect(controller.resetElementStyles).toBeCalledWith(element);
258-
expect(controller.updateElementPropsById).toBeCalledWith(element.id, {
259-
disabled: true,
260-
translateX: [100, -100],
261-
translateY: [-100, 100],
262-
});
263+
expect(controller.resetElementStyles).toHaveBeenCalledWith(element);
264+
expect(controller.updateElementPropsById).toHaveBeenCalledWith(
265+
element.id,
266+
{
267+
disabled: true,
268+
translateX: [100, -100],
269+
translateY: [-100, 100],
270+
}
271+
);
263272
});
264273

265274
it('then it resets styles on an element if the disabled prop is true', () => {
@@ -290,7 +299,7 @@ describe('given the <Parallax> component', () => {
290299
<Parallax disabled={true} translateX={offX} translateY={offY} />
291300
);
292301

293-
expect(controller.resetElementStyles).toBeCalled();
302+
expect(controller.resetElementStyles).toHaveBeenCalled();
294303
});
295304
});
296305
});

src/components/ParallaxBanner/index.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('given a <ParallaxBanner> component', () => {
4747
}
4848
);
4949
expect(asFragment()).toMatchSnapshot();
50-
expect(controller.createElement).toBeCalledWith({
50+
expect(controller.createElement).toHaveBeenCalledWith({
5151
el: expect.any(HTMLElement),
5252
props: {
5353
...props,
@@ -74,7 +74,7 @@ describe('given a <ParallaxBanner> component', () => {
7474
render(<ParallaxBanner layers={[{ children: <div /> }]} />, {
7575
wrapper: Wrapper,
7676
});
77-
expect(controller.createElement).toBeCalledWith({
77+
expect(controller.createElement).toHaveBeenCalledWith({
7878
el: expect.any(HTMLElement),
7979
props: {
8080
shouldDisableScalingTranslations: true,

src/components/ParallaxProvider/index.test.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('A <ParallaxProvider>', () => {
2929

3030
render();
3131

32-
expect(child).toBeCalled();
32+
expect(child).toHaveBeenCalled();
3333
});
3434

3535
it('to pass the controller context', () => {
@@ -90,7 +90,7 @@ describe('A <ParallaxProvider>', () => {
9090
expect(
9191
// @ts-expect-error
9292
parallaxController.enableParallaxController
93-
).toBeCalled();
93+
).toHaveBeenCalled();
9494
});
9595

9696
it('to destroy the controller when unmounting', () => {
@@ -112,8 +112,8 @@ describe('A <ParallaxProvider>', () => {
112112
screen.unmount();
113113

114114
expect(
115-
((parallaxController as unknown) as ParallaxController)?.destroy
116-
).toBeCalled();
115+
(parallaxController as unknown as ParallaxController)?.destroy
116+
).toHaveBeenCalled();
117117
});
118118

119119
it('to update the scroll container when receiving a new container el', () => {
@@ -142,7 +142,7 @@ describe('A <ParallaxProvider>', () => {
142142

143143
screen.unmount();
144144
// @ts-expect-error
145-
expect(parallaxController?.updateScrollContainer).toBeCalledWith(el);
145+
expect(parallaxController?.updateScrollContainer).toHaveBeenCalledWith(el);
146146
});
147147

148148
// NOTE: I think this test can be removed

src/hooks/useParallaxController.test.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ const Wrapper = (props: PropsWithChildren<{}>) => (
1111
);
1212

1313
describe('given useParallaxController hook', () => {
14-
const { window } = global;
14+
const { window } = global as any;
1515
afterEach(() => {
16-
global.window = window;
16+
(global as any).window = window;
1717
});
1818
describe.skip('when the window is undefined', () => {
1919
test('then it should return null', () => {
2020
try {
2121
const { result } = renderHook(() => {
22-
// @ts-expect-error
23-
delete global.window;
22+
delete (global as any).window;
2423
return useParallaxController();
2524
});
2625
expect(result.current).toBe(null);

src/testUtils/MockProvider.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import React, { Component } from 'react';
2-
import PropTypes from 'prop-types';
1+
import React, { Component, PropsWithChildren } from 'react';
32
import { ParallaxContext } from '../../src/context/ParallaxContext';
43
import { ParallaxController } from 'parallax-controller';
54

6-
export class MockProvider extends Component {
7-
static propTypes = {
8-
children: PropTypes.node.isRequired,
9-
controllerMock: PropTypes.object.isRequired,
10-
};
5+
interface MockProviderProps {
6+
controllerMock: ParallaxController;
7+
}
8+
9+
export class MockProvider extends Component<
10+
PropsWithChildren<MockProviderProps>
11+
> {
1112
controller: ParallaxController;
1213

13-
constructor(props: { controllerMock: ParallaxController }) {
14+
constructor(props: PropsWithChildren<MockProviderProps>) {
1415
super(props);
1516
this.controller = props.controllerMock;
1617
}

src/types/global.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Global type declarations
2+
declare global {
3+
const global: any;
4+
}

0 commit comments

Comments
 (0)