new-project-button.test.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { fireEvent, screen } from '@testing-library/react'
  2. import { expect } from 'chai'
  3. import fetchMock from 'fetch-mock'
  4. import NewProjectButton from '../../../../../frontend/js/features/project-list/components/new-project-button'
  5. import { renderWithProjectListContext } from '../helpers/render-with-context'
  6. import getMeta from '@/utils/meta'
  7. import * as bootstrapUtils from '@/features/utils/bootstrap-5'
  8. import sinon, { type SinonStub } from 'sinon'
  9. describe('<NewProjectButton />', function () {
  10. let isBootstrap5Stub: SinonStub
  11. before(function () {
  12. isBootstrap5Stub = sinon.stub(bootstrapUtils, 'isBootstrap5').returns(true)
  13. })
  14. after(function () {
  15. isBootstrap5Stub.restore()
  16. })
  17. beforeEach(function () {
  18. fetchMock.reset()
  19. })
  20. describe('for every user (affiliated and non-affiliated)', function () {
  21. beforeEach(function () {
  22. Object.assign(getMeta('ol-ExposedSettings'), {
  23. templateLinks: [
  24. {
  25. name: 'Journal articles',
  26. url: '/gallery/tagged/academic-journal',
  27. },
  28. {
  29. name: 'View All',
  30. url: '/latex/templates',
  31. },
  32. ],
  33. })
  34. renderWithProjectListContext(<NewProjectButton id="test" />)
  35. const newProjectButton = screen.getByRole('button', {
  36. name: 'New Project',
  37. })
  38. fireEvent.click(newProjectButton)
  39. })
  40. it('shows the correct dropdown menu', function () {
  41. // static menu
  42. screen.getByText('Blank Project')
  43. screen.getByText('Example Project')
  44. screen.getByText('Upload Project')
  45. screen.getByText('Import from GitHub')
  46. // static text
  47. screen.getByText('Templates')
  48. // dynamic menu based on templateLinks
  49. screen.getByText('Journal articles')
  50. screen.getByText('View All')
  51. })
  52. it('open new project modal when clicking at Blank Project', function () {
  53. fireEvent.click(screen.getByRole('menuitem', { name: 'Blank Project' }))
  54. screen.getByPlaceholderText('Project Name')
  55. })
  56. it('open new project modal when clicking at Example Project', function () {
  57. fireEvent.click(screen.getByRole('menuitem', { name: 'Example Project' }))
  58. screen.getByPlaceholderText('Project Name')
  59. })
  60. it('close the new project modal when clicking at the top right "x" button', function () {
  61. fireEvent.click(screen.getByRole('menuitem', { name: 'Blank Project' }))
  62. fireEvent.click(screen.getByRole('button', { name: 'Close' }))
  63. expect(screen.queryByRole('dialog')).to.be.null
  64. })
  65. it('close the new project modal when clicking at the Cancel button', function () {
  66. fireEvent.click(screen.getByRole('menuitem', { name: 'Blank Project' }))
  67. fireEvent.click(screen.getByRole('button', { name: 'Cancel' }))
  68. expect(screen.queryByRole('dialog')).to.be.null
  69. })
  70. })
  71. describe('for affiliated user with custom templates', function () {
  72. beforeEach(function () {
  73. Object.assign(getMeta('ol-ExposedSettings'), {
  74. templateLinks: [
  75. {
  76. name: 'Journal articles',
  77. url: '/gallery/tagged/academic-journal',
  78. },
  79. {
  80. name: 'View All',
  81. url: '/latex/templates',
  82. },
  83. ],
  84. })
  85. window.metaAttributesCache.set('ol-portalTemplates', [
  86. {
  87. name: 'Affiliation 1',
  88. url: '/edu/test-new-template',
  89. },
  90. ])
  91. })
  92. it('shows the correct dropdown menu', function () {
  93. renderWithProjectListContext(<NewProjectButton id="test" />)
  94. const newProjectButton = screen.getByRole('button', {
  95. name: 'New Project',
  96. })
  97. fireEvent.click(newProjectButton)
  98. // static menu
  99. screen.getByText('Blank Project')
  100. screen.getByText('Example Project')
  101. screen.getByText('Upload Project')
  102. screen.getByText('Import from GitHub')
  103. // static text for institution templates
  104. screen.getByText('Institution Templates')
  105. // dynamic menu based on portalTemplates
  106. const affiliationTemplate = screen.getByRole('menuitem', {
  107. name: 'Affiliation 1 Template',
  108. })
  109. expect(affiliationTemplate.getAttribute('href')).to.equal(
  110. '/edu/test-new-template#templates'
  111. )
  112. // static text
  113. screen.getByText('Templates')
  114. // dynamic menu based on templateLinks
  115. screen.getByText('Journal articles')
  116. screen.getByText('View All')
  117. })
  118. })
  119. })