import React from "react";
import { fireEvent, render } from "@testing-library/react-native";
import { I18nManager } from "react-native";
import { Typography } from "./Typography";
it("renders text with no additional props", () => {
const typography = render(Test Text);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with regular style", () => {
const typography = render(
Test Text,
);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with bold style", () => {
const typography = render(
Test Text,
);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with italic style", () => {
const typography = render(
Test Text,
);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with bold weight and italic style", () => {
const typography = render(
Test Text
,
);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with bold style and display as fontFamily", () => {
const typography = render(
Test Text
,
);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with extraBold weight and display as fontFamily", () => {
const typography = render(
Test Text
,
);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with black style and display as fontFamily", () => {
const typography = render(
Test Text
,
);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with uppercase transform", () => {
const typography = render(
Test Text,
);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with lowercase transform", () => {
const typography = render(
Test Text,
);
expect(typography.toJSON()).toMatchSnapshot();
});
it("supports nested children and applies transform only to string children", () => {
const typography = render(
before Inner after
,
);
expect(typography.toJSON()).toMatchSnapshot();
});
it("allows child Typography to control its own transform", () => {
const view = render(
{"before "}
Inner
{" after"}
,
).toJSON();
expect(view).toMatchSnapshot();
});
it("supports multi-level nesting across Typography and Text", () => {
const view = render(
{"level1 "}
and INNER
{" end"}
,
).toJSON();
expect(view).toMatchSnapshot();
});
it("applies transform to parent strings only", () => {
const { getByText } = render(
{"test "}
inner
,
);
expect(getByText(/TEST/)).toBeDefined();
expect(getByText("inner")).toBeDefined();
});
it("allows child transform to override parent transform", () => {
const { getByText } = render(
before INNER after
,
);
expect(getByText(/BEFORE/)).toBeDefined();
expect(getByText("inner")).toBeDefined();
expect(getByText(/AFTER/)).toBeDefined();
});
it("renders text with white color", () => {
const typography = render(Test Text);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with green color", () => {
const typography = render(Test Text);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with semantic color", () => {
const typography = render(
Test Text,
);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with default color", () => {
const typography = render(Test Text);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with center align", () => {
const typography = render(Test Text);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text respecting the text direction", () => {
I18nManager.isRTL = true;
const typography = render(Test Text);
expect(typography.toJSON()).toMatchSnapshot();
I18nManager.isRTL = false;
});
it("renders text with small size", () => {
const typography = render(Test Text);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with large size", () => {
const typography = render(Test Text);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with default size", () => {
const typography = render(Test Text);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with multiple properties", () => {
const typography = render(
Test Text
,
);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with line height override", () => {
const typography = render(
Test Text,
);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with letter spacing", () => {
const typography = render(
Test Text,
);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with reverseTheme true with reversible color", () => {
const typography = render(
Test Text
,
);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with reverseTheme false with reversible color", () => {
const typography = render(
Test Text
,
);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with adjustsFontSizeToFit set to true", () => {
const typography = render(
Test Text that happens to be longer than the rest of the text. This just
keeps going on and on. maxLines being set will make this work its magic
,
);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text accessibilityRole", () => {
const typography = render(
Test Text,
);
expect(typography.getByRole("text")).toBeDefined();
});
it("renders header accessibilityRole", () => {
const typography = render(
Test Text,
);
expect(typography.getByRole("header")).toBeDefined();
});
it("renders text using the maxLines is also passed", () => {
const typography = render(
Test Text that happens to be longer than the rest of the text. This just
keeps going on and on. maxLines being set will make this work its magic
,
);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text with strikethough styling", () => {
const typography = render(
Test Text,
);
expect(typography.toJSON()).toMatchSnapshot();
});
it("renders text that is inaccessible", () => {
const typography = render(
Test Text,
);
expect(typography.root.props).toEqual(
expect.objectContaining({
accessible: false,
accessibilityRole: "none",
importantForAccessibility: "no-hide-descendants",
}),
);
});
it("applies custom UNSAFE_style to text", () => {
const customStyle = { color: "red", fontSize: 20 };
const typography = render(
Test Text
,
);
const textElement = typography.getByText("Test Text");
expect(textElement.props.style).toEqual(
expect.arrayContaining([expect.objectContaining(customStyle)]),
);
});
describe("underline", () => {
it.each(["solid", "double", "dotted", "dashed"] as const)(
"renders text with %s underline",
underlineStyle => {
const typography = render(
Test Text,
);
expect(typography.toJSON()).toMatchSnapshot();
},
);
});
describe("onTextLayout", () => {
it("calls onTextLayout callback when text layout event occurs", () => {
const onTextLayoutMock = jest.fn();
const { getByRole } = render(
Test Text,
);
const textElement = getByRole("text");
const mockEvent = {
nativeEvent: {
lines: [
{
text: "Test Text",
x: 0,
y: 0,
width: 100,
height: 20,
ascender: 15,
descender: -5,
capHeight: 14,
xHeight: 10,
},
],
},
};
fireEvent(textElement, "onTextLayout", mockEvent);
expect(onTextLayoutMock).toHaveBeenCalledTimes(1);
expect(onTextLayoutMock).toHaveBeenCalledWith(mockEvent);
});
});
describe("TypographyGestureDetector", () => {
it("wraps text with TypographyGestureDetector by default (collapsable=false)", () => {
const { getByRole } = render(Test Text);
const textElement = getByRole("text");
expect(textElement.props.collapsable).toBe(false);
});
it("wraps text with TypographyGestureDetector (collapsable=false) when selectable=true", () => {
const { getByRole } = render(
Test Text,
);
const textElement = getByRole("text");
expect(textElement.props.collapsable).toBe(false);
});
it("does not wrap text with TypographyGestureDetector when selectable=false", () => {
const { getByRole } = render(
Test Text,
);
const textElement = getByRole("text");
expect(textElement.props.collapsable).toBeUndefined();
});
});