Skip to content

Commit 7217eea

Browse files
katiekleinkatiegoines
andauthored
adding unit tests (#7581)
* unit tests * working * fixing tests * add popover test * remove comments * remove comments --------- Co-authored-by: katiegoines <[email protected]>
1 parent 931c68a commit 7217eea

File tree

11 files changed

+419
-27
lines changed

11 files changed

+419
-27
lines changed

jest.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ module.exports = {
1515
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
1616
moduleNameMapper: {
1717
'\\.(css|less|scss)$': '<rootDir>/src/__mocks__/styleMock.js',
18+
'@docsearch/css(.*)': '<rootDir>/src/__mocks__/styleMock.js',
1819
'@/components/(.*)': '<rootDir>/src/components/$1',
1920
'@/constants/(.*)': '<rootDir>/src/constants/$1',
2021
'@/utils/(.*)': '<rootDir>/src/utils/$1',
2122
'@/data/(.*)': '<rootDir>/src/data/$1',
22-
'@/directory/(.*)': '<rootDir>/src/directory/$1'
23+
'@/directory/(.*)': '<rootDir>/src/directory/$1',
24+
'@/themes/(.*)': '<rootDir>/src/themes/$1'
2325
},
2426
transformIgnorePatterns: ['node_modules/(?!variables/.*)']
2527
};

src/components/Accordion/__tests__/Accordion.test.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,39 @@ describe('Accordion', () => {
4646
expect(bodyText).toBeInTheDocument();
4747
});
4848

49-
it('should expand Accordion when heading is clicked', async () => {
49+
it('should toggle Accordion when heading is clicked', async () => {
5050
render(component);
5151
const accordionHeading = screen.getByText('Accordion component example');
52+
const detailsEl = await screen.getByRole('group');
53+
const text = await screen.getByText(content);
5254
userEvent.click(accordionHeading);
5355

5456
await waitFor(() => {
55-
expect(screen.getByText(content)).toBeInTheDocument();
56-
expect(screen.getByText(content)).toBeVisible();
57+
expect(text).toBeVisible();
58+
expect(detailsEl).toHaveAttribute('open');
59+
});
60+
61+
userEvent.click(accordionHeading);
62+
await waitFor(() => {
63+
expect(text).not.toBeVisible();
64+
expect(detailsEl).not.toHaveAttribute('open');
5765
});
5866
});
5967

6068
it('should collapse Accordion when close button is clicked', async () => {
6169
render(component);
62-
await screen.getByText(content);
70+
const accordionHeading = screen.getByText('Accordion component example');
71+
userEvent.click(accordionHeading);
72+
const detailsEl = await screen.getByRole('group');
73+
expect(detailsEl).toHaveAttribute('open');
74+
75+
const text = await screen.getByText(content);
6376
const closeButton = screen.getByRole('button');
6477
userEvent.click(closeButton);
6578

6679
await waitFor(() => {
67-
expect(screen.getByText(content)).not.toBeVisible();
80+
expect(text).not.toBeVisible();
81+
expect(detailsEl).not.toHaveAttribute('open');
6882
});
6983
});
7084

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import * as React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import FeatureFlags from '../index';
4+
import FeatureFlagSummary from '../FeatureFlagSummary';
5+
6+
describe('FeatureFlags', () => {
7+
it('should render the FeatureFlags component', async () => {
8+
render(<FeatureFlags></FeatureFlags>);
9+
const element = await screen.findByRole('heading', { name: 'appSync' });
10+
expect(element).toBeInTheDocument();
11+
});
12+
13+
it('should render the FeatureFlagSummary component', async () => {
14+
const flag = {
15+
description:
16+
'Changes the permission format to grant access to graphql operations instead of appsync control plane operations',
17+
type: 'Feature',
18+
valueType: 'Boolean',
19+
versionAdded: '4.42.0',
20+
deprecationDate: 'May 1st 2021',
21+
values: [
22+
{
23+
value: 'true',
24+
description: 'Creates IAM policies to allow Query/Mutations',
25+
defaultNewProject: true,
26+
defaultExistingProject: false
27+
},
28+
{
29+
value: 'false',
30+
description:
31+
'Uses previous policy format which allows control plane access to AppSync',
32+
defaultNewProject: false,
33+
defaultExistingProject: true
34+
}
35+
]
36+
};
37+
38+
const component = (
39+
<FeatureFlagSummary
40+
key={`feature-flag-summary-${1}`}
41+
name={'generateGraphQLPermissions'}
42+
feature={flag}
43+
/>
44+
);
45+
render(component);
46+
const element = await screen.findByRole('link', {
47+
name: 'generateGraphQLPermissions'
48+
});
49+
expect(element).toBeInTheDocument();
50+
});
51+
52+
it('should render the FeatureFlagSummary component without description if one doesn"t exist on the flag', async () => {
53+
const flag = {
54+
type: 'Feature',
55+
valueType: 'Boolean',
56+
versionAdded: '4.42.0',
57+
deprecationDate: 'May 1st 2021',
58+
values: [
59+
{
60+
value: 'true',
61+
description: 'Creates IAM policies to allow Query/Mutations',
62+
defaultNewProject: true,
63+
defaultExistingProject: false
64+
},
65+
{
66+
value: 'false',
67+
description:
68+
'Uses previous policy format which allows control plane access to AppSync',
69+
defaultNewProject: false,
70+
defaultExistingProject: true
71+
}
72+
]
73+
};
74+
75+
const component = (
76+
<FeatureFlagSummary
77+
key={`feature-flag-summary-${1}`}
78+
name={'generateGraphQLPermissions'}
79+
feature={flag}
80+
/>
81+
);
82+
render(component);
83+
const element = await screen.findByRole('heading', {
84+
name: 'generateGraphQLPermissions'
85+
});
86+
expect(element.nextElementSibling?.tagName).not.toBe('P');
87+
});
88+
});
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import * as React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import { FeatureList, FeatureItem, PlatformFeatureList } from '../index';
4+
5+
const routerMock = {
6+
__esModule: true,
7+
useRouter: () => {
8+
return {
9+
query: { platform: 'react' },
10+
pathname: '/gen1/',
11+
asPath: '/gen1/'
12+
};
13+
}
14+
};
15+
16+
jest.mock('next/router', () => routerMock);
17+
18+
describe('FeatureLists', () => {
19+
const featureListComponent = (
20+
<FeatureList heading="Deploy" level={2}>
21+
<FeatureItem
22+
linkText="SSR/SSG/ISR hosting support"
23+
href="/react/deploy-and-host/hosting/"
24+
>
25+
Deploy apps in Next.js, Nuxt.js, Gatsby, React, Vue, Angular (and more)
26+
by simply connecting your Git repository.
27+
</FeatureItem>
28+
</FeatureList>
29+
);
30+
31+
it('should render the FeatureList component', async () => {
32+
render(featureListComponent);
33+
34+
const heading = await screen.findByRole('heading', { name: 'Deploy' });
35+
36+
expect(heading).toBeInTheDocument();
37+
expect(heading.tagName).toBe('H2');
38+
});
39+
40+
it('should render the FeatureItem component', async () => {
41+
render(featureListComponent);
42+
43+
const link = await screen.findByRole('link', {
44+
name: 'SSR/SSG/ISR hosting support'
45+
});
46+
47+
expect(link).toBeInTheDocument();
48+
});
49+
50+
it('should render the PlatformFeatureList component', async () => {
51+
render(<PlatformFeatureList platform="react" />);
52+
53+
const link = await screen.findByRole('link', {
54+
name: 'Simple configuration'
55+
});
56+
57+
const heading = await screen.findByRole('heading', {
58+
name: 'Features for React'
59+
});
60+
61+
expect(link).toBeInTheDocument();
62+
expect(heading).toBeInTheDocument();
63+
});
64+
});

src/components/Feedback/__tests__/index.test.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { render, screen, waitFor } from '@testing-library/react';
33
import userEvent from '@testing-library/user-event';
44
import * as trackModule from '../../../utils/track';
55

6-
jest.mock('next/router', () => ({
6+
const router = jest.mock('next/router', () => ({
77
useRouter() {
88
return {
99
route: '/',
@@ -19,6 +19,8 @@ jest.mock('../../../utils/track', () => ({
1919
}));
2020

2121
describe('Feedback', () => {
22+
const component = <Feedback router={router} />;
23+
2224
it('should render component with thumbs up and thumbs down button', () => {
2325
const component = <Feedback />;
2426

@@ -32,27 +34,22 @@ describe('Feedback', () => {
3234
});
3335

3436
it('should show response text when No is clicked', async () => {
35-
const component = <Feedback />;
36-
3737
render(component);
3838

39-
const thumbsDownButton = screen.getByText('No');
40-
const feedbackComponent = screen.getByText('Was this page helpful?');
41-
const feedbackText = screen.getByText('Can you provide more details?');
39+
const thumbsDownButton = screen.getByRole('button', { name: 'No' });
4240

4341
expect(thumbsDownButton).toBeInTheDocument();
4442

45-
userEvent.click(feedbackComponent);
43+
userEvent.click(thumbsDownButton);
44+
const response = screen.getByRole('link');
4645

4746
await waitFor(() => {
48-
expect(feedbackText).toBeVisible();
47+
expect(response.textContent).toBe('File an issue on GitHub');
4948
});
5049
});
5150

5251
it('should call trackFeedbackSubmission request when either button is clicked', async () => {
5352
jest.spyOn(trackModule, 'trackFeedbackSubmission');
54-
const component = <Feedback />;
55-
5653
render(component);
5754
const thumbsDownButton = screen.getByText('No');
5855
userEvent.click(thumbsDownButton);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import { FrameworkGrid } from '../index';
4+
5+
const routerMock = {
6+
__esModule: true,
7+
useRouter: () => {
8+
return {
9+
query: { platform: 'react' },
10+
pathname: '/gen1/',
11+
asPath: '/gen1/'
12+
};
13+
}
14+
};
15+
16+
jest.mock('next/router', () => routerMock);
17+
18+
describe('FrameworkGrid', () => {
19+
const component = <FrameworkGrid currentKey="react" />;
20+
21+
it('should render the FrameworkGrid component', async () => {
22+
render(component);
23+
const framework = await screen.findByRole('link', { name: 'React' });
24+
expect(framework).toBeInTheDocument();
25+
});
26+
27+
it('should highlight icon of the currentKey', async () => {
28+
render(component);
29+
const framework = await screen.findByRole('link', { name: 'React' });
30+
expect(framework.classList).toContain('framework-grid__link--current');
31+
});
32+
});

0 commit comments

Comments
 (0)