Skip to content

Commit 79fe809

Browse files
committed
Revert "feat(): Now supporting all RTL queries"
This reverts commit ddfb3ba.
1 parent 40f15cf commit 79fe809

File tree

12 files changed

+677
-32
lines changed

12 files changed

+677
-32
lines changed

package-lock.json

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

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@
3636
"devDependencies": {
3737
"@babel/preset-env": "^7.16.7",
3838
"@babel/preset-react": "^7.16.7",
39+
"@testing-library/jest-dom": "^5.16.1",
3940
"@testing-library/react": "^12.1.2",
4041
"@types/jest": "^27.4.0",
4142
"@types/react": "^17.0.38",
4243
"babel-jest": "^27.4.5",
4344
"jest": "^27.4.5",
4445
"react": "^17.0.2",
4546
"react-test-renderer": "^17.0.2",
47+
"ts-jest": "^27.1.2",
4648
"typescript": "^4.5.4"
4749
},
4850
"peerDependencies": {
@@ -51,7 +53,11 @@
5153
"@testing-library/react": "*"
5254
},
5355
"jest": {
54-
"testEnvironment": "jest-environment-jsdom"
56+
"testEnvironment": "jest-environment-jsdom",
57+
"preset": "ts-jest",
58+
"setupFilesAfterEnv": [
59+
"<rootDir>/src/jest-setup.ts"
60+
]
5561
},
5662
"release": {
5763
"branches": ["master"]

src/bad-setup.spec.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { setupMockComponent } from './index';
2+
import MockedComponent from './test-components/MockedComponent';
3+
4+
it('should throw an exception if the component is not a mock', () => {
5+
expect.assertions(1);
6+
try {
7+
setupMockComponent(MockedComponent);
8+
} catch (err: any) {
9+
expect(err.message).toEqual("MockedComponent cannot be setup because it is not a Jest mock. Call \"jest.mock('path/to/component')\" first");
10+
}
11+
});

src/happy-path.spec.tsx

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { render, screen } from '@testing-library/react';
2+
import React, { useEffect, useState } from 'react';
3+
import { setupMockComponent, getByMockComponent } from './index';
4+
import { findAllByMockComponent, findByMockComponent, getAllByMockComponent, queryAllByMockComponent, queryByMockComponent } from './queries';
5+
import MockedComponent from './test-components/MockedComponent';
6+
7+
jest.mock('./test-components/MockedComponent');
8+
9+
const TestedComponent = ({value}: {value: string}) => {
10+
return (
11+
<div>
12+
<MockedComponent value={value}/>
13+
</div>
14+
);
15+
};
16+
17+
beforeEach(() => {
18+
setupMockComponent(MockedComponent);
19+
});
20+
21+
it('should render a div', () => {
22+
render(<TestedComponent value='val' />);
23+
24+
expect(getByMockComponent(MockedComponent).tagName).toEqual('DIV');
25+
});
26+
27+
it('should have the prop held', () => {
28+
render(<TestedComponent value='val' />);
29+
30+
expect(getByMockComponent(MockedComponent).props.value).toEqual('val');
31+
});
32+
33+
it('should hold the last value on a re-render', () => {
34+
const {rerender} = render(<TestedComponent value='val' />);
35+
rerender(<TestedComponent value='new-val' />);
36+
37+
expect(getByMockComponent(MockedComponent).props.value).toEqual('new-val');
38+
});
39+
40+
it('should find via getByMockComponent', () => {
41+
render(<TestedComponent value='val' />);
42+
43+
expect(getByMockComponent(MockedComponent)).toBeInTheDocument();
44+
});
45+
46+
it('should find via getAllByMockComponent', () => {
47+
render(
48+
<div>
49+
<TestedComponent value='val' />
50+
<TestedComponent value='val' />
51+
</div>
52+
);
53+
const all = getAllByMockComponent(MockedComponent);
54+
expect(all).toHaveLength(2);
55+
expect(all[0]).toBeInTheDocument();
56+
expect(all[1]).toBeInTheDocument();
57+
expect(all[0]).not.toBe(all[2]);
58+
});
59+
60+
it('should find via queryByMockComponent', () => {
61+
render(<TestedComponent value='val' />);
62+
63+
expect(queryByMockComponent(MockedComponent)).toBeInTheDocument();
64+
});
65+
66+
it('should find via queryAllByMockComponent', () => {
67+
render(
68+
<div>
69+
<TestedComponent value='val' />
70+
<TestedComponent value='val' />
71+
</div>
72+
);
73+
const all = queryAllByMockComponent(MockedComponent);
74+
expect(all).toHaveLength(2);
75+
expect(all[0]).toBeInTheDocument();
76+
expect(all[1]).toBeInTheDocument();
77+
expect(all[0]).not.toBe(all[2]);
78+
});
79+
80+
it('should find via findByMockComponent', async () => {
81+
const TimedFunction = () => {
82+
const [visible, setVisible] = useState(false);
83+
useEffect(() => {
84+
const handle = setTimeout(() => setVisible(true), 10);
85+
return () => clearTimeout(handle);
86+
}, []);
87+
return <div>{visible && <MockedComponent value='' />}</div>;
88+
};
89+
render(<TimedFunction />);
90+
91+
expect(await findByMockComponent(MockedComponent)).toBeInTheDocument();
92+
});
93+
94+
it('should find via findAllByMockComponent', async () => {
95+
const TimedFunction = () => {
96+
const [visible, setVisible] = useState(false);
97+
useEffect(() => {
98+
const handle = setTimeout(() => setVisible(true), 10);
99+
return () => clearTimeout(handle);
100+
}, []);
101+
return (
102+
<div>
103+
{visible && <span><MockedComponent value='' /><MockedComponent value='' /></span>}
104+
</div>
105+
);
106+
};
107+
render(<TimedFunction />);
108+
109+
const all = await findAllByMockComponent(MockedComponent);
110+
expect(all).toHaveLength(2);
111+
expect(all[0]).toBeInTheDocument();
112+
expect(all[1]).toBeInTheDocument();
113+
expect(all[0]).not.toBe(all[2]);
114+
});

src/hello.spec.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/hello.spec.tsx

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export function setupMockComponent() {
2-
console.log('Setting up mock component');
3-
}
1+
export {setupMockComponent} from './setupMockComponent';
2+
export * from './queries';

src/jest-setup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@testing-library/jest-dom';

src/not-found-tests.spec.tsx

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { render, screen } from '@testing-library/react';
2+
import React, { useEffect, useState } from 'react';
3+
import { setupMockComponent, getByMockComponent } from './index';
4+
import { findAllByMockComponent, findByMockComponent, getAllByMockComponent, queryAllByMockComponent, queryByMockComponent } from './queries';
5+
import MockedComponent from './test-components/MockedComponent';
6+
7+
jest.mock('./test-components/MockedComponent');
8+
9+
const TestedComponent = () => {
10+
return <div>Nothing Here</div>;
11+
};
12+
13+
beforeEach(() => {
14+
setupMockComponent(MockedComponent);
15+
});
16+
17+
it('should error if getByMockComponent cannot find it', () => {
18+
render(<TestedComponent />);
19+
20+
expect(() => getByMockComponent(MockedComponent)).toThrow();
21+
});
22+
23+
it('should error if multiple components exist', () => {
24+
render(<div><MockedComponent /><MockedComponent /></div>);
25+
26+
expect(() => getByMockComponent(MockedComponent)).toThrow();
27+
});
28+
29+
it('should error if getAllByMockComponent cannot find it', () => {
30+
render(<TestedComponent />);
31+
32+
expect(() => getAllByMockComponent(MockedComponent)).toThrow();
33+
});
34+
35+
it('should return null if queryByMockComponent cannot find it', () => {
36+
render(<TestedComponent />);
37+
38+
expect(queryByMockComponent(MockedComponent)).toBeNull();
39+
});
40+
41+
it('should return an empty array if queryAllByMockComponent cannot find it', () => {
42+
render(<TestedComponent />);
43+
44+
expect(queryAllByMockComponent(MockedComponent)).toEqual([]);
45+
});
46+
47+
it('should reject if findByMockComponent cannot find it', async () => {
48+
render(<TestedComponent />);
49+
50+
expect(findByMockComponent(MockedComponent)).rejects.toBeDefined();
51+
});
52+
53+
it('should reject if findByMockComponent finds multiple', async () => {
54+
render(<div><MockedComponent /><MockedComponent /></div>);
55+
56+
expect(findByMockComponent(MockedComponent)).rejects.toBeDefined();
57+
});
58+
59+
it('should reject if findAllByMockComponent cannot find it', async () => {
60+
render(<TestedComponent />);
61+
62+
expect(findAllByMockComponent(MockedComponent)).rejects.toBeDefined();
63+
});

src/queries.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { FC } from "react";
2+
import { MockedElement } from "./setupMockComponent";
3+
import {MatcherFunction, screen} from '@testing-library/react';
4+
5+
function buildMatcherFunctionForComponent<PropType>(mockComponent: FC<PropType>): MatcherFunction {
6+
return (_, element) => {
7+
const mockedElement = element as unknown as MockedElement<PropType>;
8+
return mockedElement.component === mockComponent;
9+
};
10+
}
11+
12+
/**
13+
* Attempt to find the mocked element. Throws if not exactly one is found.
14+
*/
15+
export function getByMockComponent<PropType>(mockComponent: FC<PropType>): MockedElement<PropType> {
16+
return screen.getByTestId(buildMatcherFunctionForComponent(mockComponent));
17+
}
18+
19+
/**
20+
* Attempt to find all mocked elements. Throws if none found.
21+
*/
22+
export function getAllByMockComponent<PropType>(mockComponent: FC<PropType>): MockedElement<PropType>[] {
23+
return screen.getAllByTestId(buildMatcherFunctionForComponent(mockComponent));
24+
}
25+
26+
/**
27+
* Attempt to find the mocked element. Returns null if not found. Throws if multiple found.
28+
*/
29+
export function queryByMockComponent<PropType>(mockComponent: FC<PropType>): MockedElement<PropType> | null {
30+
return screen.queryByTestId(buildMatcherFunctionForComponent(mockComponent));
31+
}
32+
33+
/**
34+
* Attempt to find all mocked element. Returns empty array if not found.
35+
*/
36+
export function queryAllByMockComponent<PropType>(mockComponent: FC<PropType>): MockedElement<PropType>[] {
37+
return screen.queryAllByTestId(buildMatcherFunctionForComponent(mockComponent));
38+
}
39+
40+
/**
41+
* Attempt to find the mocked element. Reject if not eventually found or multiple are found.
42+
*/
43+
export function findByMockComponent<PropType>(mockComponent: FC<PropType>): Promise<MockedElement<PropType>> {
44+
return screen.findByTestId(buildMatcherFunctionForComponent(mockComponent));
45+
}
46+
47+
/**
48+
* Attempt to find all the mocked element. Rejects if non are eventually found.
49+
*/
50+
export function findAllByMockComponent<PropType>(mockComponent: FC<PropType>): Promise<MockedElement<PropType>[]> {
51+
return screen.findAllByTestId(buildMatcherFunctionForComponent(mockComponent));
52+
}

0 commit comments

Comments
 (0)