-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(pci-instances): add test suites for components
ref:TAPC-1928 Signed-off-by: Frédéric Vilcot <[email protected]>
- Loading branch information
1 parent
b80e11f
commit 8b98cbb
Showing
9 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
packages/manager/apps/pci-instances/src/__mocks__/instance/constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { TInstance } from '@/data/hooks/instance/useInstances'; | ||
|
||
export const mockedInstance: TInstance = { | ||
id: '12345', | ||
name: 'foo', | ||
flavorId: '678910', | ||
flavorName: 'b2-8', | ||
imageId: '11121314', | ||
imageName: 'linux', | ||
region: 'BHS', | ||
status: { severity: 'success', state: 'ACTIVE' }, | ||
addresses: new Map([ | ||
['private', [{ ip: '123.000.00', version: 1, gatewayIp: '' }]], | ||
['public', [{ ip: '777.000.00', version: 2, gatewayIp: '' }]], | ||
]), | ||
}; | ||
|
||
export const mockedInstanceWithEmptyRegion: TInstance = { | ||
...mockedInstance, | ||
region: '', | ||
}; |
29 changes: 29 additions & 0 deletions
29
packages/manager/apps/pci-instances/src/components/datagrid/cell/LoadingCell.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import { describe, test } from 'vitest'; | ||
import { LoadingCell } from './LoadingCell.component'; | ||
|
||
describe('Considering the loading cell component', () => { | ||
test.each` | ||
isLoading | ||
${false} | ||
${true} | ||
`( | ||
'Should render component correctly depending on isLoading property <$isLoading>', | ||
({ isLoading }) => { | ||
render( | ||
<LoadingCell isLoading={isLoading}> | ||
<div>Foo</div> | ||
</LoadingCell>, | ||
); | ||
const loadingCellElement = screen.getByTestId('loading-cell'); | ||
expect(loadingCellElement).toBeInTheDocument(); | ||
if (isLoading) { | ||
const skeletonElement = screen.getByTestId('skeleton'); | ||
expect(skeletonElement).toBeTruthy(); | ||
} else { | ||
const childrenElement = screen.getByText('Foo'); | ||
expect(childrenElement).toBeTruthy(); | ||
} | ||
}, | ||
); | ||
}); |
23 changes: 23 additions & 0 deletions
23
packages/manager/apps/pci-instances/src/components/datagrid/cell/TextCell.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import { describe } from 'vitest'; | ||
import { BaseTextCell, TextCell } from './TextCell.component'; | ||
|
||
describe('Considering the text cell components', () => { | ||
test('Should render BaseTextCell component correctly with children', () => { | ||
render( | ||
<BaseTextCell> | ||
<div>Foo</div> | ||
</BaseTextCell>, | ||
); | ||
const baseTextCellElement = screen.getByTestId('base-text-cell'); | ||
expect(baseTextCellElement).toBeInTheDocument(); | ||
const childElement = screen.getByText('Foo'); | ||
expect(childElement).toBeInTheDocument(); | ||
}); | ||
|
||
test('Should render TextCell component correctly with label', () => { | ||
render(<TextCell isLoading={false} label="foo" />); | ||
const textCellElement = screen.getByText('foo'); | ||
expect(textCellElement).toBeInTheDocument(); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
packages/manager/apps/pci-instances/src/components/menu/ActionsMenu.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { describe, test, vi } from 'vitest'; | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import { ActionsMenu, TActionsMenuItem } from './ActionsMenu.component'; | ||
|
||
const onMenuItemClickMock = vi.fn(); | ||
|
||
const testItems: TActionsMenuItem[] = [ | ||
{ | ||
label: 'foo', | ||
href: '/foo/bar', | ||
}, | ||
{ | ||
label: 'bar', | ||
onMenuItemClick: onMenuItemClickMock, | ||
}, | ||
]; | ||
|
||
const renderActionsMenu = (items: TActionsMenuItem[]) => { | ||
render(<ActionsMenu items={items} />); | ||
}; | ||
|
||
describe('Considering the ActionsMenu component', () => { | ||
test('Should render only action menu button with Icon as first child if items prop is []', () => { | ||
renderActionsMenu([]); | ||
const actionsMenuButtonElement = screen.getByTestId('actions-menu-button'); | ||
expect(actionsMenuButtonElement).toBeInTheDocument(); | ||
const childElements = actionsMenuButtonElement.querySelectorAll('*'); | ||
expect(childElements.length).toBe(1); | ||
}); | ||
|
||
test('Should render the list of items correctly if provided', () => { | ||
renderActionsMenu(testItems); | ||
const menuItemElements = screen.getAllByTestId('actions-menu-item'); | ||
expect(menuItemElements.length).toBe(2); | ||
const [firstChild, secondChild] = menuItemElements; | ||
expect(firstChild).toHaveTextContent('foo'); | ||
expect(firstChild).toHaveAttribute('href', '/foo/bar'); | ||
expect(secondChild).toHaveTextContent('bar'); | ||
fireEvent.click(secondChild); | ||
expect(onMenuItemClickMock).toHaveBeenCalled(); | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
packages/manager/apps/pci-instances/src/components/navigation/GoBack.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { describe, test, vi } from 'vitest'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { ODS_ICON_NAME } from '@ovhcloud/ods-components'; | ||
import { GoBack } from './GoBack.component'; | ||
|
||
const backHref = '..'; | ||
|
||
vi.mock('react-router-dom', () => ({ | ||
useHref: () => backHref, | ||
})); | ||
|
||
const getGoBackElement = () => { | ||
render(<GoBack />); | ||
return screen.getByText('pci_instances_common_go_back'); | ||
}; | ||
|
||
describe('Considering the GoBack component', () => { | ||
test('Should be rendered with correct class and href attribute', () => { | ||
const goBackElement = getGoBackElement(); | ||
expect(goBackElement).toBeInTheDocument(); | ||
expect(goBackElement).toHaveClass('mt-12 mb-3'); | ||
expect(goBackElement).toHaveAttribute('href', backHref); | ||
}); | ||
|
||
test('Should contain icon as first child element', () => { | ||
const goBackElement = getGoBackElement(); | ||
expect(goBackElement.firstChild).toBeTruthy(); | ||
expect(goBackElement.firstChild).toHaveAttribute( | ||
'name', | ||
ODS_ICON_NAME.ARROW_LEFT, | ||
); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
packages/manager/apps/pci-instances/src/pages/instances/datagrid/cell/ActionsCell.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import { describe, vi } from 'vitest'; | ||
import { ActionsCell } from './ActionsCell.component'; | ||
import { mockedInstance } from '@/__mocks__/instance/constants'; | ||
|
||
vi.mock('react-router-dom', () => ({ | ||
useHref: () => mockedInstance.id, | ||
})); | ||
|
||
describe('Considering the ActionsCell component', () => { | ||
test('Should render component correctly', () => { | ||
render(<ActionsCell instance={mockedInstance} isLoading={false} />); | ||
const actionsMenuElement = screen.getByTestId('actions-menu-button'); | ||
expect(actionsMenuElement).toBeInTheDocument(); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
packages/manager/apps/pci-instances/src/pages/instances/datagrid/cell/AddressesCell.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import { describe, test } from 'vitest'; | ||
import { AddressesCell } from './AddressesCell.component'; | ||
import { TAddress } from '@/data/hooks/instance/useInstances'; | ||
|
||
const addresses: TAddress[] = [ | ||
{ | ||
ip: '123456', | ||
version: 1, | ||
gatewayIp: '', | ||
}, | ||
{ | ||
ip: '78910', | ||
version: 2, | ||
gatewayIp: '', | ||
}, | ||
]; | ||
|
||
describe('Considering the AddressesCell component', () => { | ||
test('Should render the component with given addresses', () => { | ||
render(<AddressesCell isLoading={false} addresses={addresses} />); | ||
addresses.forEach((address) => { | ||
expect(screen.getByText(address.ip)).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
packages/manager/apps/pci-instances/src/pages/instances/datagrid/cell/NameIdCell.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import { describe, test, vi } from 'vitest'; | ||
import { NameIdCell } from './NameIdCell.component'; | ||
import { mockedInstance } from '@/__mocks__/instance/constants'; | ||
|
||
vi.mock('react-router-dom', () => ({ | ||
useHref: () => mockedInstance.id, | ||
})); | ||
|
||
describe('Considering the NameIdCell component', () => { | ||
test('Should render component with correct href and labels', () => { | ||
render(<NameIdCell instance={mockedInstance} isLoading={false} />); | ||
const nameElement = screen.getByText(mockedInstance.name); | ||
expect(nameElement).toBeInTheDocument(); | ||
expect(nameElement).toHaveAttribute('href', mockedInstance.id); | ||
const idElement = screen.getByText(mockedInstance.id); | ||
expect(idElement).toBeInTheDocument(); | ||
}); | ||
}); |
12 changes: 12 additions & 0 deletions
12
packages/manager/apps/pci-instances/src/pages/instances/datagrid/cell/StatusCell.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import { describe, test } from 'vitest'; | ||
import { mockedInstance } from '@/__mocks__/instance/constants'; | ||
import { StatusCell } from './StatusCell.component'; | ||
|
||
describe('Considering the StatusCell component', () => { | ||
test('Should render component correctly', () => { | ||
render(<StatusCell instance={mockedInstance} isLoading={false} />); | ||
const statusCellElement = screen.getByTestId('status-chip'); | ||
expect(statusCellElement).toBeInTheDocument(); | ||
}); | ||
}); |