@lumx/react 3.1.5 → 3.2.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. package/_internal/types.d.ts +16 -5
  2. package/index.d.ts +45 -4
  3. package/index.js +632 -423
  4. package/index.js.map +1 -1
  5. package/package.json +3 -3
  6. package/src/components/alert-dialog/AlertDialog.stories.tsx +96 -165
  7. package/src/components/alert-dialog/AlertDialog.test.tsx +15 -23
  8. package/src/components/avatar/Avatar.stories.tsx +91 -62
  9. package/src/components/avatar/Avatar.test.tsx +9 -24
  10. package/src/components/badge/Badge.stories.tsx +63 -38
  11. package/src/components/button/Button.stories.tsx +147 -139
  12. package/src/components/button/IconButton.stories.tsx +45 -0
  13. package/src/components/checkbox/Checkbox.stories.tsx +37 -30
  14. package/src/components/chip/Chip.stories.tsx +77 -15
  15. package/src/components/comment-block/CommentBlock.stories.tsx +90 -25
  16. package/src/components/comment-block/CommentBlock.test.tsx +12 -17
  17. package/src/components/date-picker/DatePickerField.stories.tsx +52 -83
  18. package/src/components/dialog/Dialog.stories.tsx +131 -293
  19. package/src/components/dialog/Dialog.test.tsx +0 -32
  20. package/src/components/dropdown/Dropdown.stories.tsx +1 -186
  21. package/src/components/flag/Flag.stories.tsx +33 -18
  22. package/src/components/flag/Flag.test.tsx +1 -8
  23. package/src/components/flex-box/FlexBox.stories.tsx +151 -238
  24. package/src/components/flex-box/FlexBox.test.tsx +9 -49
  25. package/src/components/generic-block/GenericBlock.stories.jsx +1 -1
  26. package/src/components/grid-column/GridColumn.stories.tsx +46 -0
  27. package/src/components/heading/Heading.stories.tsx +57 -95
  28. package/src/components/icon/Icon.stories.tsx +67 -70
  29. package/src/components/image-block/ImageBlock.stories.tsx +103 -47
  30. package/src/components/image-block/ImageBlock.test.tsx +12 -17
  31. package/src/components/inline-list/InlineList.stories.tsx +45 -29
  32. package/src/components/input-helper/InputHelper.stories.tsx +31 -25
  33. package/src/components/input-label/InputLabel.stories.tsx +33 -10
  34. package/src/components/lightbox/Lightbox.stories.tsx +39 -77
  35. package/src/components/lightbox/Lightbox.test.tsx +12 -17
  36. package/src/components/link/Link.stories.tsx +98 -128
  37. package/src/components/link-preview/LinkPreview.stories.tsx +48 -75
  38. package/src/components/list/List.stories.tsx +59 -84
  39. package/src/components/list/List.test.tsx +8 -17
  40. package/src/components/list/ListDivider.stories.tsx +9 -4
  41. package/src/components/list/ListDivider.test.tsx +12 -17
  42. package/src/components/list/ListItem.stories.tsx +97 -59
  43. package/src/components/list/ListItem.test.tsx +12 -17
  44. package/src/components/list/ListSubheader.stories.tsx +8 -5
  45. package/src/components/list/ListSubheader.test.tsx +12 -18
  46. package/src/components/message/Message.stories.tsx +51 -22
  47. package/src/components/mosaic/Mosaic.stories.tsx +78 -74
  48. package/src/components/mosaic/Mosaic.test.tsx +0 -31
  49. package/src/components/navigation/Navigation.stories.tsx +67 -0
  50. package/src/components/navigation/Navigation.test.tsx +58 -0
  51. package/src/components/navigation/Navigation.tsx +62 -0
  52. package/src/components/navigation/NavigationItem.test.tsx +37 -0
  53. package/src/components/navigation/NavigationItem.tsx +89 -0
  54. package/src/components/navigation/NavigationSection.test.tsx +126 -0
  55. package/src/components/navigation/NavigationSection.tsx +109 -0
  56. package/src/components/navigation/context.tsx +6 -0
  57. package/src/components/navigation/index.ts +1 -0
  58. package/src/components/notification/Notifications.stories.tsx +52 -47
  59. package/src/components/popover/Popover.stories.tsx +68 -201
  60. package/src/components/popover/Popover.tsx +7 -9
  61. package/src/components/popover-dialog/PopoverDialog.stories.tsx +26 -65
  62. package/src/components/post-block/PostBlock.test.tsx +12 -17
  63. package/src/components/progress/ProgressCircular.stories.tsx +24 -12
  64. package/src/components/progress/ProgressLinear.stories.tsx +6 -2
  65. package/src/components/radio-button/RadioButton.stories.tsx +35 -24
  66. package/src/components/select/Select.stories.tsx +19 -23
  67. package/src/components/select/SelectMultiple.stories.tsx +105 -2
  68. package/src/components/select/WithSelectContext.tsx +10 -4
  69. package/src/components/skeleton/SkeletonCircle.stories.tsx +37 -21
  70. package/src/components/skeleton/SkeletonCircle.test.tsx +12 -17
  71. package/src/components/skeleton/SkeletonRectangle.stories.tsx +74 -99
  72. package/src/components/skeleton/SkeletonRectangle.test.tsx +12 -17
  73. package/src/components/skeleton/SkeletonTypography.test.tsx +12 -17
  74. package/src/components/slider/Slider.stories.tsx +41 -25
  75. package/src/components/slider/Slider.test.tsx +12 -18
  76. package/src/components/slideshow/Slideshow.stories.tsx +31 -61
  77. package/src/components/slideshow/Slideshow.test.tsx +15 -23
  78. package/src/components/slideshow/SlideshowControls.stories.tsx +4 -6
  79. package/src/components/switch/Switch.stories.tsx +35 -32
  80. package/src/components/table/Table.test.tsx +12 -17
  81. package/src/components/tabs/Tabs.stories.tsx +4 -3
  82. package/src/components/text/Text.stories.tsx +130 -0
  83. package/src/components/text-field/TextField.stories.tsx +114 -148
  84. package/src/components/thumbnail/Thumbnail.stories.tsx +106 -255
  85. package/src/components/thumbnail/Thumbnail.test.tsx +12 -35
  86. package/src/components/tooltip/Tooltip.stories.tsx +51 -136
  87. package/src/components/user-block/UserBlock.stories.tsx +67 -56
  88. package/src/components/user-block/UserBlock.test.tsx +1 -5
  89. package/src/hooks/useFocusTrap.ts +2 -2
  90. package/src/index.ts +1 -0
  91. package/src/stories/controls/color.ts +6 -0
  92. package/src/stories/controls/element.ts +6 -0
  93. package/src/stories/controls/focusPoint.ts +1 -0
  94. package/src/stories/controls/icons.ts +6 -0
  95. package/src/stories/{knobs → controls}/image.ts +6 -16
  96. package/src/stories/controls/selectArgType.ts +4 -0
  97. package/src/stories/controls/theme.ts +3 -0
  98. package/src/stories/controls/typography.ts +5 -0
  99. package/src/stories/controls/withUndefined.ts +1 -0
  100. package/src/stories/decorators/withChromaticForceScreenSize.tsx +8 -0
  101. package/src/stories/decorators/withCombinations.tsx +99 -0
  102. package/src/stories/decorators/withNestedProps.tsx +23 -0
  103. package/src/stories/{withResizableBox.tsx → decorators/withResizableBox.tsx} +6 -10
  104. package/src/stories/decorators/withValueOnChange.tsx +18 -0
  105. package/src/stories/decorators/withWrapper.tsx +19 -0
  106. package/src/stories/utils/CustomLink.tsx +8 -2
  107. package/src/stories/{knobs → utils}/lorem.ts +9 -9
  108. package/src/testing/utils/commonTestsSuiteRTL.ts +2 -3
  109. package/src/testing/utils/index.ts +0 -2
  110. package/src/untypped-modules.d.ts +0 -2
  111. package/src/utils/MaterialThemeSwitcher/MaterialThemeSwitcher.tsx +1 -1
  112. package/src/utils/ThemeContext.ts +4 -0
  113. package/src/utils/forwardRefPolymorphic.ts +9 -0
  114. package/src/utils/type.ts +28 -4
  115. package/src/components/alert-dialog/__snapshots__/AlertDialog.test.tsx.snap +0 -558
  116. package/src/components/avatar/__snapshots__/Avatar.test.tsx.snap +0 -681
  117. package/src/components/comment-block/__snapshots__/CommentBlock.test.tsx.snap +0 -92
  118. package/src/components/dialog/__snapshots__/Dialog.test.tsx.snap +0 -1133
  119. package/src/components/expansion-panel/ExpansionPanel.stories.tsx +0 -65
  120. package/src/components/flag/__snapshots__/Flag.test.tsx.snap +0 -133
  121. package/src/components/flex-box/__snapshots__/FlexBox.test.tsx.snap +0 -492
  122. package/src/components/grid-column/GridColumn.stories.jsx +0 -56
  123. package/src/components/image-block/__snapshots__/ImageBlock.test.tsx.snap +0 -64
  124. package/src/components/lightbox/__snapshots__/Lightbox.test.tsx.snap +0 -194
  125. package/src/components/list/__snapshots__/List.test.tsx.snap +0 -360
  126. package/src/components/list/__snapshots__/ListDivider.test.tsx.snap +0 -7
  127. package/src/components/list/__snapshots__/ListItem.test.tsx.snap +0 -160
  128. package/src/components/list/__snapshots__/ListSubheader.test.tsx.snap +0 -9
  129. package/src/components/mosaic/__snapshots__/Mosaic.test.tsx.snap +0 -357
  130. package/src/components/post-block/__snapshots__/PostBlock.test.tsx.snap +0 -139
  131. package/src/components/skeleton/__snapshots__/SkeletonCircle.test.tsx.snap +0 -54
  132. package/src/components/skeleton/__snapshots__/SkeletonRectangle.test.tsx.snap +0 -177
  133. package/src/components/skeleton/__snapshots__/SkeletonTypography.test.tsx.snap +0 -174
  134. package/src/components/slider/__snapshots__/Slider.test.tsx.snap +0 -122
  135. package/src/components/slideshow/__snapshots__/Slideshow.test.tsx.snap +0 -157
  136. package/src/components/table/__snapshots__/Table.test.tsx.snap +0 -263
  137. package/src/components/text/Text.stories.jsx +0 -75
  138. package/src/components/thumbnail/__snapshots__/Thumbnail.test.tsx.snap +0 -130
  139. package/src/components/user-block/__snapshots__/UserBlock.test.tsx.snap +0 -362
  140. package/src/stories/chromaticForceScreenSize.tsx +0 -7
  141. package/src/stories/knobs/buttonKnob.ts +0 -9
  142. package/src/stories/knobs/emphasisKnob.ts +0 -8
  143. package/src/stories/knobs/enumKnob.ts +0 -14
  144. package/src/stories/knobs/focusKnob.ts +0 -3
  145. package/src/stories/knobs/sizeKnob.ts +0 -5
  146. package/src/stories/knobs/thumbnailsKnob.ts +0 -9
  147. package/src/testing/utils/itShouldRenderStories.tsx +0 -103
@@ -1,362 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`<UserBlock> Snapshots and structure should render story 'Clickable' 1`] = `
4
- [
5
- <div
6
- className="lumx-user-block lumx-user-block--orientation-horizontal lumx-user-block--size-m lumx-user-block--theme-light lumx-user-block--is-clickable"
7
- >
8
- <Avatar
9
- alt=""
10
- className="lumx-user-block__avatar"
11
- image="/demo-assets/avatar1.jpg"
12
- onClick={[Function]}
13
- size="m"
14
- theme="light"
15
- thumbnailProps={
16
- {
17
- "tabIndex": -1,
18
- }
19
- }
20
- />
21
- <div
22
- className="lumx-user-block__wrapper"
23
- >
24
- <Link
25
- aria-label="Emmitt O. Lum - open user profile"
26
- className="lumx-user-block__name"
27
- color="dark"
28
- onClick={[Function]}
29
- >
30
- Emmitt O. Lum
31
- </Link>
32
- <div
33
- className="lumx-user-block__fields"
34
- >
35
- <span
36
- className="lumx-user-block__field"
37
- key="0"
38
- >
39
- Creative developer
40
- </span>
41
- <span
42
- className="lumx-user-block__field"
43
- key="1"
44
- >
45
- Denpasar
46
- </span>
47
- </div>
48
- </div>
49
- </div>,
50
- <div
51
- className="lumx-user-block lumx-user-block--orientation-horizontal lumx-user-block--size-m lumx-user-block--theme-light lumx-user-block--is-clickable"
52
- >
53
- <Avatar
54
- alt=""
55
- className="lumx-user-block__avatar"
56
- image="/demo-assets/avatar1.jpg"
57
- linkProps={
58
- {
59
- "href": "https://example.com",
60
- }
61
- }
62
- size="m"
63
- theme="light"
64
- thumbnailProps={
65
- {
66
- "tabIndex": -1,
67
- }
68
- }
69
- />
70
- <div
71
- className="lumx-user-block__wrapper"
72
- >
73
- <Link
74
- aria-label="Emmitt O. Lum - open user profile"
75
- className="lumx-user-block__name"
76
- color="dark"
77
- href="https://example.com"
78
- >
79
- Emmitt O. Lum
80
- </Link>
81
- <div
82
- className="lumx-user-block__fields"
83
- >
84
- <span
85
- className="lumx-user-block__field"
86
- key="0"
87
- >
88
- Creative developer
89
- </span>
90
- <span
91
- className="lumx-user-block__field"
92
- key="1"
93
- >
94
- Denpasar
95
- </span>
96
- </div>
97
- </div>
98
- </div>,
99
- <div
100
- className="lumx-user-block lumx-user-block--orientation-horizontal lumx-user-block--size-m lumx-user-block--theme-light lumx-user-block--is-clickable"
101
- >
102
- <Avatar
103
- alt=""
104
- className="lumx-user-block__avatar"
105
- image="/demo-assets/avatar1.jpg"
106
- linkAs={[Function]}
107
- size="m"
108
- theme="light"
109
- thumbnailProps={
110
- {
111
- "tabIndex": -1,
112
- }
113
- }
114
- />
115
- <div
116
- className="lumx-user-block__wrapper"
117
- >
118
- <Link
119
- aria-label="Emmitt O. Lum - open user profile"
120
- className="lumx-user-block__name"
121
- color="dark"
122
- linkAs={[Function]}
123
- >
124
- Emmitt O. Lum
125
- </Link>
126
- <div
127
- className="lumx-user-block__fields"
128
- >
129
- <span
130
- className="lumx-user-block__field"
131
- key="0"
132
- >
133
- Creative developer
134
- </span>
135
- <span
136
- className="lumx-user-block__field"
137
- key="1"
138
- >
139
- Denpasar
140
- </span>
141
- </div>
142
- </div>
143
- </div>,
144
- ]
145
- `;
146
-
147
- exports[`<UserBlock> Snapshots and structure should render story 'Default' 1`] = `
148
- <div
149
- className="lumx-user-block lumx-user-block--orientation-horizontal lumx-user-block--size-m lumx-user-block--theme-light"
150
- onMouseEnter={[Function]}
151
- onMouseLeave={[Function]}
152
- >
153
- <Avatar
154
- alt=""
155
- className="lumx-user-block__avatar"
156
- image="/demo-assets/avatar1.jpg"
157
- size="m"
158
- theme="light"
159
- thumbnailProps={
160
- {
161
- "tabIndex": -1,
162
- }
163
- }
164
- />
165
- <div
166
- className="lumx-user-block__wrapper"
167
- >
168
- <span
169
- className="lumx-user-block__name"
170
- >
171
- Emmitt O. Lum
172
- </span>
173
- <div
174
- className="lumx-user-block__fields"
175
- >
176
- <span
177
- className="lumx-user-block__field"
178
- key="0"
179
- >
180
- Creative developer
181
- </span>
182
- <span
183
- className="lumx-user-block__field"
184
- key="1"
185
- >
186
- Denpasar
187
- </span>
188
- </div>
189
- </div>
190
- </div>
191
- `;
192
-
193
- exports[`<UserBlock> Snapshots and structure should render story 'Sizes' 1`] = `
194
- [
195
- <div
196
- className="lumx-user-block lumx-user-block--orientation-horizontal lumx-user-block--size-s lumx-user-block--theme-light"
197
- onMouseEnter={[Function]}
198
- onMouseLeave={[Function]}
199
- >
200
- <Avatar
201
- alt=""
202
- className="lumx-user-block__avatar"
203
- image="/demo-assets/avatar1.jpg"
204
- size="s"
205
- theme="light"
206
- thumbnailProps={
207
- {
208
- "tabIndex": -1,
209
- }
210
- }
211
- />
212
- <div
213
- className="lumx-user-block__wrapper"
214
- >
215
- <span
216
- className="lumx-user-block__name"
217
- >
218
- Emmitt O. Lum
219
- </span>
220
- </div>
221
- </div>,
222
- <div
223
- className="lumx-user-block lumx-user-block--orientation-horizontal lumx-user-block--size-m lumx-user-block--theme-light"
224
- onMouseEnter={[Function]}
225
- onMouseLeave={[Function]}
226
- >
227
- <Avatar
228
- alt=""
229
- className="lumx-user-block__avatar"
230
- image="/demo-assets/avatar1.jpg"
231
- size="m"
232
- theme="light"
233
- thumbnailProps={
234
- {
235
- "tabIndex": -1,
236
- }
237
- }
238
- />
239
- <div
240
- className="lumx-user-block__wrapper"
241
- >
242
- <span
243
- className="lumx-user-block__name"
244
- >
245
- Emmitt O. Lum
246
- </span>
247
- <div
248
- className="lumx-user-block__fields"
249
- >
250
- <span
251
- className="lumx-user-block__field"
252
- key="0"
253
- >
254
- Creative developer
255
- </span>
256
- <span
257
- className="lumx-user-block__field"
258
- key="1"
259
- >
260
- Denpasar
261
- </span>
262
- </div>
263
- </div>
264
- </div>,
265
- <div
266
- className="lumx-user-block lumx-user-block--orientation-horizontal lumx-user-block--size-l lumx-user-block--theme-light"
267
- onMouseEnter={[Function]}
268
- onMouseLeave={[Function]}
269
- >
270
- <Avatar
271
- alt=""
272
- className="lumx-user-block__avatar"
273
- image="/demo-assets/avatar1.jpg"
274
- size="l"
275
- theme="light"
276
- thumbnailProps={
277
- {
278
- "tabIndex": -1,
279
- }
280
- }
281
- />
282
- <div
283
- className="lumx-user-block__wrapper"
284
- >
285
- <span
286
- className="lumx-user-block__name"
287
- >
288
- Emmitt O. Lum
289
- </span>
290
- <div
291
- className="lumx-user-block__fields"
292
- >
293
- <span
294
- className="lumx-user-block__field"
295
- key="0"
296
- >
297
- Creative developer
298
- </span>
299
- <span
300
- className="lumx-user-block__field"
301
- key="1"
302
- >
303
- Denpasar
304
- </span>
305
- </div>
306
- </div>
307
- </div>,
308
- ]
309
- `;
310
-
311
- exports[`<UserBlock> Snapshots and structure should render story 'WithBadge' 1`] = `
312
- <div
313
- className="lumx-user-block lumx-user-block--orientation-horizontal lumx-user-block--size-m lumx-user-block--theme-light"
314
- >
315
- <Avatar
316
- alt=""
317
- badge={
318
- <Badge
319
- color="blue"
320
- >
321
- <Icon
322
- icon="M12,17.27L18.18,21L16.54,13.97L22,9.24L14.81,8.62L12,2L9.19,8.62L2,9.24L7.45,13.97L5.82,21L12,17.27Z"
323
- />
324
- </Badge>
325
- }
326
- className="lumx-user-block__avatar"
327
- image="/demo-assets/avatar1.jpg"
328
- size="m"
329
- theme="light"
330
- thumbnailProps={
331
- {
332
- "tabIndex": -1,
333
- }
334
- }
335
- />
336
- <div
337
- className="lumx-user-block__wrapper"
338
- >
339
- <span
340
- className="lumx-user-block__name"
341
- >
342
- Emmitt O. Lum
343
- </span>
344
- <div
345
- className="lumx-user-block__fields"
346
- >
347
- <span
348
- className="lumx-user-block__field"
349
- key="0"
350
- >
351
- Creative developer
352
- </span>
353
- <span
354
- className="lumx-user-block__field"
355
- key="1"
356
- >
357
- Denpasar
358
- </span>
359
- </div>
360
- </div>
361
- </div>
362
- `;
@@ -1,7 +0,0 @@
1
- /* eslint-disable */
2
- import React from 'react';
3
- import isChromatic from 'chromatic/isChromatic';
4
-
5
- /** Story decorator used to force a minimum screen size when running in chromatic */
6
- export const chromaticForceScreenSize = (story: any) =>
7
- isChromatic() ? <div style={{ minWidth: 1200, minHeight: 800 }}>{story()}</div> : story();
@@ -1,9 +0,0 @@
1
- import { Button, ButtonProps, Size } from '@lumx/react';
2
- import { select } from '@storybook/addon-knobs';
3
- import { ButtonSize } from '@lumx/react/components/button/ButtonRoot';
4
-
5
- export const buttonSize = (
6
- name = 'Size',
7
- value: ButtonSize = Button.defaultProps?.size as any,
8
- groupId: string | undefined = undefined,
9
- ): ButtonProps['size'] => select(name, [Size.m, Size.s], value, groupId);
@@ -1,8 +0,0 @@
1
- import { Emphasis } from '@lumx/react';
2
- import { select } from '@storybook/addon-knobs';
3
-
4
- export const emphasis = (
5
- name = 'Emphasis',
6
- value: Emphasis = Emphasis.high,
7
- groupId: string | undefined = undefined,
8
- ): Emphasis => select(name, Emphasis, value, groupId);
@@ -1,14 +0,0 @@
1
- import isArray from 'lodash/isArray';
2
- import { select } from '@storybook/addon-knobs';
3
-
4
- export function enumKnob<T>(
5
- name: string,
6
- enumObj: Readonly<Record<string, T> | Array<T> | [...T[]]>,
7
- defaultValue?: T,
8
- groupId?: string,
9
- ): T {
10
- const values: Record<string, T> = !isArray(enumObj)
11
- ? enumObj
12
- : Object.fromEntries(Object.values(enumObj).map((value) => [value, value]));
13
- return select(name, values, defaultValue, groupId);
14
- }
@@ -1,3 +0,0 @@
1
- import { number } from '@storybook/addon-knobs';
2
-
3
- export const focusKnob = (name: string, value = 0) => number(name, value, { max: 1, min: -1, range: true, step: 0.01 });
@@ -1,5 +0,0 @@
1
- import { Size } from '@lumx/react';
2
- import { enumKnob } from '@lumx/react/stories/knobs/enumKnob';
3
-
4
- export const sizeKnob = (name = 'Size', size?: Size) =>
5
- enumKnob(name, [undefined, Size.xxs, Size.xs, Size.s, Size.m, Size.l, Size.xl, Size.xxl] as const, size as any);
@@ -1,9 +0,0 @@
1
- import { ThumbnailProps } from '@lumx/react';
2
- import take from 'lodash/take';
3
- import { imageKnob, IMAGES } from '@lumx/react/stories/knobs/image';
4
-
5
- export const thumbnailsKnob = (num: number): ThumbnailProps[] =>
6
- take(Object.values(IMAGES), num).map((image, index) => ({
7
- alt: `Image ${index + 1}`,
8
- image: imageKnob(`Image ${index + 1}`, image),
9
- }));
@@ -1,103 +0,0 @@
1
- /* eslint-disable no-console */
2
- import isFunction from 'lodash/isFunction';
3
- import { shallow, ShallowWrapper } from 'enzyme';
4
- import React from 'react';
5
- import isArray from 'lodash/isArray';
6
- import isPlainObject from 'lodash/isPlainObject';
7
-
8
- /** Equivalent to CSS `selector1 > selector2 > selector3` selector */
9
- interface PathSelector {
10
- /** Path to traverse (with find and dive) to find the component to snapshot. */
11
- path: Selector[];
12
- }
13
- const isPathSelector = (s: any): s is PathSelector => isPlainObject(s) && !!s.path;
14
-
15
- /** Equivalent to CSS `selector1, selector2, selector3` selector */
16
- interface OrSelector {
17
- or: Selector[];
18
- }
19
- const isOrSelector = (s: any): s is OrSelector => isPlainObject(s) && !!s.or;
20
-
21
- type Selector = string | React.FC<any> | PathSelector | OrSelector;
22
- type Wrappers = ShallowWrapper | ShallowWrapper[];
23
-
24
- /** Use selector to find and dive into the given ShallowWrapper */
25
- function findAndDive(wrappers: Wrappers, selector: Selector): Wrappers {
26
- if (isOrSelector(selector)) {
27
- return selector.or.flatMap((s) => findAndDive(wrappers, s)).filter(Boolean);
28
- }
29
-
30
- if (isPathSelector(selector)) {
31
- return selector.path.reduce(findAndDive, wrappers);
32
- }
33
-
34
- if (isArray(wrappers)) {
35
- return wrappers.flatMap((w) => findAndDive(w, selector)).filter(Boolean);
36
- }
37
-
38
- // Find.
39
- const foundWrapper = wrappers.find(selector as any);
40
- if (foundWrapper.length >= 1) {
41
- // Dive.
42
- return foundWrapper.map((w) => {
43
- if (typeof w.getElement().type === 'string') {
44
- // Don't dive into host element (ex: div, span, etc.)
45
- return w as any;
46
- }
47
- return w.dive();
48
- });
49
- }
50
- return foundWrapper as any;
51
- }
52
-
53
- interface Options {
54
- /** Inject props in stories. */
55
- props?: any;
56
- }
57
-
58
- /** Render stories and find wrapper to snapshot using selector. */
59
- export function generateRenderedStories(
60
- stories: Record<string, any>,
61
- selector?: Selector,
62
- options?: Options,
63
- ): Record<string, Wrappers> {
64
- const storyProps: any = {};
65
- if (options?.props) {
66
- Object.assign(storyProps, options.props);
67
- }
68
- const rendered: Record<string, Wrappers> = {};
69
-
70
- for (const [name, Story] of Object.entries(stories)) {
71
- // eslint-disable-next-line no-continue
72
- if (!isFunction(Story)) continue;
73
-
74
- const wrapper = shallow(<Story {...storyProps} />);
75
- let toSnapshot: Wrappers = wrapper;
76
- if (selector) {
77
- toSnapshot = findAndDive(wrapper, selector);
78
- if (isArray(toSnapshot) && toSnapshot.length === 1) {
79
- // eslint-disable-next-line prefer-destructuring
80
- toSnapshot = toSnapshot[0];
81
- }
82
- }
83
- rendered[name] = toSnapshot;
84
- }
85
- return rendered;
86
- }
87
-
88
- /**
89
- * Generate jest snapshot testing from stories.
90
- *
91
- * **Warning: Always read and validate your snapshots before committing!**
92
- *
93
- * @param stories Object of stories indexed by name.
94
- * @param selector Selector(s) used to find the correct element to snapshot.
95
- * @param options Other options (see {@link Options})
96
- */
97
- export function itShouldRenderStories(stories: Record<string, any>, selector?: Selector, options?: Options): void {
98
- for (const [story, toSnapshot] of Object.entries(generateRenderedStories(stories, selector, options))) {
99
- it(`should render story '${story}'`, () => {
100
- expect(toSnapshot).toMatchSnapshot();
101
- });
102
- }
103
- }