dropdown-button.spec.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import type { PropsWithChildren } from 'react'
  2. import sinon from 'sinon'
  3. import DropdownButton from '@/features/group-management/components/members-table/dropdown-button'
  4. import { GroupMembersProvider } from '@/features/group-management/context/group-members-context'
  5. import { User } from '../../../../../../types/group-management/user'
  6. function Wrapper({ children }: PropsWithChildren<Record<string, unknown>>) {
  7. return (
  8. <ul className="managed-users-list">
  9. <span
  10. className="managed-users-actions"
  11. style={{ display: 'flex', width: '100%', justifyContent: 'flex-end' }}
  12. >
  13. <GroupMembersProvider>{children}</GroupMembersProvider>
  14. </span>
  15. </ul>
  16. )
  17. }
  18. function mountDropDownComponent(user: User, subscriptionId: string) {
  19. cy.mount(
  20. <Wrapper>
  21. <DropdownButton
  22. user={user}
  23. openOffboardingModalForUser={sinon.stub()}
  24. groupId={subscriptionId}
  25. setManagedUserAlert={sinon.stub()}
  26. />
  27. </Wrapper>
  28. )
  29. }
  30. describe('ManagedUserDropdownButton', function () {
  31. const subscriptionId = '123abc'
  32. describe('with managed user', function () {
  33. const user = {
  34. _id: 'some-user',
  35. email: 'some.user@example.com',
  36. first_name: 'Some',
  37. last_name: 'User',
  38. invite: false,
  39. last_active_at: new Date(),
  40. enrollment: {
  41. managedBy: 'some-group',
  42. enrolledAt: new Date(),
  43. },
  44. isEntityAdmin: undefined,
  45. }
  46. beforeEach(function () {
  47. cy.window().then(win => {
  48. win.metaAttributesCache.set('ol-users', [user])
  49. })
  50. mountDropDownComponent(user, subscriptionId)
  51. })
  52. it('should render dropdown button', function () {
  53. cy.get('#managed-user-dropdown-some\\.user\\@example\\.com').should(
  54. 'exist'
  55. )
  56. cy.get(`.action-btn`).should('exist')
  57. })
  58. it('should show the correct menu when dropdown button is clicked', function () {
  59. cy.get('.action-btn').click()
  60. cy.findByTestId('delete-user-action').should('exist')
  61. cy.findByTestId('delete-user-action').then($el => {
  62. Cypress.dom.isVisible($el)
  63. })
  64. })
  65. })
  66. describe('with non-managed user (have joined group)', function () {
  67. const user = {
  68. _id: 'some-user',
  69. email: 'some.user@example.com',
  70. first_name: 'Some',
  71. last_name: 'User',
  72. invite: false,
  73. last_active_at: new Date(),
  74. enrollment: {},
  75. isEntityAdmin: undefined,
  76. }
  77. beforeEach(function () {
  78. cy.window().then(win => {
  79. win.metaAttributesCache.set('ol-users', [user])
  80. })
  81. mountDropDownComponent(user, subscriptionId)
  82. })
  83. it('should render dropdown button', function () {
  84. cy.get('#managed-user-dropdown-some\\.user\\@example\\.com').should(
  85. 'exist'
  86. )
  87. cy.get(`.action-btn`).should('exist')
  88. })
  89. it('should show the correct menu when dropdown button is clicked', function () {
  90. cy.get('.action-btn').click()
  91. cy.findByTestId('resend-managed-user-invite-action').should('exist')
  92. cy.findByTestId('resend-managed-user-invite-action').then($el => {
  93. Cypress.dom.isVisible($el)
  94. })
  95. cy.findByTestId('remove-user-action').should('exist')
  96. cy.findByTestId('remove-user-action').then($el => {
  97. Cypress.dom.isVisible($el)
  98. })
  99. })
  100. })
  101. describe('with pending user (have not joined group)', function () {
  102. const user = {
  103. _id: 'some-user',
  104. email: 'some.user@example.com',
  105. first_name: 'Some',
  106. last_name: 'User',
  107. invite: true,
  108. last_active_at: new Date(),
  109. enrollment: {},
  110. isEntityAdmin: undefined,
  111. }
  112. beforeEach(function () {
  113. cy.window().then(win => {
  114. win.metaAttributesCache.set('ol-users', [user])
  115. })
  116. mountDropDownComponent(user, subscriptionId)
  117. })
  118. it('should render dropdown button', function () {
  119. cy.get('#managed-user-dropdown-some\\.user\\@example\\.com').should(
  120. 'exist'
  121. )
  122. cy.get(`.action-btn`).should('exist')
  123. })
  124. it('should show the correct menu when dropdown button is clicked', function () {
  125. cy.get('.action-btn').click()
  126. cy.findByTestId('resend-group-invite-action').should('exist')
  127. cy.findByTestId('resend-group-invite-action').then($el => {
  128. Cypress.dom.isVisible($el)
  129. })
  130. cy.findByTestId('remove-user-action').should('exist')
  131. cy.findByTestId('remove-user-action').then($el => {
  132. Cypress.dom.isVisible($el)
  133. })
  134. })
  135. })
  136. describe('with group admin user', function () {
  137. const user = {
  138. _id: 'some-user',
  139. email: 'some.user@example.com',
  140. first_name: 'Some',
  141. last_name: 'User',
  142. invite: false,
  143. last_active_at: new Date(),
  144. enrollment: {},
  145. isEntityAdmin: true,
  146. }
  147. beforeEach(function () {
  148. cy.window().then(win => {
  149. win.metaAttributesCache.set('ol-users', [user])
  150. })
  151. mountDropDownComponent(user, subscriptionId)
  152. })
  153. it('should render dropdown button', function () {
  154. cy.get('#managed-user-dropdown-some\\.user\\@example\\.com').should(
  155. 'exist'
  156. )
  157. cy.get(`.action-btn`).should('exist')
  158. })
  159. it('should show the (empty) menu when dropdown button is clicked', function () {
  160. cy.get('.action-btn').click()
  161. cy.findByTestId('no-actions-available').should('exist')
  162. })
  163. })
  164. describe('with managed group admin user', function () {
  165. const user = {
  166. _id: 'some-user',
  167. email: 'some.user@example.com',
  168. first_name: 'Some',
  169. last_name: 'User',
  170. invite: false,
  171. last_active_at: new Date(),
  172. enrollment: {
  173. managedBy: 'some-group',
  174. enrolledAt: new Date(),
  175. },
  176. isEntityAdmin: true,
  177. }
  178. beforeEach(function () {
  179. cy.window().then(win => {
  180. win.metaAttributesCache.set('ol-users', [user])
  181. })
  182. mountDropDownComponent(user, subscriptionId)
  183. })
  184. it('should render the button', function () {
  185. cy.get('#managed-user-dropdown-some\\.user\\@example\\.com').should(
  186. 'exist'
  187. )
  188. cy.get(`.action-btn`).should('exist')
  189. })
  190. it('should show the (empty) menu when the button is clicked', function () {
  191. cy.get('.action-btn').click()
  192. cy.findByTestId('no-actions-available').should('exist')
  193. })
  194. })
  195. describe('sending SSO invite reminder', function () {
  196. const user = {
  197. _id: 'some-user',
  198. email: 'some.user@example.com',
  199. first_name: 'Some',
  200. last_name: 'User',
  201. invite: false,
  202. last_active_at: new Date(),
  203. enrollment: {
  204. managedBy: 'some-group',
  205. enrolledAt: new Date(),
  206. },
  207. isEntityAdmin: undefined,
  208. }
  209. beforeEach(function () {
  210. cy.window().then(win => {
  211. win.metaAttributesCache.set('ol-users', [user])
  212. })
  213. cy.window().then(win => {
  214. win.metaAttributesCache.set('ol-groupSSOActive', true)
  215. })
  216. })
  217. it('should show resend invite when user is admin', function () {
  218. mountDropDownComponent({ ...user, isEntityAdmin: true }, '123abc')
  219. cy.get('.action-btn').click()
  220. cy.findByTestId('resend-sso-link-invite-action').should('exist')
  221. })
  222. it('should not show resend invite when SSO is disabled', function () {
  223. cy.window().then(win => {
  224. win.metaAttributesCache.set('ol-groupSSOActive', false)
  225. })
  226. mountDropDownComponent(user, '123abc')
  227. cy.get('.action-btn').click()
  228. cy.findByTestId('resend-sso-link-invite-action').should('not.exist')
  229. })
  230. it('should not show resend invite when user has accepted SSO already', function () {
  231. cy.window().then(win => {
  232. win.metaAttributesCache.set('ol-groupSSOActive', false)
  233. })
  234. mountDropDownComponent(
  235. {
  236. ...user,
  237. enrollment: {
  238. managedBy: 'some-group',
  239. enrolledAt: new Date(),
  240. sso: [
  241. {
  242. groupId: 'abc123abc123',
  243. linkedAt: new Date(),
  244. primary: true,
  245. },
  246. ],
  247. },
  248. },
  249. '123abc'
  250. )
  251. cy.get('.action-btn').click()
  252. cy.findByTestId('resend-sso-link-invite-action').should('not.exist')
  253. })
  254. it('should show the resend SSO invite option when dropdown button is clicked', function () {
  255. cy.window().then(win => {
  256. win.metaAttributesCache.set('ol-groupSSOActive', true)
  257. })
  258. mountDropDownComponent(user, '123abc')
  259. cy.get('.action-btn').click()
  260. cy.findByTestId('resend-sso-link-invite-action').should('exist')
  261. cy.findByTestId('resend-sso-link-invite-action').then($el => {
  262. Cypress.dom.isVisible($el)
  263. })
  264. })
  265. it('should make the correct post request when resend SSO invite is clicked ', function () {
  266. cy.window().then(win => {
  267. win.metaAttributesCache.set('ol-groupSSOActive', true)
  268. })
  269. cy.intercept(
  270. 'POST',
  271. '/manage/groups/123abc/resendSSOLinkInvite/some-user',
  272. { success: true }
  273. ).as('resendInviteRequest')
  274. mountDropDownComponent(user, '123abc')
  275. cy.get('.action-btn').click()
  276. cy.findByTestId('resend-sso-link-invite-action')
  277. .should('exist')
  278. .as('resendInvite')
  279. cy.get('@resendInvite').click()
  280. cy.wait('@resendInviteRequest')
  281. })
  282. })
  283. })