Skip to content

Commit a67605f

Browse files
committed
[Layout foundations] Add tests for Box (#7094)
1 parent 5906f65 commit a67605f

File tree

2 files changed

+72
-20
lines changed

2 files changed

+72
-20
lines changed

polaris-react/src/components/Box/Box.tsx

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, {createElement, forwardRef, ReactNode} from 'react';
22
import type {colors, depth, shape, spacing} from '@shopify/polaris-tokens';
33

4-
import {classNames} from '../../utilities/css';
4+
import {classNames, sanitizeCustomProperties} from '../../utilities/css';
55

66
import styles from './Box.scss';
77

@@ -186,64 +186,68 @@ export const Box = forwardRef<HTMLElement, BoxProps>(
186186
} as Spacing;
187187

188188
const style = {
189-
...(background ? {'--pc-box-background': `var(--p-${background})`} : {}),
189+
...(background
190+
? {'--pc-box-background': `var(--p-${background})`}
191+
: undefined),
190192
...(borders.bottom
191193
? {'--pc-box-border-bottom': `var(--p-border-${borders.bottom})`}
192-
: {}),
194+
: undefined),
193195
...(borders.left
194196
? {'--pc-box-border-left': `var(--p-border-${borders.left})`}
195-
: {}),
197+
: undefined),
196198
...(borders.right
197199
? {'--pc-box-border-right': `var(--p-border-${borders.right})`}
198-
: {}),
200+
: undefined),
199201
...(borders.top
200202
? {'--pc-box-border-top': `var(--p-border-${borders.top})`}
201-
: {}),
203+
: undefined),
202204
...(borderRadiuses.bottomLeft
203205
? {
204206
'--pc-box-border-radius-bottom-left': `var(--p-border-radius-${borderRadiuses.bottomLeft})`,
205207
}
206-
: {}),
208+
: undefined),
207209
...(borderRadiuses.bottomRight
208210
? {
209211
'--pc-box-border-radius-bottom-right': `var(--p-border-radius-${borderRadiuses.bottomRight})`,
210212
}
211-
: {}),
213+
: undefined),
212214
...(borderRadiuses.topLeft
213215
? {
214216
'--pc-box-border-radius-top-left': `var(--p-border-radius-${borderRadiuses.topLeft})`,
215217
}
216-
: {}),
218+
: undefined),
217219
...(borderRadiuses.topRight
218220
? {
219221
'--pc-box-border-radius-top-right': `var(--p-border-radius-${borderRadiuses.topRight})`,
220222
}
221-
: {}),
223+
: undefined),
222224
...(margins.bottom
223225
? {'--pc-box-margin-bottom': `var(--p-space-${margins.bottom})`}
224-
: {}),
226+
: undefined),
225227
...(margins.left
226228
? {'--pc-box-margin-left': `var(--p-space-${margins.left})`}
227-
: {}),
229+
: undefined),
228230
...(margins.right
229231
? {'--pc-box-margin-right': `var(--p-space-${margins.right})`}
230-
: {}),
232+
: undefined),
231233
...(margins.top
232234
? {'--pc-box-margin-top': `var(--p-space-${margins.top})`}
233-
: {}),
235+
: undefined),
234236
...(paddings.bottom
235237
? {'--pc-box-padding-bottom': `var(--p-space-${paddings.bottom})`}
236-
: {}),
238+
: undefined),
237239
...(paddings.left
238240
? {'--pc-box-padding-left': `var(--p-space-${paddings.left})`}
239-
: {}),
241+
: undefined),
240242
...(paddings.right
241243
? {'--pc-box-padding-right': `var(--p-space-${paddings.right})`}
242-
: {}),
244+
: undefined),
243245
...(paddings.top
244246
? {'--pc-box-padding-top': `var(--p-space-${paddings.top})`}
245-
: {}),
246-
...(shadow ? {'--pc-box-shadow': `var(--p-shadow-${shadow})`} : {}),
247+
: undefined),
248+
...(shadow
249+
? {'--pc-box-shadow': `var(--p-shadow-${shadow})`}
250+
: undefined),
247251
} as React.CSSProperties;
248252

249253
const className = classNames(styles.Box);
@@ -252,7 +256,7 @@ export const Box = forwardRef<HTMLElement, BoxProps>(
252256
as,
253257
{
254258
className,
255-
style,
259+
style: sanitizeCustomProperties(style),
256260
ref,
257261
},
258262
children,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from 'react';
2+
import {mountWithApp} from 'tests/utilities';
3+
4+
import {Box} from '..';
5+
6+
const text = 'This is a box';
7+
const children = <p>{text}</p>;
8+
9+
describe('Box', () => {
10+
it('renders children', () => {
11+
const box = mountWithApp(<Box>{children}</Box>);
12+
13+
expect(box).toContainReactComponent('p', {children: text});
14+
});
15+
16+
it('does not render custom properties by default', () => {
17+
const box = mountWithApp(<Box>{children}</Box>);
18+
19+
expect(box).toContainReactComponent('div', {style: undefined});
20+
});
21+
22+
it('only renders the custom property that matches the property passed in', () => {
23+
const box = mountWithApp(<Box marginLeft="2">{children}</Box>);
24+
25+
expect(box).toContainReactComponent('div', {
26+
style: {
27+
'--pc-box-margin-left': 'var(--p-space-2)',
28+
} as React.CSSProperties,
29+
});
30+
});
31+
32+
it('renders custom properties combined with any overrides if they are passed in', () => {
33+
const box = mountWithApp(
34+
<Box margin="1" marginLeft="2">
35+
{children}
36+
</Box>,
37+
);
38+
39+
expect(box).toContainReactComponent('div', {
40+
style: {
41+
'--pc-box-margin-bottom': 'var(--p-space-1)',
42+
'--pc-box-margin-left': 'var(--p-space-2)',
43+
'--pc-box-margin-right': 'var(--p-space-1)',
44+
'--pc-box-margin-top': 'var(--p-space-1)',
45+
} as React.CSSProperties,
46+
});
47+
});
48+
});

0 commit comments

Comments
 (0)