@lumx/react 3.3.1-alpha.0 → 3.3.1

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 (117) hide show
  1. package/_internal/ClickAwayProvider.js.map +1 -1
  2. package/_internal/types.d.ts +0 -2
  3. package/index.d.ts +2 -0
  4. package/index.js +347 -75
  5. package/index.js.map +1 -1
  6. package/package.json +23 -19
  7. package/src/components/autocomplete/Autocomplete.test.tsx +55 -142
  8. package/src/components/autocomplete/AutocompleteMultiple.test.tsx +37 -75
  9. package/src/components/autocomplete/__mockData__/index.ts +6 -1
  10. package/src/components/badge/Badge.test.tsx +20 -64
  11. package/src/components/button/Button.test.tsx +44 -121
  12. package/src/components/button/ButtonGroup.test.tsx +16 -39
  13. package/src/components/button/IconButton.stories.tsx +7 -0
  14. package/src/components/button/IconButton.test.tsx +37 -78
  15. package/src/components/button/IconButton.tsx +8 -1
  16. package/src/components/checkbox/Checkbox.test.tsx +62 -67
  17. package/src/components/chip/Chip.test.tsx +89 -139
  18. package/src/components/chip/ChipGroup.test.tsx +27 -10
  19. package/src/components/date-picker/DatePicker.test.tsx +15 -23
  20. package/src/components/date-picker/DatePickerControlled.test.tsx +24 -20
  21. package/src/components/date-picker/DatePickerField.test.tsx +43 -27
  22. package/src/components/dialog/Dialog.test.tsx +36 -35
  23. package/src/components/divider/Divider.test.tsx +23 -69
  24. package/src/components/dropdown/Dropdown.test.tsx +30 -61
  25. package/src/components/expansion-panel/ExpansionPanel.test.tsx +12 -8
  26. package/src/components/flag/Flag.test.tsx +28 -53
  27. package/src/components/generic-block/GenericBlock.test.tsx +93 -89
  28. package/src/components/grid-column/GridColumn.stories.tsx +3 -3
  29. package/src/components/icon/Icon.test.tsx +80 -64
  30. package/src/components/index.ts +0 -2
  31. package/src/components/inline-list/InlineList.test.tsx +30 -17
  32. package/src/components/input-helper/InputHelper.test.tsx +21 -81
  33. package/src/components/input-label/InputLabel.test.tsx +19 -61
  34. package/src/components/lightbox/Lightbox.test.tsx +3 -2
  35. package/src/components/link/Link.test.tsx +47 -31
  36. package/src/components/link-preview/LinkPreview.test.tsx +51 -51
  37. package/src/components/message/Message.test.tsx +31 -52
  38. package/src/components/mosaic/Mosaic.test.tsx +56 -72
  39. package/src/components/notification/Notification.test.tsx +51 -82
  40. package/src/components/popover/Popover.tsx +7 -9
  41. package/src/components/progress-tracker/ProgressTracker.test.tsx +20 -33
  42. package/src/components/progress-tracker/ProgressTrackerProvider.test.tsx +61 -36
  43. package/src/components/progress-tracker/ProgressTrackerStep.test.tsx +19 -109
  44. package/src/components/progress-tracker/ProgressTrackerStepPanel.test.tsx +21 -58
  45. package/src/components/progress-tracker/ProgressTrackerStepPanel.tsx +1 -1
  46. package/src/components/radio-button/RadioButton.test.tsx +78 -92
  47. package/src/components/radio-button/RadioGroup.test.tsx +13 -59
  48. package/src/components/select/Select.test.tsx +115 -284
  49. package/src/components/select/SelectMultiple.stories.tsx +105 -2
  50. package/src/components/select/SelectMultiple.test.tsx +126 -322
  51. package/src/components/select/WithSelectContext.tsx +10 -4
  52. package/src/components/side-navigation/SideNavigation.test.tsx +22 -35
  53. package/src/components/side-navigation/SideNavigationItem.test.tsx +72 -139
  54. package/src/components/switch/Switch.test.tsx +70 -149
  55. package/src/components/table/Table.test.tsx +2 -0
  56. package/src/components/table/TableBody.test.tsx +18 -8
  57. package/src/components/table/TableCell.test.tsx +34 -9
  58. package/src/components/table/TableHeader.test.tsx +18 -8
  59. package/src/components/table/TableRow.test.tsx +28 -8
  60. package/src/components/tabs/Tab.test.tsx +27 -96
  61. package/src/components/tabs/TabList.test.tsx +21 -56
  62. package/src/components/tabs/TabPanel.test.tsx +20 -55
  63. package/src/components/tabs/TabPanel.tsx +1 -1
  64. package/src/components/tabs/TabProvider.test.tsx +158 -37
  65. package/src/components/tabs/test-utils.ts +39 -0
  66. package/src/components/text-field/TextField.stories.tsx +14 -5
  67. package/src/components/text-field/TextField.test.tsx +54 -8
  68. package/src/components/text-field/TextField.tsx +49 -5
  69. package/src/components/tooltip/Tooltip.test.tsx +134 -75
  70. package/src/components/tooltip/useInjectTooltipRef.tsx +9 -2
  71. package/src/components/uploader/Uploader.test.tsx +60 -48
  72. package/src/components/user-block/UserBlock.test.tsx +69 -13
  73. package/src/hooks/useFocusTrap.ts +2 -2
  74. package/src/testing/utils/commonTestsSuiteRTL.ts +18 -8
  75. package/src/testing/utils/index.ts +0 -1
  76. package/src/utils/flattenChildren.ts +5 -0
  77. package/src/components/autocomplete/__snapshots__/Autocomplete.test.tsx.snap +0 -213
  78. package/src/components/autocomplete/__snapshots__/AutocompleteMultiple.test.tsx.snap +0 -88
  79. package/src/components/badge/__snapshots__/Badge.test.tsx.snap +0 -11
  80. package/src/components/button/ButtonRoot.test.tsx +0 -203
  81. package/src/components/button/__snapshots__/Button.test.tsx.snap +0 -96
  82. package/src/components/button/__snapshots__/ButtonGroup.test.tsx.snap +0 -22
  83. package/src/components/button/__snapshots__/ButtonRoot.test.tsx.snap +0 -160
  84. package/src/components/button/__snapshots__/IconButton.test.tsx.snap +0 -83
  85. package/src/components/checkbox/__snapshots__/Checkbox.test.tsx.snap +0 -141
  86. package/src/components/chip/__snapshots__/Chip.test.tsx.snap +0 -12
  87. package/src/components/chip/__snapshots__/ChipGroup.test.tsx.snap +0 -29
  88. package/src/components/date-picker/__snapshots__/DatePicker.test.tsx.snap +0 -22
  89. package/src/components/date-picker/__snapshots__/DatePickerControlled.test.tsx.snap +0 -597
  90. package/src/components/date-picker/__snapshots__/DatePickerField.test.tsx.snap +0 -43
  91. package/src/components/divider/__snapshots__/Divider.test.tsx.snap +0 -9
  92. package/src/components/dropdown/__snapshots__/Dropdown.test.tsx.snap +0 -35
  93. package/src/components/icon/__snapshots__/Icon.test.tsx.snap +0 -49
  94. package/src/components/input-helper/__snapshots__/InputHelper.test.tsx.snap +0 -9
  95. package/src/components/input-label/__snapshots__/InputLabel.test.tsx.snap +0 -10
  96. package/src/components/link/__snapshots__/Link.test.tsx.snap +0 -29
  97. package/src/components/message/__snapshots__/Message.test.tsx.snap +0 -15
  98. package/src/components/notification/__snapshots__/Notification.test.tsx.snap +0 -34
  99. package/src/components/progress-tracker/__snapshots__/ProgressTracker.test.tsx.snap +0 -41
  100. package/src/components/progress-tracker/__snapshots__/ProgressTrackerStep.test.tsx.snap +0 -141
  101. package/src/components/progress-tracker/__snapshots__/ProgressTrackerStepPanel.test.tsx.snap +0 -25
  102. package/src/components/radio-button/__snapshots__/RadioButton.test.tsx.snap +0 -113
  103. package/src/components/radio-button/__snapshots__/RadioGroup.test.tsx.snap +0 -26
  104. package/src/components/select/__snapshots__/Select.test.tsx.snap +0 -43
  105. package/src/components/select/__snapshots__/SelectMultiple.test.tsx.snap +0 -87
  106. package/src/components/side-navigation/__snapshots__/SideNavigation.test.tsx.snap +0 -7
  107. package/src/components/side-navigation/__snapshots__/SideNavigationItem.test.tsx.snap +0 -30
  108. package/src/components/switch/__snapshots__/Switch.test.tsx.snap +0 -179
  109. package/src/components/tabs/__snapshots__/Tab.test.tsx.snap +0 -62
  110. package/src/components/tabs/__snapshots__/TabList.test.tsx.snap +0 -22
  111. package/src/components/tabs/__snapshots__/TabPanel.test.tsx.snap +0 -25
  112. package/src/components/tabs/test.mocks.ts +0 -33
  113. package/src/components/text-field/__snapshots__/TextField.test.tsx.snap +0 -42
  114. package/src/components/tooltip/__snapshots__/Tooltip.test.tsx.snap +0 -233
  115. package/src/components/uploader/__snapshots__/Uploader.test.tsx.snap +0 -14
  116. package/src/testing/utils/commonTestsSuite.ts +0 -71
  117. package/src/utils/flattenChildren.test.tsx +0 -58
@@ -1,10 +1,10 @@
1
- import React, { ReactElement } from 'react';
2
-
3
- import { mount, shallow } from 'enzyme';
4
- import 'jest-enzyme';
1
+ import React from 'react';
5
2
 
6
3
  import { Kind, Theme } from '@lumx/react';
7
- import { Wrapper } from '@lumx/react/testing/utils';
4
+ import { commonTestsSuiteRTL } from '@lumx/react/testing/utils';
5
+ import { getByClassName } from '@lumx/react/testing/utils/queries';
6
+ import { render } from '@testing-library/react';
7
+ import { INPUT_HELPER_CONFIGURATION } from '@lumx/react/components/input-helper/constants';
8
8
  import { InputHelper, InputHelperProps } from './InputHelper';
9
9
 
10
10
  const CLASSNAME = InputHelper.className as string;
@@ -14,94 +14,34 @@ type SetupProps = Partial<InputHelperProps>;
14
14
  /**
15
15
  * Mounts the component and returns common DOM elements / data needed in multiple tests further down.
16
16
  */
17
- const setup = (propsOverride: SetupProps = {}, shallowRendering = true) => {
17
+ const setup = (propsOverride: SetupProps = {}) => {
18
18
  const props: any = { ...propsOverride };
19
- const renderer: (el: ReactElement) => Wrapper = shallowRendering ? shallow : mount;
20
- const wrapper: Wrapper = renderer(<InputHelper {...props} />);
21
- const helper: Wrapper = wrapper.find('.lumx-input-helper');
22
-
23
- return { helper, props, wrapper };
24
- };
19
+ render(<InputHelper {...props} />);
20
+ const helper = getByClassName(document.body, CLASSNAME);
25
21
 
26
- const properties = {
27
- error: {
28
- children: 'Error',
29
- kind: Kind.error,
30
- },
31
- info: {
32
- children: 'Info',
33
- kind: Kind.info,
34
- },
35
- success: {
36
- children: 'Success',
37
- kind: Kind.success,
38
- },
39
- warning: {
40
- children: 'Warning',
41
- kind: Kind.warning,
42
- },
22
+ return { helper, props };
43
23
  };
44
24
 
45
25
  describe(`<${InputHelper.displayName}>`, () => {
46
- // 1. Test render via snapshot (default states of component).
47
- describe('Snapshots and structure', () => {
48
- it('should render defaults', () => {
49
- const children = 'The helper text';
50
- const { wrapper, helper } = setup({ children });
51
-
52
- expect(wrapper).toMatchSnapshot();
53
- expect(helper).toExist();
54
- expect(helper).toHaveClassName(CLASSNAME);
55
- expect(helper).toHaveClassName(`${CLASSNAME}--theme-light`);
56
- expect(helper).toHaveText(children);
57
- });
58
- });
59
-
60
- // 2. Test defaultProps value and important props custom values.
61
26
  describe('Props', () => {
62
- it('should render info', () => {
63
- const { helper } = setup(properties.info);
64
- expect(helper).toHaveText(properties.info.children);
65
- });
66
- it('should render error', () => {
67
- const { helper } = setup(properties.error);
68
- expect(helper).toIncludeText(properties.error.children);
69
- });
70
- it('should render valid', () => {
71
- const { helper } = setup(properties.success);
72
- expect(helper).toIncludeText(properties.success.children);
73
- });
74
- it('should render warning', () => {
75
- const { helper } = setup(properties.warning);
76
- expect(helper).toIncludeText(properties.warning.children);
27
+ it('should render text', () => {
28
+ const { props, helper } = setup({ children: 'helper text' });
29
+
30
+ expect(helper).toHaveTextContent(props.children);
31
+ expect(helper.className).toMatchInlineSnapshot(`"lumx-input-helper lumx-input-helper--theme-light"`);
77
32
  });
78
33
 
79
34
  it('should render dark theme', () => {
80
- const { helper } = setup({ ...properties.info, theme: Theme.dark });
81
- expect(helper).toHaveClassName(CLASSNAME);
82
- expect(helper).toHaveClassName(`${CLASSNAME}--theme-dark`);
35
+ const { helper } = setup({ theme: Theme.dark });
36
+ expect(helper).toHaveClass('lumx-input-helper--theme-dark');
83
37
  });
84
38
 
85
- it('should render custom className', () => {
86
- const className = 'my-class__test';
87
- const { helper } = setup({ ...properties.info, className });
88
- expect(helper).toHaveClassName(CLASSNAME);
89
- expect(helper).toHaveClassName(className);
39
+ it.each(Object.values(Kind))('should render kind %s', (kind) => {
40
+ const { helper } = setup({ kind });
41
+ const { color } = INPUT_HELPER_CONFIGURATION[kind as any] || {};
42
+ if (color) expect(helper).toHaveClass(`${CLASSNAME}--color-${color}`);
90
43
  });
91
44
  });
92
45
 
93
- // 3. Test events.
94
- describe('Events', () => {
95
- // Nothing to do here.
96
- });
97
-
98
- // 4. Test conditions (i.e. things that display or not in the UI based on props).
99
- describe('Conditions', () => {
100
- // Nothing to do here.
101
- });
102
-
103
- // 5. Test state.
104
- describe('State', () => {
105
- // Nothing to do here.
106
- });
46
+ commonTestsSuiteRTL(setup, { baseClassName: CLASSNAME, forwardClassName: 'helper', forwardAttributes: 'helper' });
107
47
  });
@@ -1,10 +1,9 @@
1
- import React, { ReactElement } from 'react';
2
-
3
- import { mount, shallow } from 'enzyme';
4
- import 'jest-enzyme';
1
+ import React from 'react';
5
2
 
6
3
  import { Theme } from '@lumx/react';
7
- import { Wrapper } from '@lumx/react/testing/utils';
4
+ import { getByClassName } from '@lumx/react/testing/utils/queries';
5
+ import { render } from '@testing-library/react';
6
+ import { commonTestsSuiteRTL } from '@lumx/react/testing/utils';
8
7
  import { InputLabel, InputLabelProps } from './InputLabel';
9
8
 
10
9
  const CLASSNAME = InputLabel.className as string;
@@ -14,73 +13,32 @@ type SetupProps = Partial<InputLabelProps>;
14
13
  /**
15
14
  * Mounts the component and returns common DOM elements / data needed in multiple tests further down.
16
15
  */
17
- const setup = (propsOverride: SetupProps = {}, shallowRendering = true) => {
16
+ const setup = (propsOverride: SetupProps = {}) => {
18
17
  const props: any = { htmlFor: 'id', ...propsOverride };
19
- const renderer: (el: ReactElement) => Wrapper = shallowRendering ? shallow : mount;
20
- const wrapper: Wrapper = renderer(<InputLabel {...props} />);
21
- const label: Wrapper = wrapper.find('.lumx-input-label');
18
+ render(<InputLabel {...props} />);
19
+ const label = getByClassName(document.body, CLASSNAME);
22
20
 
23
- return { label, props, wrapper };
21
+ return { label, props };
24
22
  };
25
23
 
26
24
  describe(`<${InputLabel.displayName}>`, () => {
27
- // 1. Test render via snapshot (default states of component).
28
- describe('Snapshots and structure', () => {
29
- it('should render defaults', () => {
30
- const children = 'The Label text';
31
- const { wrapper, label } = setup({ children });
32
-
33
- expect(wrapper).toMatchSnapshot();
34
- expect(label).toExist();
35
- expect(label).toHaveClassName(CLASSNAME);
36
- expect(label).toHaveClassName(`${CLASSNAME}--theme-light`);
37
- expect(label).toHaveText(children);
38
- });
39
- });
40
-
41
- // 2. Test defaultProps value and important props custom values.
42
25
  describe('Props', () => {
43
- it('should render children', () => {
44
- const children = (
45
- <span>
46
- The label<strong>*</strong>
47
- </span>
48
- );
49
-
50
- const { label } = setup({
51
- children,
26
+ it('should render text', () => {
27
+ const { label, props } = setup({
28
+ children: 'Some label text',
52
29
  htmlFor: '123',
53
30
  });
54
- expect(label).toContainReact(children);
55
- expect(label).toHaveProp('htmlFor', '123');
31
+ expect(label).toHaveTextContent(props.children);
32
+ expect(label).toHaveAttribute('for', props.htmlFor);
56
33
  });
57
34
 
58
- it('should render dark theme', () => {
59
- const { label } = setup({ children: 'The label', theme: Theme.dark });
60
- expect(label).toHaveClassName(CLASSNAME);
61
- expect(label).toHaveClassName(`${CLASSNAME}--theme-dark`);
35
+ it('should render dark theme & required', () => {
36
+ const { label } = setup({ children: 'The label', theme: Theme.dark, isRequired: true });
37
+ expect(label).toHaveClass(CLASSNAME);
38
+ expect(label).toHaveClass(`${CLASSNAME}--theme-dark`);
39
+ expect(label).toHaveClass(`${CLASSNAME}--is-required`);
62
40
  });
63
-
64
- it('should render custom className', () => {
65
- const className = 'my-class__test';
66
- const { label } = setup({ children: 'The label', className });
67
- expect(label).toHaveClassName(CLASSNAME);
68
- expect(label).toHaveClassName(className);
69
- });
70
- });
71
-
72
- // 3. Test events.
73
- describe('Events', () => {
74
- // Nothing to do here.
75
41
  });
76
42
 
77
- // 4. Test conditions (i.e. things that display or not in the UI based on props).
78
- describe('Conditions', () => {
79
- // Nothing to do here.
80
- });
81
-
82
- // 5. Test state.
83
- describe('State', () => {
84
- // Nothing to do here.
85
- });
43
+ commonTestsSuiteRTL(setup, { baseClassName: CLASSNAME, forwardClassName: 'label', forwardAttributes: 'label' });
86
44
  });
@@ -1,9 +1,9 @@
1
1
  import React from 'react';
2
- import 'jest-enzyme';
3
- import { commonTestsSuiteRTL } from '@lumx/react/testing/utils';
4
2
 
3
+ import { commonTestsSuiteRTL } from '@lumx/react/testing/utils';
5
4
  import { queryByClassName } from '@lumx/react/testing/utils/queries';
6
5
  import { render } from '@testing-library/react';
6
+
7
7
  import { Lightbox, LightboxProps } from './Lightbox';
8
8
 
9
9
  const CLASSNAME = Lightbox.className as string;
@@ -21,5 +21,6 @@ describe(`<${Lightbox.displayName}>`, () => {
21
21
  baseClassName: CLASSNAME,
22
22
  forwardClassName: 'lightbox',
23
23
  forwardAttributes: 'lightbox',
24
+ forwardRef: 'lightbox',
24
25
  });
25
26
  });
@@ -1,60 +1,76 @@
1
- import React, { ReactElement } from 'react';
2
-
3
- import { mount, shallow } from 'enzyme';
4
- import 'jest-enzyme';
1
+ import React from 'react';
5
2
 
6
3
  import { ColorPalette, ColorVariant, Typography } from '@lumx/react';
7
- import { commonTestsSuite, Wrapper } from '@lumx/react/testing/utils';
4
+ import { commonTestsSuiteRTL } from '@lumx/react/testing/utils';
5
+ import { getByClassName, queryByClassName } from '@lumx/react/testing/utils/queries';
6
+ import { render, screen } from '@testing-library/react';
7
+ import { mdiCheck, mdiPlus } from '@lumx/icons';
8
8
  import { Link, LinkProps } from './Link';
9
9
 
10
10
  const CLASSNAME = Link.className as string;
11
11
 
12
- type SetupProps = Partial<LinkProps>;
13
-
14
12
  /**
15
13
  * Mounts the component and returns common DOM elements / data needed in multiple tests further down.
16
14
  */
17
- const setup = ({ ...propsOverride }: SetupProps = {}, shallowRendering = true) => {
18
- const props: LinkProps = {
19
- ...propsOverride,
20
- };
21
- const renderer: (el: ReactElement) => Wrapper = shallowRendering ? shallow : mount;
22
- const wrapper: Wrapper = renderer(<Link {...props} />);
23
-
24
- return { props, wrapper };
15
+ const setup = (props: LinkProps = {}) => {
16
+ render(<Link {...props} />);
17
+ const link = getByClassName(document.body, CLASSNAME);
18
+ const content = queryByClassName(link, `${CLASSNAME}__content`);
19
+ const rightIcon = queryByClassName(link, `${CLASSNAME}__right-icon`);
20
+ const leftIcon = queryByClassName(link, `${CLASSNAME}__left-icon`);
21
+
22
+ return { props, link, content, rightIcon, leftIcon };
25
23
  };
26
24
 
27
25
  describe(`<${Link.displayName}>`, () => {
28
- describe('Snapshots and structure', () => {
29
- it('should render correctly', () => {
30
- const { wrapper } = setup({ href: 'https://google.com' });
31
- expect(wrapper).toMatchSnapshot();
26
+ describe('Props', () => {
27
+ it('should render link', () => {
28
+ const name = 'Link';
29
+ const { props, link, rightIcon, leftIcon } = setup({ href: 'https://example.com', children: name });
30
+
31
+ expect(link).toBe(screen.queryByRole('link', { name }));
32
+ expect(link).toHaveAttribute('href', props.href);
33
+ expect(link.className).toMatchInlineSnapshot(`"lumx-link"`);
34
+
35
+ expect(rightIcon).not.toBeInTheDocument();
36
+ expect(leftIcon).not.toBeInTheDocument();
32
37
  });
33
38
 
34
39
  it('should render color & color variant', () => {
35
- const { wrapper } = setup({
40
+ const { link } = setup({
36
41
  href: 'https://google.com',
37
42
  color: ColorPalette.primary,
38
43
  colorVariant: ColorVariant.D1,
39
44
  });
40
- expect(wrapper).toMatchSnapshot();
45
+ expect(link.className).toMatchInlineSnapshot(
46
+ `"lumx-link lumx-link--color-primary lumx-link--color-variant-D1"`,
47
+ );
41
48
  });
42
49
 
43
50
  it('should render typography', () => {
44
- const { wrapper } = setup({ href: 'https://google.com', typography: Typography.title });
45
- expect(wrapper).toMatchSnapshot();
51
+ const { content } = setup({ href: 'https://google.com', typography: Typography.title });
52
+ expect(content?.className).toMatchInlineSnapshot(`undefined`);
53
+ });
54
+
55
+ it('should render a button', () => {
56
+ const name = 'Link';
57
+ const onClick = jest.fn();
58
+ const { link } = setup({ onClick, children: name });
59
+ expect(link).toBe(screen.queryByRole('button', { name }));
46
60
  });
47
61
 
48
- it('should render a button if href attribute is missing', () => {
49
- const { wrapper } = setup();
50
- expect(wrapper).toMatchSnapshot();
62
+ it('should render with icons', () => {
63
+ const { rightIcon, leftIcon } = setup({ rightIcon: mdiPlus, leftIcon: mdiCheck });
64
+ expect(rightIcon).toBeInTheDocument();
65
+ expect(leftIcon).toBeInTheDocument();
51
66
  });
52
67
  });
53
68
 
54
69
  // Common tests suite.
55
- commonTestsSuite(
56
- setup,
57
- { className: 'wrapper', prop: 'wrapper' },
58
- { className: CLASSNAME, prop: 'href', propValue: 'https://google.com' },
59
- );
70
+ commonTestsSuiteRTL(setup, {
71
+ baseClassName: CLASSNAME,
72
+ forwardAttributes: 'link',
73
+ forwardClassName: 'link',
74
+ forwardRef: 'link',
75
+ });
60
76
  });
@@ -1,16 +1,14 @@
1
- import React, { ReactElement } from 'react';
1
+ import React from 'react';
2
2
 
3
- import { mount, shallow } from 'enzyme';
4
- import 'jest-enzyme';
5
-
6
- import { commonTestsSuite, Wrapper } from '@lumx/react/testing/utils';
3
+ import { commonTestsSuiteRTL } from '@lumx/react/testing/utils';
7
4
  import { getBasicClass } from '@lumx/react/utils/className';
8
5
  import { Link, Thumbnail } from '@lumx/react';
6
+ import { render, screen, within } from '@testing-library/react';
7
+ import { getByClassName, queryByClassName, queryAllByClassName } from '@lumx/react/testing/utils/queries';
9
8
 
10
9
  import { Size, Theme } from '..';
11
10
  import { LinkPreview, LinkPreviewProps } from './LinkPreview';
12
11
 
13
- const DEFAULT_PROPS = LinkPreview.defaultProps as any;
14
12
  const CLASSNAME = LinkPreview.className as string;
15
13
 
16
14
  type SetupProps = Partial<LinkPreviewProps>;
@@ -18,46 +16,47 @@ type SetupProps = Partial<LinkPreviewProps>;
18
16
  /**
19
17
  * Mounts the component and returns common DOM elements / data needed in multiple tests further down.
20
18
  */
21
- const setup = (propsOverride: SetupProps = {}, shallowRendering = true) => {
19
+ const setup = (propsOverride: SetupProps = {}) => {
22
20
  const props: any = { ...propsOverride };
23
- const renderer: (el: ReactElement) => Wrapper = shallowRendering ? shallow : mount;
24
- const wrapper: Wrapper = renderer(<LinkPreview {...props} />);
25
-
26
- return {
27
- thumbnail: wrapper.find(Thumbnail),
28
- title: wrapper.find(`.${CLASSNAME}__title`),
29
- description: wrapper.find(`.${CLASSNAME}__description`),
30
- link: wrapper.find(`.${CLASSNAME}__link`).find(Link),
31
- props,
32
- wrapper,
33
- };
21
+
22
+ render(<LinkPreview {...props} />);
23
+ const linkPreview = getByClassName(document.body, CLASSNAME);
24
+ const thumbnail = queryByClassName(linkPreview, Thumbnail.className as string);
25
+ const title = queryByClassName(linkPreview, `${CLASSNAME}__title`);
26
+ const description = queryByClassName(linkPreview, `${CLASSNAME}__description`);
27
+ const link = queryAllByClassName(linkPreview, `${CLASSNAME}__link`)?.[1];
28
+
29
+ return { props, linkPreview, thumbnail, title, description, link };
34
30
  };
35
31
 
36
32
  describe(`<${LinkPreview.displayName}>`, () => {
37
33
  it('should render with default props', () => {
38
- const { wrapper, thumbnail, title, link, description } = setup();
39
- expect(wrapper).toHaveClassName(CLASSNAME);
40
-
41
- ['size', 'theme'].forEach((type) => {
42
- expect(wrapper).toHaveClassName(getBasicClass({ prefix: CLASSNAME, type, value: DEFAULT_PROPS[type] }));
43
- });
44
- expect(thumbnail).not.toExist();
45
- expect(title).not.toExist();
46
- expect(link).toExist();
47
- expect(link).toHaveProp('tabIndex', undefined);
48
- expect(description).not.toExist();
34
+ const { linkPreview, thumbnail, title, link, description } = setup();
35
+ expect(linkPreview).toHaveClass(CLASSNAME);
36
+ expect(linkPreview).toHaveClass(`${CLASSNAME}--size-regular`);
37
+ expect(linkPreview).toHaveClass(`${CLASSNAME}--theme-light`);
38
+
39
+ expect(thumbnail).not.toBeInTheDocument();
40
+ expect(title).not.toBeInTheDocument();
41
+ expect(link).toBeInTheDocument();
42
+ expect(link).not.toHaveAttribute('tabindex');
43
+ expect(description).not.toBeInTheDocument();
49
44
  });
50
45
 
51
46
  it('should render with only the title', () => {
52
- const { title, link } = setup({ title: 'Title' });
47
+ const { props, title, link } = setup({ title: 'Title', link: 'https://example.com' });
53
48
 
54
- expect(title).toExist();
55
- expect(link).toExist();
56
- expect(link).toHaveProp('tabIndex', '-1');
49
+ expect(title).toBe(screen.queryByRole('heading', { name: props.title }));
50
+ const titleLink = within(title as any).queryByRole('link');
51
+ expect(titleLink).toBeInTheDocument();
52
+ expect(titleLink).toHaveAttribute('href', props.link);
53
+
54
+ expect(link).toBeInTheDocument();
55
+ expect(link).toHaveAttribute('tabindex', '-1');
57
56
  });
58
57
 
59
58
  it('should render with complete props', () => {
60
- const { wrapper, thumbnail, title, link, description, props } = setup({
59
+ const { linkPreview, thumbnail, title, link, description, props } = setup({
61
60
  size: Size.big,
62
61
  theme: Theme.dark,
63
62
  thumbnailProps: { image: 'https://example.com/thumbnail.jpg', alt: '' },
@@ -68,35 +67,36 @@ describe(`<${LinkPreview.displayName}>`, () => {
68
67
  });
69
68
 
70
69
  const validateLink = (linkElement: any) => {
71
- expect(linkElement).toHaveProp('href', props.link);
70
+ expect(linkElement).toHaveAttribute('href', props.link);
72
71
  // Props forwarding
73
- expect(linkElement).toHaveProp('data-custom-attr', 'true');
72
+ expect(linkElement).toHaveAttribute('data-custom-attr', 'true');
74
73
  };
75
74
 
76
- expect(wrapper).toExist();
75
+ expect(linkPreview).toBeInTheDocument();
76
+ expect(linkPreview).toHaveClass(`${CLASSNAME}--size-big`);
77
+ expect(linkPreview).toHaveClass(`${CLASSNAME}--theme-dark`);
77
78
 
78
79
  // Thumbnail
79
- expect(thumbnail).toExist();
80
- validateLink((thumbnail as any).dive());
80
+ expect(thumbnail).toBeInTheDocument();
81
+ validateLink(thumbnail);
81
82
 
82
83
  // Title
83
- expect(title).toHaveText(props.title);
84
- validateLink(title.find(Link));
84
+ expect(title).toHaveTextContent(props.title);
85
+ validateLink(within(title as any).queryByRole('link'));
85
86
 
86
87
  // Link
87
- expect(link).toHaveText(props.link);
88
- validateLink(link.find(Link));
88
+ expect(link).toHaveTextContent(props.link);
89
+ validateLink(link);
89
90
 
90
91
  // Description
91
- expect(description).toHaveText(props.description);
92
-
93
- // Size prop applied
94
- expect(wrapper).toHaveClassName(getBasicClass({ prefix: CLASSNAME, type: 'size', value: props.size }));
95
-
96
- // Dark theme applied
97
- expect(wrapper).toHaveClassName(getBasicClass({ prefix: CLASSNAME, type: 'theme', value: Theme.dark }));
92
+ expect(description).toHaveTextContent(props.description);
98
93
  });
99
94
 
100
95
  // Common tests suite.
101
- commonTestsSuite(setup, { className: 'wrapper', prop: 'wrapper' }, { className: CLASSNAME });
96
+ commonTestsSuiteRTL(setup, {
97
+ baseClassName: CLASSNAME,
98
+ forwardClassName: 'linkPreview',
99
+ forwardAttributes: 'linkPreview',
100
+ forwardRef: 'linkPreview',
101
+ });
102
102
  });
@@ -1,9 +1,10 @@
1
- import { commonTestsSuite, Wrapper } from '@lumx/react/testing/utils';
1
+ import { commonTestsSuiteRTL } from '@lumx/react/testing/utils';
2
2
  import { Kind } from '@lumx/react';
3
- import { getBasicClass } from '@lumx/react/utils/className';
4
- import { mount, shallow } from 'enzyme';
5
- import 'jest-enzyme';
6
- import React, { ReactElement } from 'react';
3
+
4
+ import React from 'react';
5
+ import { render } from '@testing-library/react';
6
+ import { getByClassName, queryByClassName } from '@lumx/react/testing/utils/queries';
7
+ import { mdiAbTesting } from '@lumx/icons';
7
8
  import { Message, MessageProps } from './Message';
8
9
 
9
10
  const CLASSNAME = Message.className as string;
@@ -13,64 +14,42 @@ type SetupProps = Partial<MessageProps>;
13
14
  /**
14
15
  * Mounts the component and returns common DOM elements / data needed in multiple tests further down.
15
16
  */
16
- const setup = (propsOverride: SetupProps = {}, shallowRendering = true) => {
17
+ const setup = (propsOverride: SetupProps = {}) => {
17
18
  const props: any = { ...propsOverride };
18
- const renderer: (el: ReactElement) => Wrapper = shallowRendering ? shallow : mount;
19
- const wrapper: Wrapper = renderer(
20
- <Message {...props}>
21
- <span>Lorem Ipsum</span>
22
- </Message>,
23
- );
24
- const message: Wrapper = wrapper.find('div').first();
25
-
26
- return { message, props, wrapper };
19
+ render(<Message {...props} />);
20
+ const message = getByClassName(document.body, CLASSNAME);
21
+ const icon = queryByClassName(message, `${CLASSNAME}__icon`);
22
+ return { message, icon, props };
27
23
  };
28
24
 
29
25
  describe(`<${Message.displayName}>`, () => {
30
- // 1. Test render via snapshot (default states of component).
31
- describe('Snapshots and structure', () => {
32
- // Here is an example of a basic rendering check, with snapshot.
33
-
34
- it('should render correctly', () => {
35
- const { wrapper } = setup();
36
- expect(wrapper).toMatchSnapshot();
37
-
38
- expect(wrapper).toExist();
39
- expect(wrapper).toHaveClassName(CLASSNAME);
40
- });
41
- });
42
-
43
- // 2. Test defaultProps value and important props custom values.
44
26
  describe('Props', () => {
45
- // Here are some examples of basic props check.
46
-
47
- it('should use the given `kind`', () => {
48
- const testedProp = 'kind';
49
- const modifiedProps: Partial<MessageProps> = {
50
- [testedProp]: Kind.success,
51
- };
27
+ it('should render default', () => {
28
+ const { message, icon } = setup({ children: 'Message text' });
29
+ expect(message).toBeInTheDocument();
30
+ expect(message.className).toMatchInlineSnapshot(`"lumx-message"`);
31
+ expect(message).toHaveTextContent('Message text');
52
32
 
53
- const { message } = setup(modifiedProps);
54
-
55
- expect(message).toHaveClassName(getBasicClass({ prefix: CLASSNAME, type: 'color', value: `green` }));
33
+ expect(icon).not.toBeInTheDocument();
56
34
  });
57
- });
58
35
 
59
- // 3. Test events.
60
- describe('Events', () => {
61
- // Nothing to do here.
62
- });
36
+ it('should render hasBackground', () => {
37
+ const { message } = setup({ hasBackground: true });
38
+ expect(message).toHaveClass(`${CLASSNAME}--has-background`);
39
+ });
63
40
 
64
- // 4. Test conditions (i.e. things that display or not in the UI based on props).
65
- describe('Conditions', () => {
66
- // Nothing to do here.
67
- });
41
+ it('should render icon', () => {
42
+ const { icon } = setup({ icon: mdiAbTesting });
43
+ expect(icon).toBeInTheDocument();
44
+ });
68
45
 
69
- // 5. Test state.
70
- describe('State', () => {
71
- // Nothing to do here.
46
+ it.each(Object.values(Kind))('should render kind %s', (kind) => {
47
+ const { message, icon } = setup({ kind });
48
+ expect(message.className).toEqual(expect.stringMatching(/\blumx-message--color-\w+\b/));
49
+ expect(icon).toBeInTheDocument();
50
+ });
72
51
  });
73
52
 
74
53
  // Common tests suite.
75
- commonTestsSuite(setup, { className: 'wrapper', prop: 'wrapper' }, { className: CLASSNAME });
54
+ commonTestsSuiteRTL(setup, { baseClassName: CLASSNAME, forwardClassName: 'message', forwardAttributes: 'message' });
76
55
  });