gpt4 book ai didi

javascript - 在 React Jest/Enzyme 中测试子组件的条件渲染

转载 作者:行者123 更新时间:2023-12-04 14:58:04 25 4
gpt4 key购买 nike

我正在尝试为此文件添加更多测试:

import React, { useContext } from 'react';
import UserContext from '../../contexts/user';
import styles from './index-styles.scss';

const UserLogo = ({ isMember }) =>
isMember ? (
<div className={styles.memberLogo}>&nbsp;</div>
) : (
<div className={styles.logo}>&nbsp;</div>
);

const UserDesc = ({ isMember }) => {

return isMember ? (
<p className={styles.memberDesc}>Hi Member!</p>
) : (
<p>Make An Account Today!</p>
);
};

function UserCard() {
const user = useContext(UserContext);
const userLink = 'www.google.com';

// check compatibility
const isCompatible = user.formats?.some(format => format === 'verified');
const isMember =
user.info && user.info[0].accountData?.data?.type === 'member';
if (!isCompatible || user.language !== 'en') {
return null;
}

return (
<div className={styles.userCard}>
<div className={styles.title}>Welcome!</div>
<UserLogo isMember={isMember} />
<UserDesc isMember={isMember} />
<a
className={styles.userLink}
href={userLink}
>
Click Here!
</a>
</div>
);
}

export default UserCard;

目前我有这些测试:

import React from 'react';
import { mount } from 'enzyme';
import UserCard from '.';
import UserDesc from '.';
import { UserProvider } from '../../contexts/user';

function UserCardWithUser(props) {
const user = {
id: '12345',
};

return (
<UserProvider value={{ ...buzz, ...(props.buzz || {}) }}>
<UserCard />
</UserProvider>
);
}

jest.mock('react', () => {
return {
...jest.requireActual('react'),
useContext: jest.fn(),
};
});

describe('user card', () => {
beforeEach(async () => {
jest.resetAllMocks();
React.useContext.mockReturnValue({
getExperimentValue: jest.fn().mockReturnValue('control'),
});
});

it('should render on eligable user', () => {
const user = {
formats: ['verified'],
info: [
{
accountData: {
data: {
type: 'member',
},
},
},
],
language: 'en'
};
const component = mount(<UserCardWithUser user={user} />);
component.update();
expect(component.find('UserCard').exists()).toBe(true);
});

it('should not render when user is ineligible', () => {
const userParts = [
{
bfpFormats: ['na'],
formats: [
{
accountData: {
data: {
type: 'member',
},
},
},
],
language: 'en'
},
];

for (const userPart of userParts) {
const component = mount(<UserCardWithUser user={userParts} />);
component.update();
expect(component.isEmptyRender()).toBe(true);
}
});
});

但是我希望能够测试 UserLogo 和 UserDesc 中涉及的条件呈现。我试过这个:

 const isMember = true;
const component = mount(<UserDesc isMember={isMember}/>);
expect(component.exists('.memberDesc')).toBe(true);

当 isMember 为真时检查类名是否存在,但它似乎没有找到类名。我想知道这是否是因为 css 选择器“样式”。

最佳答案

您可以在 expect 语句之前尝试 console.log(component.find('p').debug()) 以准确查看样式的显示方式在测试中。然后您应该能够在您的 expect 中看到要查找的选择器。

我无法以与您相同的方式导入 .scss 文件,但这是我的测试文件和输出。

样式.test.tsx

import React, { FC } from 'react';
import { mount } from 'enzyme';
import '../styles/index-styles.scss';

const UserDesc: FC<{ isMember: boolean }> = ({ isMember }) => {
return isMember ? <p className={'memberDesc'}>Hi Member!</p> : <p>Make An Account Today!</p>;
};

test('UserDesc renders with style if true', () => {
const isMember = true;
const component = mount(<UserDesc isMember={isMember} />);
console.log(component.find('p').debug());
expect(component.find('.memberDesc')).toHaveLength(1);
});

test('UserDesc does not render with style if false', () => {
const isMember = false;
const component = mount(<UserDesc isMember={isMember} />);
console.log(component.find('p').debug());
expect(component.find('.memberDesc')).toHaveLength(0);
});

输出

PASS  src/__test__/ClassTest.test.tsx
● Console

console.log
<p className="memberDesc">
Hi Member!
</p>

at Object.<anonymous> (src/__test__/ClassTest.test.tsx:12:13)

console.log
<p>
Make An Account Today!
</p>

at Object.<anonymous> (src/__test__/ClassTest.test.tsx:19:13)

关于javascript - 在 React Jest/Enzyme 中测试子组件的条件渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67537497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com