Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit a01ea6d

Browse files
Add baseStyle tests for component
1 parent f203fe3 commit a01ea6d

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`createStyledAttrsMixin baseStyle should be overiden by props 1`] = `
4+
<DocumentFragment>
5+
.emotion-0 {
6+
background: var(--colors-blue-200);
7+
color: var(--colors-gray-200);
8+
}
9+
10+
<div
11+
class="emotion-0"
12+
data-chakra-component="FakeComponent"
13+
>
14+
Fake component
15+
</div>
16+
</DocumentFragment>
17+
`;
18+
19+
exports[`createStyledAttrsMixin baseStyle should use theme.baseStyle if given 1`] = `
20+
<DocumentFragment>
21+
.emotion-0 {
22+
background: var(--colors-red-400);
23+
color: var(--colors-gray-200);
24+
}
25+
26+
<div
27+
class="emotion-0"
28+
data-chakra-component="FakeComponent"
29+
>
30+
Fake component
31+
</div>
32+
</DocumentFragment>
33+
`;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import defaultTheme from '@chakra-ui/theme-vue'
2+
import { toCSSVar } from '@chakra-ui/styled-system'
3+
import { createStyledAttrsMixin } from './components'
4+
import { render } from '@/tests/test-utils'
5+
6+
describe('createStyledAttrsMixin', () => {
7+
const FakeComponent = {
8+
name: 'FakeComponent',
9+
mixins: [createStyledAttrsMixin('FakeComponent', true)],
10+
render (h) {
11+
return h(
12+
'div',
13+
{
14+
class: this.className,
15+
attrs: this.computedAttrs
16+
},
17+
'Fake component'
18+
)
19+
}
20+
}
21+
22+
describe('baseStyle', () => {
23+
const renderFakeComponent = ({ theme, ...props }) => {
24+
const inlineAttrs = (props && props.inlineAttrs) || ''
25+
return render({
26+
template: `<FakeComponent ${inlineAttrs} />`,
27+
components: {
28+
FakeComponent
29+
},
30+
provide: () => ({
31+
$chakraTheme: () => toCSSVar(theme),
32+
$chakraColorMode: () => 'light'
33+
}),
34+
...props
35+
})
36+
}
37+
38+
it('should use theme.baseStyle if given', () => {
39+
const { asFragment } = renderFakeComponent({
40+
theme: {
41+
...defaultTheme,
42+
baseStyle: {
43+
FakeComponent: {
44+
bg: 'red.400',
45+
color: 'gray.200'
46+
}
47+
}
48+
}
49+
})
50+
expect(asFragment()).toMatchSnapshot()
51+
})
52+
53+
it('should be overiden by props', () => {
54+
const { asFragment } = renderFakeComponent({
55+
inlineAttrs: 'bg="blue.200"',
56+
theme: {
57+
...defaultTheme,
58+
baseStyle: {
59+
FakeComponent: {
60+
bg: 'red.400',
61+
color: 'gray.200'
62+
}
63+
}
64+
}
65+
})
66+
expect(asFragment()).toMatchSnapshot()
67+
})
68+
})
69+
})

packages/chakra-ui-core/src/utils/components.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export const createStyledAttrsMixin = name => ({
6969
}
7070
},
7171
baseStyle () {
72-
return __get(this.theme, `baseStyle.${name}`, {})
72+
return __get(this.theme, `baseStyle.${name}`) || {}
7373
},
7474
className () {
7575
const { styleAttrs } = this.splitProps

0 commit comments

Comments
 (0)