forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Text.test.tsx
92 lines (74 loc) · 2.77 KB
/
Text.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import * as React from 'react';
import { render } from '@testing-library/react-native';
import renderer from 'react-test-renderer';
import Provider from '../../../core/Provider';
import configureFonts from '../../../styles/fonts';
import { MD3LightTheme } from '../../../styles/themes';
import { tokens } from '../../../styles/themes/v3/tokens';
import Text, { customText } from '../../Typography/Text';
const content = 'Something rendered as a child content';
it('renders every variant of Text with children as content', () => {
const variants = (
<>
<Text variant="displayLarge">{content}</Text>
<Text variant="displayMedium">{content}</Text>
<Text variant="displaySmall">{content}</Text>
<Text variant="headlineLarge">{content}</Text>
<Text variant="headlineMedium">{content}</Text>
<Text variant="headlineSmall">{content}</Text>
<Text variant="titleLarge">{content}</Text>
<Text variant="titleMedium">{content}</Text>
<Text variant="titleSmall">{content}</Text>
<Text variant="bodyLarge">{content}</Text>
<Text variant="bodyMedium">{content}</Text>
<Text variant="bodySmall">{content}</Text>
<Text variant="labelLarge">{content}</Text>
<Text variant="labelMedium">{content}</Text>
<Text variant="labelSmall">{content}</Text>
</>
);
const tree = renderer.create(variants).toJSON();
expect(tree).toMatchSnapshot();
});
it('renders v3 Text component without variant with default fontWeight and fontFamily', () => {
const { getByTestId } = render(
<Text testID="text-without-variant">{content}</Text>
);
const { brandRegular, weightRegular } = tokens.md.ref.typeface;
expect(getByTestId('text-without-variant')).toHaveStyle({
fontFamily: brandRegular,
fontWeight: weightRegular,
});
});
it('renders v3 Text component with custom variant correctly', () => {
const fontConfig = {
customVariant: {
fontFamily: 'Montserrat-Regular',
fontWeight: '400',
letterSpacing: 0.51,
lineHeight: 54.1,
fontSize: 41,
},
} as const;
const theme = {
...MD3LightTheme,
fonts: configureFonts({ config: fontConfig }),
};
const Text = customText<'customVariant'>();
const { getByTestId } = render(
<Provider theme={theme}>
<Text testID="text-with-custom-variant" variant="customVariant">
{content}
</Text>
</Provider>
);
expect(getByTestId('text-with-custom-variant').props.style).toMatchSnapshot();
});
it('throws when custom variant not provided', () => {
jest.spyOn(console, 'error').mockImplementation(() => {});
const Text = customText<'myCustomVariant'>();
expect(() =>
render(<Text variant="myCustomVariant">{content}</Text>)
).toThrow(/myCustomVariant was not provided/);
jest.clearAllMocks();
});