I have below ponent in react. I put in short only
export interface EditCertificateProps {
id:string;
}
export function EditCertificate(props: any) {
injectStyle();
const {id} = props.match.params;
const history = useHistory();
}
When I am doing jest testing it is throwing error.
const id = '123';
describe('EditCertificate', () => {
const params = {
id: '123',
};
it('should render successfully', () => {
const { baseElement } = render(<EditCertificate id={id} />);
expect(baseElement).toBeTruthy();
});
});
it is throwing error
from another ponent this page gets called like below.
<SecureRoute path="/:id/edit" ponent={EditCertificate} />
I changed my testcase like below still error.
describe('EditCertificate', () => {
const props = {
match: {
params: 123,
},
};
it('should render successfully', () => {
const { baseElement } = render(<EditCertificate {...props.match.params} />);
expect(baseElement).toBeTruthy();
});
});
what wrong I am doing?
I have below ponent in react. I put in short only
export interface EditCertificateProps {
id:string;
}
export function EditCertificate(props: any) {
injectStyle();
const {id} = props.match.params;
const history = useHistory();
}
When I am doing jest testing it is throwing error.
const id = '123';
describe('EditCertificate', () => {
const params = {
id: '123',
};
it('should render successfully', () => {
const { baseElement } = render(<EditCertificate id={id} />);
expect(baseElement).toBeTruthy();
});
});
it is throwing error
from another ponent this page gets called like below.
<SecureRoute path="/:id/edit" ponent={EditCertificate} />
I changed my testcase like below still error.
describe('EditCertificate', () => {
const props = {
match: {
params: 123,
},
};
it('should render successfully', () => {
const { baseElement } = render(<EditCertificate {...props.match.params} />);
expect(baseElement).toBeTruthy();
});
});
what wrong I am doing?
Share Improve this question edited Feb 11, 2022 at 7:56 Shruti sharma asked Feb 11, 2022 at 7:33 Shruti sharmaShruti sharma 2119 gold badges33 silver badges92 bronze badges 8-
2
Why would that change fix anything? You still don't have a
props.match
at all, much less one with aparams
property. – jonrsharpe Commented Feb 11, 2022 at 7:37 - I renamed id to match and sent this .still getting error. ` const { baseElement } = render(<EditCertificate {...params.match} />);` – Shruti sharma Commented Feb 11, 2022 at 7:43
- 1 That makes even less sense, you're now trying to spread a string as props (which still doesn't itself have a match attribute). You need to know the basics of JSX syntax to render a ponent in this way. More broadly you need to think about the context that ponent needs to be rendered in for the hook to work. – jonrsharpe Commented Feb 11, 2022 at 7:47
-
1
The ponent under test is accessing a
props.match.params
, so it's expecting at least amatch
prop. Once you address and fix the props issue you'll next hit an issue with theuseHistory
hook and a missing routing context. – Drew Reese Commented Feb 11, 2022 at 7:55 - Hi @DrewReese I checked that as well. I have edited question at the end. could you please have a look? – Shruti sharma Commented Feb 11, 2022 at 7:56
1 Answer
Reset to default 2The EditCertificate
ponent is expecting a match
prop with a params
property.
export function EditCertificate(props: any) {
injectStyle();
const {id} = props.match.params;
const history = useHistory();
...
}
This match
prop needs to be provided in the unit test. You are creating a props object so you can just spread that into EditCertificate
. Spread the entire props
object, not props.match.params
, the latter only spreads the individual params.
describe('EditCertificate', () => {
const props = {
match: {
params: {
id: 123, // <-- props.match.params.id
},
},
};
it('should render successfully', () => {
const { baseElement } = render(<EditCertificate {...props} />);
expect(baseElement).toBeTruthy();
});
});
The next issue will be a missing routing context for the useHistory
hook. You can provide a wrapper
for the render
util, or simply wrap EditCertificate
directly.
const RouterWrapper = ({ children }) => (
<MemoryRouter>{children}</MemoryRouter> // *
);
...
const { baseElement } = render(
<EditCertificate {...props} />,
{
wrapper: RouterWrapper
},
);
or
const { baseElement } = render(
<MemoryRouter>
<EditCertificate {...props} />
</MemoryRouter>
);
* MemoryRouter
used for unit testing since there is no DOM
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745303893a4621599.html
评论列表(0条)