forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToggleButton.test.tsx
103 lines (86 loc) · 2.86 KB
/
ToggleButton.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
93
94
95
96
97
98
99
100
101
102
103
import * as React from 'react';
import { Animated } from 'react-native';
import { render } from '@testing-library/react-native';
import color from 'color';
import renderer from 'react-test-renderer';
import { getTheme } from '../../core/theming';
import { tokens } from '../../styles/themes/v3/tokens';
import ToggleButton from '../ToggleButton';
import { getToggleButtonColor } from '../ToggleButton/utils';
it('renders toggle button', () => {
const tree = renderer
.create(<ToggleButton status="checked" onPress={() => {}} icon="heart" />)
.toJSON();
expect(tree).toMatchSnapshot();
});
it('renders disabled toggle button', () => {
const tree = renderer
.create(
<ToggleButton disabled value="toggle" status="checked" icon="heart" />
)
.toJSON();
expect(tree).toMatchSnapshot();
});
it('renders unchecked toggle button', () => {
const tree = renderer
.create(<ToggleButton disabled status="unchecked" icon="heart" />)
.toJSON();
expect(tree).toMatchSnapshot();
});
describe('getToggleButtonColor', () => {
it('should return correct color when checked and theme version 3', () => {
expect(getToggleButtonColor({ theme: getTheme(), checked: true })).toBe(
color(getTheme().colors.onSecondaryContainer)
.alpha(tokens.md.ref.opacity.level2)
.rgb()
.string()
);
});
it('should return correct color when checked and theme version 3, dark theme', () => {
expect(getToggleButtonColor({ theme: getTheme(true), checked: true })).toBe(
color(getTheme(true).colors.onSecondaryContainer)
.alpha(tokens.md.ref.opacity.level2)
.rgb()
.string()
);
});
it('should return correct color when checked and theme version 2', () => {
expect(
getToggleButtonColor({ theme: getTheme(false, false), checked: true })
).toBe('rgba(0, 0, 0, .08)');
});
it('should return correct color when checked and theme version 2, dark theme', () => {
expect(
getToggleButtonColor({ theme: getTheme(true, false), checked: true })
).toBe('rgba(255, 255, 255, .12)');
});
it('should return transparent color when not checked', () => {
expect(getToggleButtonColor({ theme: getTheme(), checked: false })).toBe(
'transparent'
);
});
});
it('animated value changes correctly', () => {
const value = new Animated.Value(1);
const { getByTestId } = render(
<ToggleButton
disabled
status="unchecked"
icon="heart"
testID="toggle-button"
style={[{ transform: [{ scale: value }] }]}
/>
);
expect(getByTestId('toggle-button-container')).toHaveStyle({
transform: [{ scale: 1 }],
});
Animated.timing(value, {
toValue: 1.5,
useNativeDriver: false,
duration: 200,
}).start();
jest.advanceTimersByTime(200);
expect(getByTestId('toggle-button-container')).toHaveStyle({
transform: [{ scale: 1.5 }],
});
});