-
Notifications
You must be signed in to change notification settings - Fork 15
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
Sabina Talipova
committed
Feb 13, 2024
1 parent
9340c28
commit 713e85c
Showing
5 changed files
with
321 additions
and
130 deletions.
There are no files selected for viewing
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
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
76 changes: 76 additions & 0 deletions
76
client/src/components/LinkPicker/tests/LinkPickerMenu-test.js
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,76 @@ | ||
/* global jest, test */ | ||
|
||
import React from 'react'; | ||
import { render, fireEvent, act } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
import { LinkFieldContext } from 'components/LinkField/LinkField'; | ||
import LinkPickerMenu from '../LinkPickerMenu'; | ||
|
||
function makeProps(obj = {}) { | ||
return { | ||
types: [ | ||
{ key: 'sitetree', title: 'Page', icon: 'font-icon-page', allowed: true }, | ||
{ key: 'external', title: 'External URL', icon: 'font-icon-link', allowed: true }, | ||
{ key: 'email', title: 'Email', icon: 'font-icon-email', allowed: true }, | ||
{ key: 'phone', title: 'Phone', icon: 'font-icon-phone', allowed: true }, | ||
], | ||
onSelect: jest.fn(), | ||
onKeyDownEdit: jest.fn(), | ||
...obj | ||
}; | ||
} | ||
describe('Render LinkPickerMenu element', () => { | ||
test('LinkPickerMenu render() should display link list', () => { | ||
const { container } = render(<LinkFieldContext.Provider value={{ loading: false }}> | ||
<LinkPickerMenu {...makeProps()} /> | ||
</LinkFieldContext.Provider>); | ||
expect(container.querySelectorAll('.dropdown-item')).toHaveLength(4); | ||
expect(container.querySelectorAll('.dropdown-item')[0]).toHaveTextContent('Page'); | ||
expect(container.querySelectorAll('.dropdown-item')[1]).toHaveTextContent('External URL'); | ||
expect(container.querySelectorAll('.dropdown-item')[2]).toHaveTextContent('Email'); | ||
expect(container.querySelectorAll('.dropdown-item')[3]).toHaveTextContent('Phone'); | ||
}); | ||
|
||
test('LinkPickerMenu render() should display link list with allowed SiteTreeLink and EmailLink', () => { | ||
const { container } = render(<LinkFieldContext.Provider value={{ loading: false }}> | ||
<LinkPickerMenu {...makeProps( | ||
{ types: [ | ||
{ key: 'sitetree', title: 'Page', icon: 'font-icon-page', allowed: true }, | ||
{ key: 'email', title: 'Email', icon: 'font-icon-email', allowed: true }, | ||
{ key: 'phone', title: 'Phone', icon: 'font-icon-phone', allowed: false }, | ||
{ key: 'external', title: 'External URL', icon: 'font-icon-link', allowed: false }, | ||
] })} | ||
/> | ||
</LinkFieldContext.Provider>); | ||
expect(container.querySelectorAll('.dropdown-item')).toHaveLength(2); | ||
expect(container.querySelectorAll('.dropdown-item')[0]).toHaveTextContent('Page'); | ||
expect(container.querySelectorAll('.dropdown-item')[0].firstChild).toHaveClass('font-icon-page'); | ||
expect(container.querySelectorAll('.dropdown-item')[1]).toHaveTextContent('Email'); | ||
expect(container.querySelectorAll('.dropdown-item')[1].firstChild).toHaveClass('font-icon-email'); | ||
}); | ||
}); | ||
|
||
describe('Handle user event on LinkPickerMenu element', () => { | ||
test('LinkPickerMenu onSelect() should call onSelect with selected type', async () => { | ||
const onSelect = jest.fn(); | ||
const { container } = render(<LinkFieldContext.Provider value={{ loading: false }}> | ||
<LinkPickerMenu {...makeProps({ onSelect })} /> | ||
</LinkFieldContext.Provider>); | ||
await act(async () => { | ||
await fireEvent.click(container.querySelectorAll('.dropdown-item')[1]); | ||
}); | ||
expect(onSelect).toHaveBeenCalledTimes(1); | ||
expect(onSelect).toHaveBeenCalledWith('external'); | ||
}); | ||
|
||
test('LinkPickerMenu onKeyDownEdit() should call onKeyDownEdit with selected type', async () => { | ||
const onKeyDownEdit = jest.fn(); | ||
const { container } = render(<LinkFieldContext.Provider value={{ loading: false }}> | ||
<LinkPickerMenu {...makeProps({ onKeyDownEdit })} /> | ||
</LinkFieldContext.Provider>); | ||
await act(async () => { | ||
await fireEvent.keyDown(container.querySelector('.dropdown-item'), { key: 'Enter', code: 'Enter', charCode: 13 }); | ||
}); | ||
expect(onKeyDownEdit).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
Oops, something went wrong.