UserDropdown.spec.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. Copyright (C) 2021 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React from 'react'
  15. import { render } from '@testing-library/react'
  16. import TestUtils from '@tests/TestUtils'
  17. import { User } from '@src/@types/User'
  18. import UserDropdown from '.'
  19. jest.mock('react-router-dom', () => ({ Link: 'div' }))
  20. const USER: User = {
  21. id: 'user-id',
  22. name: 'User Name',
  23. email: 'email@email.test',
  24. project: {
  25. id: 'project-id',
  26. name: 'Project Name',
  27. },
  28. }
  29. describe('UserDropdown', () => {
  30. it('renders no user', () => {
  31. render(<UserDropdown user={null} onItemClick={() => { }} />)
  32. TestUtils.select('UserDropdown__Icon')?.click()
  33. expect(TestUtils.select('UserDropdown__Label')?.textContent).toBe('No signed in user')
  34. })
  35. it('renders user menu', () => {
  36. render(<UserDropdown user={USER} onItemClick={() => { }} />)
  37. TestUtils.select('UserDropdown__Icon')?.click()
  38. expect(TestUtils.select('UserDropdown__Username')?.textContent).toBe(USER.name)
  39. expect(TestUtils.select('UserDropdown__Email')?.textContent).toBe(USER.email)
  40. const listItems = TestUtils.selectAll('UserDropdown__ListItem')
  41. expect(listItems).toHaveLength(3)
  42. expect(listItems[0].textContent).toBe('About Coriolis')
  43. })
  44. it('fires item click', () => {
  45. const onItemClick = jest.fn()
  46. render(<UserDropdown user={USER} onItemClick={onItemClick} />)
  47. TestUtils.select('UserDropdown__Icon')?.click()
  48. TestUtils.selectAll('UserDropdown__Label')[2].click()
  49. expect(onItemClick).toHaveBeenCalledWith(expect.objectContaining({ value: 'signout' }))
  50. })
  51. })