最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jestjs - Nativewind className is undefined in jest test - Stack Overflow

programmeradmin4浏览0评论

I am using "nativewind": "^4.1.23" in "expo": "~52.0.31", I have a simple component which uses className styling and the styles are applied as expected:

const FAB: React.FC<FABProps> = ({ style = "Surface", onPress, iconName }) => {
  const getStyleClass = () => {
    switch (style) {
      case "Primary":
        return "bg-primary";
      case "Secondary":
        return "bg-secondary";
      case "Tertiary":
        return "bg-tertiary";
      default:
        return "bg-surface";
    }
  };

  return (
    <TouchableOpacity
      onPress={onPress}
      className={`w-14 h-14 rounded-full shadow-lg ${getStyleClass()}`}
      testID="fab-button"
    >
      <View className="flex-1 justify-center items-center">
        <Icon name={iconName} size={24} />
      </View>
    </TouchableOpacity>
  );
};

When I am trying to test it using jest:

import React from "react";
import { render } from "@utils/test-utils";
import FAB from "./Fab";

describe("FAB Component", () => {
  it("renders correctly with default style", async () => {
    const { getByTestId } = await render(
      <FAB onPress={() => {}} iconName="menu" />,
    );
    const fabButton = getByTestId("fab-button");
    console.log("fabButton.props", fabButton.props);
    expect(fabButton.props.className).toContain("bg-surface");
  });
});

fabButton.props.className is always undefined. Should I do any additional setup to get the className when running jest?

fabButton.props.className should have the classNames assigned

发布评论

评论列表(0)

  1. 暂无评论