Skip to content

Commit b907421

Browse files
committed
Adjust tests
1 parent b377c79 commit b907421

File tree

3 files changed

+78
-55
lines changed

3 files changed

+78
-55
lines changed

packages/color-modes/src/index.js

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
import React from 'react'
2-
import {
3-
jsx,
4-
useThemeUI,
5-
merge,
6-
Context,
7-
} from '@theme-ui/core'
2+
import { jsx, useThemeUI, merge, Context } from '@theme-ui/core'
83
import { get } from '@theme-ui/css'
9-
import {
10-
Global,
11-
ThemeContext as EmotionContext
12-
} from '@emotion/core'
13-
import {
14-
toCustomProperties,
15-
createColorStyles,
16-
} from './custom-properties'
4+
import { Global, ThemeContext as EmotionContext } from '@emotion/core'
5+
import { toCustomProperties, createColorStyles } from './custom-properties'
176

187
const STORAGE_KEY = 'theme-ui-color-mode'
19-
const HAS_STORAGE = typeof Storage !== 'undefined'
208

219
const storage = {
22-
get: init => HAS_STORAGE && window.localStorage.getItem(STORAGE_KEY) || init,
23-
set: value => HAS_STORAGE && window.localStorage.setItem(STORAGE_KEY, value),
10+
get: init => {
11+
try {
12+
return window.localStorage.getItem(STORAGE_KEY) || init
13+
} catch (e) {
14+
console.warn(
15+
'localStorage is disabled and color mode might not work as expected.',
16+
'Please check your Site Settings.',
17+
e
18+
)
19+
}
20+
},
21+
set: value => {
22+
try {
23+
window.localStorage.setItem(STORAGE_KEY, value)
24+
} catch (e) {
25+
console.warn(
26+
'localStorage is disabled and color mode might not work as expected.',
27+
'Please check your Site Settings.',
28+
e
29+
)
30+
}
31+
},
2432
}
2533

2634
const getMediaQuery = () => {
@@ -36,7 +44,9 @@ const getMediaQuery = () => {
3644
}
3745

3846
const useColorModeState = (theme = {}) => {
39-
const [mode, setMode] = React.useState(theme.initialColorModeName || 'default')
47+
const [mode, setMode] = React.useState(
48+
theme.initialColorModeName || 'default'
49+
)
4050

4151
// initialize state
4252
React.useEffect(() => {
@@ -92,16 +102,14 @@ const applyColorMode = (theme, mode) => {
92102

93103
const BodyStyles = () =>
94104
jsx(Global, {
95-
styles: theme => createColorStyles(theme)
105+
styles: theme => createColorStyles(theme),
96106
})
97107

98-
export const ColorModeProvider = ({
99-
children,
100-
}) => {
108+
export const ColorModeProvider = ({ children }) => {
101109
const outer = useThemeUI()
102110
const [colorMode, setColorMode] = useColorModeState(outer.theme)
103111
const theme = applyColorMode(outer.theme || {}, colorMode)
104-
const emotionTheme = {...theme}
112+
const emotionTheme = { ...theme }
105113

106114
if (theme.useCustomProperties !== false) {
107115
emotionTheme.colors = toCustomProperties(emotionTheme.colors, 'colors')
@@ -114,8 +122,12 @@ export const ColorModeProvider = ({
114122
setColorMode,
115123
}
116124

117-
return jsx(EmotionContext.Provider, { value: emotionTheme },
118-
jsx(Context.Provider, { value: context },
125+
return jsx(
126+
EmotionContext.Provider,
127+
{ value: emotionTheme },
128+
jsx(
129+
Context.Provider,
130+
{ value: context },
119131
jsx(BodyStyles, { key: 'color-mode' }),
120132
children
121133
)
@@ -128,11 +140,10 @@ const noflash = `(function() { try {
128140
document.body.classList.add('theme-ui-' + mode);
129141
} catch (e) {} })();`
130142

131-
export const InitializeColorMode = () => (
143+
export const InitializeColorMode = () =>
132144
jsx('script', {
133145
key: 'theme-ui-no-flash',
134146
dangerouslySetInnerHTML: {
135147
__html: noflash,
136-
}
148+
},
137149
})
138-
)

packages/color-modes/test/index.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import { render, fireEvent, cleanup, act } from '@testing-library/react'
55
import { matchers } from 'jest-emotion'
66
import mockConsole from 'jest-mock-console'
77
import { jsx, ThemeProvider, useThemeUI } from '@theme-ui/core'
8-
import {
9-
ColorModeProvider,
10-
useColorMode,
11-
InitializeColorMode,
12-
} from '../src'
8+
import { ColorModeProvider, useColorMode, InitializeColorMode } from '../src'
139

1410
const STORAGE_KEY = 'theme-ui-color-mode'
1511

@@ -38,9 +34,9 @@ test('renders with color modes', () => {
3834
modes: {
3935
dark: {
4036
text: 'white',
41-
}
42-
}
43-
}
37+
},
38+
},
39+
},
4440
}}>
4541
<ColorModeProvider>
4642
<Mode />
@@ -67,8 +63,8 @@ test('renders with initial color mode name', () => {
6763
colors: {
6864
modes: {
6965
dark: {},
70-
}
71-
}
66+
},
67+
},
7268
}}>
7369
<ColorModeProvider>
7470
<Mode />
@@ -570,8 +566,30 @@ test('raw color values are passed to theme-ui context when custom properties are
570566
})
571567

572568
test('InitializeColorMode renders', () => {
573-
const json = renderJSON(
574-
<InitializeColorMode />
575-
)
569+
const json = renderJSON(<InitializeColorMode />)
576570
expect(json).toMatchSnapshot()
577571
})
572+
573+
test('warns when localStorage is disabled', () => {
574+
Object.defineProperty(window, 'localStorage', {
575+
get: jest.fn(() => {
576+
throw 'SecurityError: The operation is insecure.'
577+
}),
578+
})
579+
580+
let mode
581+
const Consumer = props => {
582+
const [colorMode] = useColorMode()
583+
mode = colorMode
584+
return false
585+
}
586+
587+
render(
588+
<ThemeProvider>
589+
<ColorModeProvider>
590+
<Consumer />
591+
</ColorModeProvider>
592+
</ThemeProvider>
593+
)
594+
expect(mode).toBe('default')
595+
})

packages/theme-ui/test/color-modes.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@ import { render, fireEvent, cleanup, act } from '@testing-library/react'
55
import { matchers } from 'jest-emotion'
66
import mockConsole from 'jest-mock-console'
77
import { version as emotionVersion } from '@emotion/core/package.json'
8-
import {
9-
jsx,
10-
ThemeProvider,
11-
useColorMode,
12-
useThemeUI,
13-
ColorMode,
14-
} from '../src/index'
8+
import { jsx, ThemeProvider, useColorMode, useThemeUI } from '../src/index'
159

1610
const STORAGE_KEY = 'theme-ui-color-mode'
1711

@@ -38,9 +32,9 @@ test('renders with color modes', () => {
3832
modes: {
3933
dark: {
4034
text: 'white',
41-
}
42-
}
43-
}
35+
},
36+
},
37+
},
4438
}}>
4539
<Mode />
4640
</ThemeProvider>
@@ -65,8 +59,8 @@ test('renders with initial color mode name', () => {
6559
colors: {
6660
modes: {
6761
dark: {},
68-
}
69-
}
62+
},
63+
},
7064
}}>
7165
<Mode />
7266
</ThemeProvider>
@@ -213,8 +207,8 @@ test('inherits color mode state from parent context', () => {
213207
colors: {
214208
modes: {
215209
dark: {},
216-
}
217-
}
210+
},
211+
},
218212
}}>
219213
<ThemeProvider
220214
theme={{

0 commit comments

Comments
 (0)