Skip to content

Unit testing widths for different screen sizes #1429

@dan-pzbp

Description

@dan-pzbp

Ask your Question

I currently have a React Native project which uses Tailwind/NativeWind for applying different widths depending on the screen size.

const MyComponent = () => (
    <View className="w-16 lg:m-32" testID="testID">
        <Text title="Hello world" />
    </View>
);

In my current unit test, I've got something along the lines of...

it("should have a width of 64px", () => {
   const { getByTestId } = render(<MyComponent />);
    const foundBodyElement = getByTestId("testID");
    expect((foundBodyElement.props as ElementProps).style).toEqual(expect.arrayContaining([
      expect.objectContaining({
        width: 64,
      })
    ]));
});

I'd like to also have a test for the the large screens to assert they have a larger width... something along the lines of:

it("should have a width of 128px", () => {
   const { getByTestId } = render(<MyComponent />);
    const foundBodyElement = getByTestId("testID");
    expect((foundBodyElement.props as ElementProps).style).toEqual(expect.arrayContaining([
      expect.objectContaining({
        width: 128,
      })
    ]));
});

However, I'm not sure how I can get the second test to think it's on a larger screen so that the larger class name gets applied.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions