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

Commit 28ffffd

Browse files
committed
test: add tests for mode function
1 parent 1dc99b8 commit 28ffffd

File tree

5 files changed

+124
-17
lines changed

5 files changed

+124
-17
lines changed

.storybook/config.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,18 @@ import {
2929
feSend,
3030
feServer
3131
} from 'feather-icons-paths'
32+
import { mode } from '../packages/chakra-ui-core/src/utils/color-mode-observer';
3233

3334
Vue.use(Chakra, {
35+
extendTheme: {
36+
baseStyle: {
37+
CButton: ({ colorMode, theme }) => ({
38+
bg: mode('tomato', 'hotpink'),
39+
borderRadius: "lg",
40+
shadow: 'xl'
41+
})
42+
}
43+
},
3444
icons: {
3545
iconSet: {
3646
feAnchor,
@@ -60,7 +70,7 @@ Vue.use(Chakra, {
6070
path: `<path fill="currentColor" d="M297.216 243.2c0 15.616-11.52 28.416-26.112 28.416-14.336 0-26.112-12.8-26.112-28.416s11.52-28.416 26.112-28.416c14.592 0 26.112 12.8 26.112 28.416zm-119.552-28.416c-14.592 0-26.112 12.8-26.112 28.416s11.776 28.416 26.112 28.416c14.592 0 26.112-12.8 26.112-28.416.256-15.616-11.52-28.416-26.112-28.416zM448 52.736V512c-64.494-56.994-43.868-38.128-118.784-107.776l13.568 47.36H52.48C23.552 451.584 0 428.032 0 398.848V52.736C0 23.552 23.552 0 52.48 0h343.04C424.448 0 448 23.552 448 52.736zm-72.96 242.688c0-82.432-36.864-149.248-36.864-149.248-36.864-27.648-71.936-26.88-71.936-26.88l-3.584 4.096c43.52 13.312 63.744 32.512 63.744 32.512-60.811-33.329-132.244-33.335-191.232-7.424-9.472 4.352-15.104 7.424-15.104 7.424s21.248-20.224 67.328-33.536l-2.56-3.072s-35.072-.768-71.936 26.88c0 0-36.864 66.816-36.864 149.248 0 0 21.504 37.12 78.08 38.912 0 0 9.472-11.52 17.152-21.248-32.512-9.728-44.8-30.208-44.8-30.208 3.766 2.636 9.976 6.053 10.496 6.4 43.21 24.198 104.588 32.126 159.744 8.96 8.96-3.328 18.944-8.192 29.44-15.104 0 0-12.8 20.992-46.336 30.464 7.68 9.728 16.896 20.736 16.896 20.736 56.576-1.792 78.336-38.912 78.336-38.912z" fa-key="3" fill="currentColor"></path>`,
6171
viewBox: '0 0 496 512'
6272
}
63-
}
73+
},
6474
}
6575
})
6676

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`colorModeObserver \`colorModeObserver\` should provide and observe values 1`] = `
4+
Object {
5+
"baseStyle": Object {
6+
"CButton": Object {
7+
"bg": "success",
8+
"borderRadius": "200px",
9+
"shadow": "inner",
10+
},
11+
},
12+
"colors": Object {
13+
"info": "#0000FF",
14+
},
15+
"shadows": Object {
16+
"inner": "inset 0 2px 4px 0 rgba( 0, 0, 0, 0.06)",
17+
"outline": "0 0 0 3px rgba(66, 153, 225, 0.6)",
18+
},
19+
}
20+
`;
Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
import Vue from 'vue'
22

3+
export const defineColorModeObserver = ({ colorMode, theme, icons } = {}) => Vue.observable({
4+
colorMode,
5+
theme,
6+
icons
7+
})
8+
9+
310
/**
411
* This observed store object observed the colorMode and stores it in an
5-
* oberved object that other components can consume.
12+
* observed object that other components can consume.
613
*/
7-
export const colorModeObserver = Vue.observable({
8-
colorMode: undefined,
9-
theme: undefined,
10-
icons: undefined
11-
})
14+
export const colorModeObserver = defineColorModeObserver()
15+
16+
/**
17+
* Utility function that returns a value based on the colorMode
18+
* @param {string | number | Array<string | number>} lightValue Value when colorMode is `light`
19+
* @param {string | number | Array<string | number>} darkValue Value when colorMode is `dark`
20+
* @param {import('Vue').ComponentOptions<Vue, { colorMode: 'light' | 'dark', theme: any, icons: any }>} observer Value when colorMode is `dark`
21+
* @return {string | number | Array<string | number>}
22+
*/
23+
export const mode = (lightValue, darkValue, observer) => {
24+
const { colorMode } = observer || colorModeObserver
25+
return colorMode === 'dark' ? darkValue : lightValue
26+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { mode, defineColorModeObserver } from './color-mode-observer'
2+
3+
4+
describe('colorModeObserver', () => {
5+
it('`colorModeObserver` should provide and observe values', () => {
6+
const baseTheme = {
7+
colors: {
8+
success: '#3ea76a',
9+
warning: '#ffc61a',
10+
error: '#dd3b4b'
11+
},
12+
shadows: {
13+
outline: '0 0 0 3px rgba(66, 153, 225, 0.6)',
14+
inner: 'inset 0 2px 4px 0 rgba( 0, 0, 0, 0.06)'
15+
},
16+
baseStyle: {
17+
CButton: {
18+
bg: 'success',
19+
borderRadius: '200px',
20+
shadow: 'inner'
21+
}
22+
}
23+
}
24+
25+
const observer = defineColorModeObserver()
26+
27+
observer.theme = baseTheme
28+
observer.colorMode = 'light'
29+
expect(observer.theme).toEqual(baseTheme)
30+
expect(observer.colorMode).toEqual('light')
31+
32+
// Mutate `colorMode`
33+
observer.colorMode = 'dark'
34+
const readColorMode = observer.colorMode
35+
expect(readColorMode).toBe('dark')
36+
expect(readColorMode).toEqual(observer.colorMode)
37+
38+
// Mutate `theme`
39+
const newTheme = Object.assign(baseTheme, {
40+
colors: {
41+
info: '#0000FF'
42+
}
43+
})
44+
observer.theme = newTheme
45+
const readTheme = observer.theme
46+
expect(readTheme).toMatchObject(newTheme)
47+
expect(readTheme).toMatchSnapshot()
48+
})
49+
50+
it('`mode` function should return current colorMode value', () => {
51+
const observer = defineColorModeObserver({
52+
colorMode: 'dark'
53+
})
54+
55+
const bg = mode('red', 'blue', observer)
56+
expect(bg).toBe('blue')
57+
observer.colorMode = 'light'
58+
59+
// We invoke as a function since we are in a non-reactive context
60+
const _bg = mode('red', 'blue', observer)
61+
expect(_bg).toBe('red')
62+
})
63+
})

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,28 @@ export const createStyledAttrsMixin = name => ({
6161
/** Split style attributes and native attributes */
6262
splitProps () {
6363
const $attrs = this.$data.attrs$
64-
const styles = Object.assign({}, this.componentStyles || {}, $attrs)
64+
const { styleAttrs, nativeAttrs } = extractChakraAttrs($attrs)
6565

66-
const { styleAttrs, nativeAttrs } = extractChakraAttrs(styles)
6766
return {
6867
styleAttrs,
6968
nativeAttrs
7069
}
7170
},
7271
baseStyle () {
73-
const { nativeAttrs } = this.splitProps
74-
const styleObjectOrFn = __get(this.theme, `baseStyle.${name}`)
75-
return (
76-
runIfFn(styleObjectOrFn, {
72+
const componentBaseStyleObjectOrFunction = __get(this.theme, `baseStyle.${name}`)
73+
return componentBaseStyleObjectOrFunction ? (
74+
runIfFn(componentBaseStyleObjectOrFunction, {
7775
theme: this.theme,
78-
colorMode: this.colorMode,
79-
...nativeAttrs
80-
}) || {}
81-
)
76+
colorMode: this.colorMode
77+
})
78+
) : {}
8279
},
8380
className () {
8481
const { styleAttrs } = this.splitProps
82+
8583
const boxStylesObject = composeSystem(
8684
{
85+
...this.componentStyles || {},
8786
...this.baseStyle,
8887
...styleAttrs
8988
},

0 commit comments

Comments
 (0)