Skip to content

Commit 9f3b30d

Browse files
committed
adds lgids and test utils
1 parent c9ba506 commit 9f3b30d

File tree

9 files changed

+144
-14
lines changed

9 files changed

+144
-14
lines changed

packages/wizard/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { WizardSubComponentProperties } from './constants';
2+
export { getLgIds } from './utils/getLgIds';
23
export { Wizard, type WizardProps } from './Wizard';
34
export {
45
useWizardContext,

templates/delete-wizard/src/DeleteWizard/DeleteWizard.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
import { cx } from '@leafygreen-ui/emotion';
88
import { Wizard } from '@leafygreen-ui/wizard';
99

10+
import { DEFAULT_LGID_ROOT, getLgIds } from '../utils/getLgIds';
11+
1012
import { DeleteWizardSubComponentKeys } from './compoundComponentProperties';
1113
import { wizardWrapperStyles } from './DeleteWizard.styles';
1214
import { DeleteWizardProps } from './DeleteWizard.types';
@@ -27,14 +29,29 @@ export const DeleteWizard = CompoundComponent(
2729
onCancel,
2830
onDelete,
2931
onStepChange,
32+
'data-lgid': dataLgId = DEFAULT_LGID_ROOT,
33+
...rest
3034
}: DeleteWizardProps) => {
35+
const lgIds = getLgIds(dataLgId);
3136
const header = findChild(children, DeleteWizardSubComponentKeys.Header);
3237

3338
return (
34-
<div className={cx(wizardWrapperStyles, className)}>
39+
<div
40+
className={cx(wizardWrapperStyles, className)}
41+
data-lgid={lgIds.root}
42+
{...rest}
43+
>
3544
{header}
36-
<DeleteWizardContextProvider onCancel={onCancel} onDelete={onDelete}>
37-
<Wizard activeStep={activeStep} onStepChange={onStepChange}>
45+
<DeleteWizardContextProvider
46+
onCancel={onCancel}
47+
onDelete={onDelete}
48+
lgIds={lgIds}
49+
>
50+
<Wizard
51+
activeStep={activeStep}
52+
onStepChange={onStepChange}
53+
data-lgid={lgIds.wizard}
54+
>
3855
{children}
3956
</Wizard>
4057
</DeleteWizardContextProvider>

templates/delete-wizard/src/DeleteWizard/DeleteWizardContext.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,32 @@ import React, { createContext, PropsWithChildren, useContext } from 'react';
22

33
import { useWizardContext, WizardContextData } from '@leafygreen-ui/wizard';
44

5+
import { getLgIds, GetLgIdsReturnType } from '../utils/getLgIds';
6+
57
import { DeleteWizardProps } from './DeleteWizard.types';
68

79
export interface DeleteWizardContextData {
810
onCancel?: DeleteWizardProps['onCancel'];
911
onDelete?: DeleteWizardProps['onDelete'];
12+
lgIds: GetLgIdsReturnType;
1013
}
1114

12-
export const DeleteWizardContext = createContext<DeleteWizardContextData>({});
15+
export const DeleteWizardContext = createContext<DeleteWizardContextData>({
16+
lgIds: getLgIds(),
17+
});
1318

1419
export const DeleteWizardContextProvider = ({
1520
children,
1621
onCancel,
1722
onDelete,
23+
lgIds,
1824
}: PropsWithChildren<DeleteWizardContextData>) => {
1925
return (
2026
<DeleteWizardContext.Provider
2127
value={{
2228
onCancel,
2329
onDelete,
30+
lgIds,
2431
}}
2532
>
2633
{children}
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
import { CanvasHeader } from '@leafygreen-ui/canvas-header';
1+
import React from 'react';
2+
3+
import { CanvasHeader, CanvasHeaderProps } from '@leafygreen-ui/canvas-header';
24
import { CompoundSubComponent } from '@leafygreen-ui/compound-component';
35

46
import { DeleteWizardSubComponentKeys } from './compoundComponentProperties';
57

68
/**
79
* A wrapper around the {@link CanvasHeader} component for embedding into a DeleteWizard
810
*/
9-
export const DeleteWizardHeader = CompoundSubComponent(CanvasHeader, {
10-
displayName: 'DeleteWizardHeader',
11-
key: DeleteWizardSubComponentKeys.Header,
12-
});
11+
export const DeleteWizardHeader = CompoundSubComponent(
12+
(props: CanvasHeaderProps) => {
13+
return <CanvasHeader {...props} />;
14+
},
15+
{
16+
displayName: 'DeleteWizardHeader',
17+
key: DeleteWizardSubComponentKeys.Header,
18+
},
19+
);

templates/delete-wizard/src/DeleteWizard/DeleteWizardStep.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from '@leafygreen-ui/wizard';
1515

1616
import { DeleteWizardSubComponentKeys } from './compoundComponentProperties';
17+
import { useDeleteWizardContext } from './DeleteWizardContext';
1718

1819
export const useDeleteWizardStepContext = useWizardStepContext;
1920

@@ -30,6 +31,7 @@ export const DeleteWizardStep = CompoundSubComponent(
3031
requiresAcknowledgement,
3132
...rest
3233
}: DeleteWizardStepProps) => {
34+
const { lgIds } = useDeleteWizardContext();
3335
const footerChild = findChild(
3436
children,
3537
WizardSubComponentProperties.Footer,
@@ -48,6 +50,7 @@ export const DeleteWizardStep = CompoundSubComponent(
4850
`,
4951
className,
5052
)}
53+
data-lgid={lgIds.step}
5154
{...rest}
5255
>
5356
{restChildren}
Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,86 @@
11
import { findByLgId, getByLgId, queryByLgId } from '@lg-tools/test-harnesses';
22

33
import { LgIdString } from '@leafygreen-ui/lib';
4+
import { getTestUtils as getWizardTestUtils } from '@leafygreen-ui/wizard/testing';
45

56
import { DEFAULT_LGID_ROOT, getLgIds } from '../utils/getLgIds';
67

7-
import { TestUtilsReturnType } from './getTestUtils.types';
8+
import { DeleteWizardTestUtilsReturnType } from './getTestUtils.types';
89

910
export const getTestUtils = (
1011
lgId: LgIdString = DEFAULT_LGID_ROOT,
11-
): TestUtilsReturnType => {
12+
): DeleteWizardTestUtilsReturnType => {
1213
const lgIds = getLgIds(lgId);
1314

14-
return {};
15+
// Get the Wizard test utils (the DeleteWizard wraps a Wizard component internally)
16+
const wizardUtils = getWizardTestUtils(lgId);
17+
18+
/**
19+
* @returns the DeleteWizard root element using the `data-lgid` data attribute.
20+
* Will throw if no elements match or if more than one match is found.
21+
*/
22+
const getDeleteWizard = () => getByLgId!<HTMLElement>(lgIds.root);
23+
24+
/**
25+
* @returns the DeleteWizard root element using the `data-lgid` data attribute or `null` if no elements match.
26+
* Will throw if more than one match is found.
27+
*/
28+
const queryDeleteWizard = () => queryByLgId!<HTMLElement>(lgIds.root);
29+
30+
/**
31+
* @returns a promise that resolves to the DeleteWizard root element using the `data-lgid` data attribute.
32+
* The promise is rejected if no elements match or if more than one match is found.
33+
*/
34+
const findDeleteWizard = () => findByLgId!<HTMLElement>(lgIds.root);
35+
36+
/**
37+
* @returns the DeleteWizard Header element using the `data-lgid` data attribute.
38+
* Will throw if no elements match or if more than one match is found.
39+
*/
40+
const getHeader = () => getByLgId!<HTMLElement>(lgIds.header);
41+
42+
/**
43+
* @returns the DeleteWizard Header element using the `data-lgid` data attribute or `null` if no elements match.
44+
* Will throw if more than one match is found.
45+
*/
46+
const queryHeader = () => queryByLgId!<HTMLElement>(lgIds.header);
47+
48+
/**
49+
* @returns a promise that resolves to the DeleteWizard Header element using the `data-lgid` data attribute.
50+
* The promise is rejected if no elements match or if more than one match is found.
51+
*/
52+
const findHeader = () => findByLgId!<HTMLElement>(lgIds.header);
53+
54+
/**
55+
* @returns the currently active WizardStep element using the `data-lgid` data attribute.
56+
* Will throw if no elements match or if more than one match is found.
57+
*/
58+
const getActiveStep = () => getByLgId!<HTMLElement>(lgIds.step);
59+
60+
/**
61+
* @returns the currently active WizardStep element using the `data-lgid` data attribute or `null` if no elements match.
62+
* Will throw if more than one match is found.
63+
*/
64+
const queryActiveStep = () => queryByLgId!<HTMLElement>(lgIds.step);
65+
66+
/**
67+
* @returns a promise that resolves to the currently active WizardStep element using the `data-lgid` data attribute.
68+
* The promise is rejected if no elements match or if more than one match is found.
69+
*/
70+
const findActiveStep = () => findByLgId!<HTMLElement>(lgIds.step);
71+
72+
return {
73+
// Spread all Wizard test utils (footer, buttons, etc.)
74+
...wizardUtils,
75+
// DeleteWizard-specific utils
76+
getDeleteWizard,
77+
queryDeleteWizard,
78+
findDeleteWizard,
79+
getHeader,
80+
queryHeader,
81+
findHeader,
82+
getActiveStep,
83+
queryActiveStep,
84+
findActiveStep,
85+
};
1586
};
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
export interface TestUtilsReturnType {}
1+
import { TestUtilsReturnType as WizardTestUtilsReturnType } from '@leafygreen-ui/wizard/testing';
2+
3+
export interface DeleteWizardTestUtilsReturnType
4+
extends WizardTestUtilsReturnType {
5+
// DeleteWizard root element utils
6+
getDeleteWizard: () => HTMLElement;
7+
queryDeleteWizard: () => HTMLElement | null;
8+
findDeleteWizard: () => Promise<HTMLElement>;
9+
10+
// Header utils
11+
getHeader: () => HTMLElement;
12+
queryHeader: () => HTMLElement | null;
13+
findHeader: () => Promise<HTMLElement>;
14+
15+
// Active Step utils (from Wizard)
16+
getActiveStep: () => HTMLElement;
17+
queryActiveStep: () => HTMLElement | null;
18+
findActiveStep: () => Promise<HTMLElement>;
19+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export { getTestUtils } from './getTestUtils';
2-
export { type TestUtilsReturnType } from './getTestUtils.types';
2+
export { type DeleteWizardTestUtilsReturnType } from './getTestUtils.types';

templates/delete-wizard/src/utils/getLgIds.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import { LgIdString } from '@leafygreen-ui/lib';
2+
import { getLgIds as getWizardLgIds } from '@leafygreen-ui/wizard';
23

34
export const DEFAULT_LGID_ROOT = 'lg-delete_wizard';
45

56
export const getLgIds = (root: LgIdString = DEFAULT_LGID_ROOT) => {
7+
const wizardRoot: LgIdString = `${root}-wizard`;
8+
const wizardLgIds = getWizardLgIds(wizardRoot);
69
const ids = {
710
root,
11+
header: `${root}-header`,
12+
wizard: wizardRoot,
13+
...wizardLgIds,
814
} as const;
915
return ids;
1016
};

0 commit comments

Comments
 (0)