0

我有一个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无法正常工作。

对此的任何帮助将不胜感激!谢谢

4

1 回答 1

1

经过一番进一步的谷歌搜索,事实证明我应该使用来自的 ThemeProviderstyled-components/native

// change
import { ThemeProvider } from 'styled-components';
// to
import { ThemeProvider } from 'styled-components/native';
于 2020-10-13T15:17:28.330 回答