-
Notifications
You must be signed in to change notification settings - Fork 39
/
test.js
113 lines (94 loc) · 3.25 KB
/
test.js
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
104
105
106
107
108
109
110
111
112
113
// @flow
import React from 'react';
import PropTypes from 'prop-types';
import theme from './';
import styled, {ThemeProvider} from 'styled-components';
import ReactTestRenderer from 'react-test-renderer';
function render(jsx) {
return ReactTestRenderer.create(jsx).toJSON();
}
test('basic', () => {
const backgroundColor = theme('mode', {
light: '#fff',
dark: '#000'
});
const borderColor = theme('mode', {
light: props => props.theme.accent.light,
dark: props => props.theme.accent.dark,
});
const Page = styled.div`
background-color: ${backgroundColor};
border-color: ${borderColor};
`;
const accent = {
light: 'skyblue',
dark: 'royalblue'
};
expect(render(
<ThemeProvider theme={{ mode: 'light', accent }}>
<Page/>
</ThemeProvider>
)).toMatchSnapshot();
expect(render(
<ThemeProvider theme={{ mode: 'dark', accent }}>
<Page/>
</ThemeProvider>
)).toMatchSnapshot();
});
test('variants', () => {
const headingColor = theme.variants('mode', 'variant', {
default: { light: '#000', dark: '#fff' },
fancy: {
light: props => props.theme.accent.light,
dark: props => props.theme.accent.dark,
},
});
const Heading = styled.h1`
color: ${headingColor};
`;
Heading.propTypes = { variant: PropTypes.oneOf(['default', 'fancy']) };
Heading.defaultProps = { variant: 'default' };
const accent = {
light: 'skyblue',
dark: 'royalblue'
};
expect(render(
<ThemeProvider theme={{ mode: 'light', accent }}>
<Heading/>
</ThemeProvider>
)).toMatchSnapshot();
expect(render(
<ThemeProvider theme={{ mode: 'light', accent }}>
<Heading variant="fancy"/>
</ThemeProvider>
)).toMatchSnapshot();
});
describe('theme()', () => {
const fn = theme('mode', { light: '#fff', dark: '#000' });
it('should create a function that returns a matching prop when passed a string', () => {
expect(fn({ theme: { mode: 'light' } })).toBe('#fff');
expect(fn({ theme: { mode: 'dark' } })).toBe('#000');
});
it('should create a function that returns calls a passed function', () => {
expect(fn({ theme: { mode: themes => themes.light } })).toBe('#fff');
expect(fn({ theme: { mode: themes => themes.dark } })).toBe('#000');
});
});
describe('theme.variants()', () => {
const fn = theme.variants('mode', 'kind', {
default: { light: '#fff', dark: '#000' },
fancy: { light: '#f0f', dark: '#0f0' },
});
it('should create a function that returns a matching prop when passed a string', () => {
expect(fn({ kind: 'default', theme: { mode: 'light' } })).toBe('#fff');
expect(fn({ kind: 'default', theme: { mode: 'dark' } })).toBe('#000');
expect(fn({ kind: 'fancy', theme: { mode: 'light' } })).toBe('#f0f');
expect(fn({ kind: 'fancy', theme: { mode: 'dark' } })).toBe('#0f0');
});
it('should create a function that returns calls a passed function', () => {
expect(fn({ kind: 'default', theme: { mode: themes => themes.light } })).toBe('#fff');
expect(fn({ kind: 'default', theme: { mode: themes => themes.dark } })).toBe('#000');
expect(fn({ kind: 'fancy', theme: { mode: themes => themes.light } })).toBe('#f0f');
expect(fn({ kind: 'fancy', theme: { mode: themes => themes.dark } })).toBe('#0f0');
});
});