|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2015 - present Instructure, Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +/** @jsx jsx */ |
| 26 | +import { useState } from 'react' |
| 27 | + |
| 28 | +import { expect, mount, stub, within } from '@instructure/ui-test-utils' |
| 29 | +import { jsx, InstUISettingsProvider, WithStyleProps, useStyle } from '../index' |
| 30 | + |
| 31 | +type Props = { |
| 32 | + inverse?: boolean |
| 33 | +} & WithStyleProps<ComponentTheme> |
| 34 | + |
| 35 | +type Theme = { |
| 36 | + key: string |
| 37 | + colors: { |
| 38 | + contrasts: { |
| 39 | + grey1111: string |
| 40 | + green4570: string |
| 41 | + blue4570: string |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +type ComponentTheme = { |
| 47 | + textColor: string |
| 48 | + textColorInverse: string |
| 49 | + backgroundColor: string |
| 50 | +} |
| 51 | + |
| 52 | +describe('useStyle', async () => { |
| 53 | + const grey1111 = 'rgb(0, 128, 0)' |
| 54 | + const green4570 = 'rgb(10, 10, 10)' |
| 55 | + const blue4570 = 'rgb(255, 255, 0)' |
| 56 | + const exampleTheme: Theme = { |
| 57 | + key: 'exampleTheme', |
| 58 | + colors: { |
| 59 | + contrasts: { |
| 60 | + grey1111, |
| 61 | + green4570, |
| 62 | + blue4570 |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + const generateComponentTheme = function (theme: Theme): ComponentTheme { |
| 68 | + const { colors } = theme |
| 69 | + return { |
| 70 | + textColor: colors.contrasts.grey1111, |
| 71 | + textColorInverse: colors.contrasts.green4570, |
| 72 | + backgroundColor: colors.contrasts.blue4570 |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + type StyleParams = { |
| 77 | + inverse: boolean |
| 78 | + clearBackground: boolean |
| 79 | + themeOverride: Props['themeOverride'] |
| 80 | + } |
| 81 | + |
| 82 | + const generateStyle = function ( |
| 83 | + componentTheme: ComponentTheme, |
| 84 | + params: StyleParams |
| 85 | + ) { |
| 86 | + const { inverse, clearBackground } = params |
| 87 | + |
| 88 | + return { |
| 89 | + exampleComponent: { |
| 90 | + label: 'exampleComponent', |
| 91 | + color: componentTheme.textColor, |
| 92 | + background: componentTheme.backgroundColor, |
| 93 | + insetInlineStart: '8px', |
| 94 | + ...(inverse && { color: componentTheme.textColorInverse }), |
| 95 | + ...(clearBackground && { background: 'transparent' }) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + const ThemedComponent = ({ inverse = false, themeOverride }: Props) => { |
| 101 | + const [clearBackground, setClearBackground] = useState(false) |
| 102 | + |
| 103 | + const styles = useStyle({ |
| 104 | + generateStyle, |
| 105 | + generateComponentTheme, |
| 106 | + componentId: 'ThemedComponent', |
| 107 | + params: { inverse, clearBackground, themeOverride } |
| 108 | + }) |
| 109 | + |
| 110 | + const handleClick = () => { |
| 111 | + setClearBackground(true) |
| 112 | + } |
| 113 | + |
| 114 | + return ( |
| 115 | + <div css={styles!.exampleComponent}> |
| 116 | + <p>Hello World</p> |
| 117 | + <button onClick={handleClick}>Button</button> |
| 118 | + </div> |
| 119 | + ) |
| 120 | + } |
| 121 | + |
| 122 | + describe('with theme provided by InstUISettingsProvider', async () => { |
| 123 | + it('should add css class suffixed with label', async () => { |
| 124 | + const subject = await mount( |
| 125 | + <InstUISettingsProvider theme={exampleTheme}> |
| 126 | + <ThemedComponent /> |
| 127 | + </InstUISettingsProvider> |
| 128 | + ) |
| 129 | + const emotionClassRegex = new RegExp(/^css-.+-exampleComponent$/) |
| 130 | + |
| 131 | + expect(subject.getDOMNode().classList[0]).to.match(emotionClassRegex) |
| 132 | + }) |
| 133 | + |
| 134 | + it('should apply correct css props', async () => { |
| 135 | + const subject = await mount( |
| 136 | + <InstUISettingsProvider theme={exampleTheme}> |
| 137 | + <ThemedComponent /> |
| 138 | + </InstUISettingsProvider> |
| 139 | + ) |
| 140 | + const component = subject.getDOMNode() |
| 141 | + const computedStyle = getComputedStyle(component) |
| 142 | + |
| 143 | + expect(computedStyle.color).to.equal('rgb(0, 128, 0)') |
| 144 | + expect(computedStyle.backgroundColor).to.equal('rgb(255, 255, 0)') |
| 145 | + }) |
| 146 | + |
| 147 | + describe('should allow configuration through the themeOverride prop', async () => { |
| 148 | + it('when it is an object', async () => { |
| 149 | + const subject = await mount( |
| 150 | + <InstUISettingsProvider theme={exampleTheme}> |
| 151 | + <ThemedComponent |
| 152 | + themeOverride={{ |
| 153 | + textColor: 'rgb(128, 0, 128)' |
| 154 | + }} |
| 155 | + /> |
| 156 | + </InstUISettingsProvider> |
| 157 | + ) |
| 158 | + const component = subject.getDOMNode() |
| 159 | + const computedStyle = getComputedStyle(component) |
| 160 | + |
| 161 | + expect(computedStyle.color).to.equal('rgb(128, 0, 128)') |
| 162 | + expect(computedStyle.backgroundColor).to.equal('rgb(255, 255, 0)') |
| 163 | + }) |
| 164 | + |
| 165 | + it('when it is a function', async () => { |
| 166 | + const subject = await mount( |
| 167 | + <InstUISettingsProvider theme={exampleTheme}> |
| 168 | + <ThemedComponent |
| 169 | + themeOverride={(componentTheme) => ({ |
| 170 | + textColor: componentTheme.backgroundColor |
| 171 | + })} |
| 172 | + /> |
| 173 | + </InstUISettingsProvider> |
| 174 | + ) |
| 175 | + const component = subject.getDOMNode() |
| 176 | + const computedStyle = getComputedStyle(component) |
| 177 | + |
| 178 | + expect(computedStyle.color).to.equal('rgb(255, 255, 0)') |
| 179 | + expect(computedStyle.backgroundColor).to.equal('rgb(255, 255, 0)') |
| 180 | + }) |
| 181 | + }) |
| 182 | + |
| 183 | + it('should ignore empty themeOverride props', async () => { |
| 184 | + const subject = await mount( |
| 185 | + <InstUISettingsProvider theme={exampleTheme}> |
| 186 | + <ThemedComponent themeOverride={{}} /> |
| 187 | + </InstUISettingsProvider> |
| 188 | + ) |
| 189 | + const component = subject.getDOMNode() |
| 190 | + const computedStyle = getComputedStyle(component) |
| 191 | + |
| 192 | + expect(computedStyle.color).to.equal('rgb(0, 128, 0)') |
| 193 | + expect(computedStyle.backgroundColor).to.equal('rgb(255, 255, 0)') |
| 194 | + }) |
| 195 | + }) |
| 196 | + |
| 197 | + describe('should update css props', async () => { |
| 198 | + it('when props are updated', async () => { |
| 199 | + // `setProps` can be called on the outer component, |
| 200 | + // so have to add the theme ad themeOverride here, and suppress the error |
| 201 | + stub(console, 'warn') // suppress "no theme provided error" |
| 202 | + |
| 203 | + const subject = await mount( |
| 204 | + <ThemedComponent |
| 205 | + inverse={false} |
| 206 | + themeOverride={{ |
| 207 | + textColor: grey1111, |
| 208 | + textColorInverse: blue4570, |
| 209 | + backgroundColor: green4570 |
| 210 | + }} |
| 211 | + /> |
| 212 | + ) |
| 213 | + const component = subject.getDOMNode() |
| 214 | + |
| 215 | + expect(getComputedStyle(component).color).to.equal(grey1111) |
| 216 | + |
| 217 | + await subject.setProps({ inverse: true }) |
| 218 | + |
| 219 | + expect(getComputedStyle(component).color).to.equal(blue4570) |
| 220 | + }) |
| 221 | + |
| 222 | + it('when state is updated', async () => { |
| 223 | + const subject = await mount( |
| 224 | + <InstUISettingsProvider theme={exampleTheme}> |
| 225 | + <ThemedComponent /> |
| 226 | + </InstUISettingsProvider> |
| 227 | + ) |
| 228 | + const main = within(subject.getDOMNode()) |
| 229 | + const clearBackgroundButton = await main.find('button') |
| 230 | + const component = main.getDOMNode() |
| 231 | + |
| 232 | + expect(getComputedStyle(component).backgroundColor).to.equal( |
| 233 | + 'rgb(255, 255, 0)' |
| 234 | + ) |
| 235 | + |
| 236 | + await clearBackgroundButton.click() |
| 237 | + |
| 238 | + expect(getComputedStyle(component).backgroundColor).to.equal( |
| 239 | + 'rgba(0, 0, 0, 0)' |
| 240 | + ) |
| 241 | + }) |
| 242 | + }) |
| 243 | + |
| 244 | + describe('should apply bi-directional polyfill on styles object', async () => { |
| 245 | + it('in default "ltr" mode', async () => { |
| 246 | + const subject = await mount( |
| 247 | + <InstUISettingsProvider theme={exampleTheme}> |
| 248 | + <ThemedComponent /> |
| 249 | + </InstUISettingsProvider> |
| 250 | + ) |
| 251 | + const component = subject.getDOMNode() |
| 252 | + const computedStyle = getComputedStyle(component) |
| 253 | + |
| 254 | + // `inset-inline-start` should be transformed to 'left' in 'ltr' mode |
| 255 | + expect(computedStyle.left).to.equal('8px') |
| 256 | + expect(computedStyle.right).to.equal('auto') |
| 257 | + }) |
| 258 | + |
| 259 | + it('in "rtl" mode', async () => { |
| 260 | + const subject = await mount( |
| 261 | + <InstUISettingsProvider theme={exampleTheme} dir="rtl"> |
| 262 | + <ThemedComponent /> |
| 263 | + </InstUISettingsProvider> |
| 264 | + ) |
| 265 | + const component = subject.getDOMNode().firstElementChild |
| 266 | + const computedStyle = getComputedStyle(component!) |
| 267 | + |
| 268 | + // `inset-inline-start` should be transformed to 'right' in 'rtl' mode |
| 269 | + expect(computedStyle.left).to.equal('auto') |
| 270 | + expect(computedStyle.right).to.equal('8px') |
| 271 | + }) |
| 272 | + }) |
| 273 | +}) |
0 commit comments