我有一个styled-component
正在使用的styled-system
并且运行良好,但是当我去测试组件时,我似乎无法让它从主题中正确填充样式。
这很奇怪,因为如果我应用marginLeft={1}
这种风格的道具会从Theme.space
属性中正确翻译。这是color
系统的具体问题吗?
文本组件
import React from 'react';
import styled from 'styled-components/native';
import {
color,
space,
typography,
variant,
system,
} from 'styled-system';
const textDecoration = system({ textDecoration: true });
const textDecorationColor = system({ textDecorationColor: true });
const textVariant = variant({ scale: 'text' });
const Text = styled.Text`
${textVariant}
${textDecoration}
${textDecorationColor}
${color} <-- using color system
${space}
${typography}
`;
export default Text;
测试
import React from 'react';
import renderer from 'react-test-renderer';
import { ThemeProvider } from 'styled-components';
import Text from '../src/atoms/Text';
import Theme from '../src/theme';
test('Text is referencing Theme (colors)', () => {
console.log('Theme navy -->', Theme.colors.navy); // Theme navy --> #004175
const tree = renderer
.create(
<ThemeProvider theme={Theme}>
<Text color="navy" />
</ThemeProvider>,
).toJSON();
const [styles] = tree.props?.style || {};
console.log('Post-render color -->', styles.color); // Post-render color --> navy
expect(styles.color).toBe(Theme.colors.navy);
});
npm run test
...
● Text is referencing Theme (colors)
expect(received).toBe(expected) // Object.is equality
Expected: "#004175"
Received: "navy"
...
通常我会测试color="primary"
which is#3592fb
但我只是得到一个错误,指出这primary
不是 style-property 的有效值,color
它告诉我ThemeProvider
无法正常工作。
对此的任何帮助将不胜感激!谢谢