-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bcec6e5
commit 4b63a08
Showing
17 changed files
with
1,273 additions
and
117 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
101 changes: 101 additions & 0 deletions
101
apps/sap-commerce-cloud/frontend/src/AppConfig/FieldSelector.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,101 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import FieldSelector from './FieldSelector'; | ||
import { vi, describe, it, expect } from 'vitest'; | ||
|
||
const contentTypes = [ | ||
{ | ||
sys: { id: 'ct1' }, | ||
name: 'Content Type 1', | ||
}, | ||
{ | ||
sys: { id: 'ct2' }, | ||
name: 'Content Type 2', | ||
}, | ||
]; | ||
|
||
const compatibleFields = { | ||
ct1: [ | ||
{ id: 'field1', name: 'Field 1', type: 'Symbol' }, | ||
{ id: 'field2', name: 'Field 2', type: 'Symbol' }, | ||
], | ||
ct2: [ | ||
{ id: 'field3', name: 'Field 3', type: 'Symbol' }, | ||
{ id: 'field4', name: 'Field 4', type: 'Symbol' }, | ||
], | ||
}; | ||
|
||
const selectedFields = { | ||
ct1: ['field1'], | ||
ct2: [], | ||
}; | ||
|
||
describe('FieldSelector', () => { | ||
it('renders content types and fields correctly', () => { | ||
render( | ||
<FieldSelector | ||
contentTypes={contentTypes} | ||
compatibleFields={compatibleFields} | ||
selectedFields={selectedFields} | ||
onSelectedFieldsChange={vi.fn()} | ||
/> | ||
); | ||
|
||
expect(screen.getByText('Content Type 1')).toBeInTheDocument(); | ||
expect(screen.getByText('Content Type 2')).toBeInTheDocument(); | ||
expect(screen.getByText('Field 1')).toBeInTheDocument(); | ||
expect(screen.getByText('Field 2')).toBeInTheDocument(); | ||
expect(screen.getByText('Field 3')).toBeInTheDocument(); | ||
expect(screen.getByText('Field 4')).toBeInTheDocument(); | ||
|
||
const field1Checkbox = screen.getByLabelText('Field 1'); | ||
const field2Checkbox = screen.getByLabelText('Field 2'); | ||
|
||
expect(field1Checkbox).toBeChecked(); | ||
expect(field2Checkbox).not.toBeChecked(); | ||
}); | ||
|
||
it('calls onSelectedFieldsChange with correct arguments when a checkbox is checked', () => { | ||
const onSelectedFieldsChangeMock = vi.fn(); | ||
|
||
render( | ||
<FieldSelector | ||
contentTypes={contentTypes} | ||
compatibleFields={compatibleFields} | ||
selectedFields={selectedFields} | ||
onSelectedFieldsChange={onSelectedFieldsChangeMock} | ||
/> | ||
); | ||
|
||
const field2Checkbox = screen.getByLabelText('Field 2'); | ||
fireEvent.click(field2Checkbox); | ||
|
||
expect(onSelectedFieldsChangeMock).toHaveBeenCalledWith({ | ||
...selectedFields, | ||
ct1: ['field1', 'field2'], | ||
}); | ||
}); | ||
|
||
it('calls onSelectedFieldsChange with correct arguments when a checkbox is unchecked', () => { | ||
const onSelectedFieldsChangeMock = vi.fn(); | ||
|
||
render( | ||
<FieldSelector | ||
contentTypes={contentTypes} | ||
compatibleFields={compatibleFields} | ||
selectedFields={{ | ||
ct1: ['field1', 'field2'], | ||
ct2: [], | ||
}} | ||
onSelectedFieldsChange={onSelectedFieldsChangeMock} | ||
/> | ||
); | ||
|
||
const field2Checkbox = screen.getByLabelText('Field 2'); | ||
fireEvent.click(field2Checkbox); | ||
|
||
expect(onSelectedFieldsChangeMock).toHaveBeenCalledWith({ | ||
...selectedFields, | ||
ct1: ['field1'], | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.