Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LG-4799: Code test harnesses #2697

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
c31994b
wip
shaneeza Feb 5, 2025
36b4fa2
Merge branch 'main' of github.com:mongodb/leafygreen-ui into LG-4799-…
shaneeza Feb 5, 2025
a03ffd7
add utils
shaneeza Feb 5, 2025
e8d28ad
Merge branch 'LG-4657-code-improvements-integration' of github.com:mo…
shaneeza Feb 5, 2025
f6e545e
update spec
shaneeza Feb 5, 2025
1bf3fe2
some cleanup
shaneeza Feb 5, 2025
b19d930
tests
shaneeza Feb 5, 2025
c992b7c
fixes
shaneeza Feb 5, 2025
7489de3
update README
shaneeza Feb 5, 2025
f9102a9
add changetset and export utils
shaneeza Feb 6, 2025
a24e573
the correct path
shaneeza Feb 6, 2025
846c378
readme typo
shaneeza Feb 6, 2025
1a6006c
update README
shaneeza Feb 6, 2025
44bb5a1
getTitle
shaneeza Feb 6, 2025
b6672f3
update current changeset
shaneeza Feb 6, 2025
1265927
export
shaneeza Feb 6, 2025
c92ea8a
comment
shaneeza Feb 6, 2025
c543f78
Merge branch 'LG-4657-code-improvements-integration' of github.com:mo…
shaneeza Feb 12, 2025
f0c8e14
minor feedback
shaneeza Feb 12, 2025
f222f33
use select test utils for language switcher
shaneeza Feb 12, 2025
15b50e2
remove null from getInput return type
shaneeza Feb 12, 2025
eacaab5
remove isExpanded from expand button
shaneeza Feb 12, 2025
dbbece5
add utils
shaneeza Feb 12, 2025
9d028b3
lint
shaneeza Feb 12, 2025
0fddd83
LGIDs to LGIDS
shaneeza Feb 12, 2025
e9b23b2
LGIDs
shaneeza Feb 12, 2025
85dbb21
unused vars
shaneeza Feb 13, 2025
9fc615f
select types
shaneeza Feb 13, 2025
7697390
rename styles
shaneeza Feb 13, 2025
d84af07
use button test utils
shaneeza Feb 13, 2025
54f3d66
lint
shaneeza Feb 13, 2025
e403731
update readme
shaneeza Feb 13, 2025
af580f7
more readme updates
shaneeza Feb 13, 2025
6bd672c
add getLgIds
shaneeza Feb 14, 2025
36c3c5a
update readme, lint
shaneeza Feb 14, 2025
8683c9f
add another test
shaneeza Feb 14, 2025
80cffd5
update tests
shaneeza Feb 14, 2025
455462d
fix failing tests
shaneeza Feb 14, 2025
4b2d424
select changeset
shaneeza Feb 14, 2025
4e7bc78
button changeset
shaneeza Feb 14, 2025
dacfb93
merge conflict
shaneeza Feb 18, 2025
d453d46
spelling
shaneeza Feb 19, 2025
a933a2c
feedback - button readme
shaneeza Feb 19, 2025
6047849
remove comment
shaneeza Feb 19, 2025
97271a5
merge conflict
shaneeza Feb 20, 2025
d722aac
lint
shaneeza Feb 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chatty-ears-exercise.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ e.g.
</Code>
```

### Test Harnesses
Exports `getTestUtils`, a util to reliably interact with LG `Code` in a product test suite. For more details, check out the [README](https://github.com/mongodb/leafygreen-ui/tree/main/packages/code#test-harnesses) [LG-4799](https://jira.mongodb.org/browse/LG-4799)

Exports `getLgIds`, a util to instantiate an object of `data-lgid` values for a given LG `Code` component instance.

### `baseFontSize`
Adds `baseFontSize` prop, which allows you to override the `LeafyGreenProvider` value.

Expand Down
5 changes: 5 additions & 0 deletions .changeset/soft-experts-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@leafygreen-ui/select': minor
---

Exports `type GetTestUtilsReturnType`.
5 changes: 5 additions & 0 deletions .changeset/tame-timers-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@leafygreen-ui/button': minor
---

Exports `type GetTestUtilsReturnType`.
2 changes: 1 addition & 1 deletion packages/button/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { Button as default } from './Button';
export type { BaseButtonProps, ButtonProps } from './types';
export { FontSize, Size, Variant } from './types';
export { getTestUtils } from './utils';
export { getTestUtils, type GetTestUtilsReturnType } from './utils';
1 change: 1 addition & 0 deletions packages/button/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { getTestUtils } from './getTestUtils';
export type { GetTestUtilsReturnType } from './getTestUtils.types';
138 changes: 138 additions & 0 deletions packages/code/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,141 @@ interface LanguageOption {
image?: React.ReactElement;
}
```

# Test Harnesses

## getTestUtils()

`getTestUtils()` is a util that allows consumers to reliably interact with LG `Code` in a product test suite. If the `Code` component cannot be found, an error will be thrown.

### Usage

```tsx
import { getTestUtils } from '@leafygreen-ui/code';

const utils = getTestUtils(lgId?: `lg-${string}`); // lgId refers to the custom `data-lgid` attribute passed to `Code`. It defaults to 'lg-code' if left empty.
```

#### Single `Code` component

```tsx
import { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import Code, { getTestUtils } from '@leafygreen-ui/code';

...

test('code', () => {
render(
<Code
language="javascript"
panel={
<Panel
onChange={() => {}}
languageOptions={languageOptions}
title="Title"
/>
}
>
{codeSnippet}
</Code>
);

const { getLanguage, getLanguageSwitcherUtils, getIsLoading, getCopyButtonUtils, getExpandButton } = getTestUtils();
const { getInput, getOptions, getOptionByValue, getInputValue, isDisabled: isLanguageSwitcherDisabled } = getLanguageSwitcherUtils();
const { getButton: getCopyButtonUtils, isDisabled: isCopyButtonDisabled } = getCopyButtonUtils();

expect(getLanguage()).toBe('javascript');
expect(getTitle()).toBe('Title');
expect(getInput()).toBeInTheDocument();
expect(getOptions()).toHaveLength(2);
expect(getOptionByValue('js')).toBeInTheDocument();
expect(getInputValue()).toBe('javascript');
expect(isLanguageSwitcherDisabled()).toBe(false);
expect(getIsLoading()).toBe(false);
expect(getCopyButtonUtils()).toBeInTheDocument();
expect(isCopyButtonDisabled()).toBe(false);
expect(getExpandButton()).toBeInTheDocument();
expect(isExpanded()).toBe(false);
});
```

#### Multiple `Code` components

When testing multiple `Code` components, it is recommended to add the custom `data-lgid` attribute to each `Code`.

```tsx
import { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Code, getTestUtils } from '@leafygreen-ui/code';

...

test('code', () => {
render(
<>
<Code
language="javascript"
panel={<Panel/>}
data-lgid="lg-code-1"
>
{codeSnippet}
</Code>
<Code
language="python"
panel={<Panel/>}
data-lgid="lg-code-2"
>
{codeSnippet}
</Code>
</>
);

const testUtils1 = getTestUtils('lg-code-1');
const testUtils2 = getTestUtils('lg-code-2');

// First Code
expect(testUtils1.getLanguage()).toBe('javascript');

// Second Code
expect(testUtils2.getLanguage()).toBe('python');
});
```

### Test Utils

```tsx
const {
getLanguage,
getTitle,
getLanguageSwitcherUtils: {
getInput,
getOptions,
getOptionByValue,
isDisabled,
},
getIsLoading,
getCopyButtonUtils: { getButton, isDisabled },
getExpandButton,,
} = getTestUtils();
```

| Util | Description | Returns |
| ---------------------------- | -------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
| `getLanguage()` | Returns the current language of the code block | `string` |
| `getTitle()` | Returns the title of the code block | `string` \| `null` |
| `getLanguageSwitcherUtils()` | Returns utils for interacting with the language switcher | `LanguageSwitcherUtils` |
| `getIsLoading()` | Returns whether the code block is in loading state | `boolean` |
| `getCopyButtonUtils()` | Returns utils for interacting with the copy button | [Button test utils return type](https://github.com/mongodb/leafygreen-ui/blob/main/packages/button/README.md#test-utils) |
| `getExpandButton()` | Returns the expand button | `HTMLButtonElement` |
| `getIsExpanded()` | Returns whether the code block is expanded | `boolean` |

### LanguageSwitcherUtils

| Util | Description | Returns |
| --------------------------------- | ------------------------------------------------- | ----------------------- |
| `getInput()` | Returns the language switcher trigger | `HTMLButtonElement` |
| `getInputValue()` | Returns the language switcher input value | `string` |
| `getOptions()` | Returns all options in the language switcher | `Array<HTMLElement>` |
| `getOptionByValue(value: string)` | Returns the option element by its value | `HTMLElement` \| `null` |
| `isDisabled()` | Returns whether the language switcher is disabled | `boolean` |
1 change: 1 addition & 0 deletions packages/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@leafygreen-ui/tokens": "workspace:^",
"@leafygreen-ui/tooltip": "workspace:^",
"@leafygreen-ui/typography": "workspace:^",
"@lg-tools/test-harnesses": "workspace:^",
"@types/facepaint": "^1.2.1",
"@types/highlight.js": "^10.1.0",
"clipboard": "^2.0.6",
Expand Down
116 changes: 116 additions & 0 deletions packages/code/src/Code.testutils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import React from 'react';
import { render, RenderResult } from '@testing-library/react';

import Code from './Code/Code';
import { CodeProps } from './Code/Code.types';
import { PanelProps } from './Panel/Panel.types';
import { Panel } from './Panel';
import { Language } from './types';
import { getTestUtils, TestUtilsReturnType } from './utils';

const codeSnippet = `
import datetime from './';

const myVar = 42;

var myObj = {
someProp: ['arr', 'ay'],
regex: /([A-Z])w+/
}

export default class myClass {
constructor(){
// access properties
this.myProp = false
}
}

function greeting(entity) {
return \`Hello, \${entity}! Cras justo odio, dapibus ac facilisis in, egestas eget quam. Vestibulum id ligula porta felis euismod semper.\`;
}

console.log(greeting('World'));`;

export const languageOptions = [
{
displayName: 'JavaScript',
language: Language.JavaScript,
},
{
displayName: 'Python',
language: Language.Python,
},
];

export const renderCode = (
props: Partial<CodeProps> = {},
): RenderResult & TestUtilsReturnType => {
const renderResults = render(
<Code language={languageOptions[0].language} {...props}>
{codeSnippet}
</Code>,
);

const testUtils = getTestUtils();

return {
...renderResults,
...testUtils,
};
};

export const renderCodeWithLanguageSwitcher = ({
props = {},
isLoading = false,
}: {
props?: Partial<PanelProps>;
isLoading?: boolean;
}): RenderResult & TestUtilsReturnType => {
const renderResults = render(
<Code
isLoading={isLoading}
language={languageOptions[0].displayName}
panel={
<Panel
onChange={() => {}}
{...props}
languageOptions={languageOptions}
/>
}
>
{codeSnippet}
</Code>,
);

const testUtils = getTestUtils();

return {
...renderResults,
...testUtils,
};
};

export const renderMultipleCodes = (): RenderResult => {
const renderResults = render(
<>
<Code
data-lgid="lg-code-1"
language={languageOptions[0].displayName}
panel={<Panel onChange={() => {}} languageOptions={languageOptions} />}
>
{codeSnippet}
</Code>
<Code
data-lgid="lg-code-2"
language={languageOptions[1].displayName}
panel={<Panel onChange={() => {}} languageOptions={languageOptions} />}
>
{codeSnippet}
</Code>
</>,
);

return {
...renderResults,
};
};
Loading