2024-04-04 08:29:28 -04:00
|
|
|
|
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
|
2024-04-03 10:12:09 -04:00
|
|
|
|
import { ReactElement } from 'react'
|
|
|
|
|
import sinon from 'sinon'
|
|
|
|
|
import fetchMock from 'fetch-mock'
|
|
|
|
|
import UnlinkUserModal from '@/features/group-management/components/members-table/unlink-user-modal'
|
|
|
|
|
import { GroupMembersProvider } from '@/features/group-management/context/group-members-context'
|
2024-04-04 08:29:28 -04:00
|
|
|
|
import { expect } from 'chai'
|
2024-04-03 10:12:09 -04:00
|
|
|
|
|
|
|
|
|
export function renderWithContext(component: ReactElement, props = {}) {
|
|
|
|
|
const GroupMembersProviderWrapper = ({
|
|
|
|
|
children,
|
|
|
|
|
}: {
|
|
|
|
|
children: ReactElement
|
|
|
|
|
}) => <GroupMembersProvider {...props}>{children}</GroupMembersProvider>
|
|
|
|
|
|
|
|
|
|
return render(component, { wrapper: GroupMembersProviderWrapper })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('<UnlinkUserModal />', function () {
|
|
|
|
|
let defaultProps: any
|
2024-04-04 08:29:28 -04:00
|
|
|
|
const groupId = 'group123'
|
|
|
|
|
const userId = 'user123'
|
2024-04-03 10:12:09 -04:00
|
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2024-04-04 08:29:28 -04:00
|
|
|
|
window.metaAttributesCache = new Map()
|
2024-04-03 10:12:09 -04:00
|
|
|
|
defaultProps = {
|
|
|
|
|
onClose: sinon.stub(),
|
2024-04-04 08:29:28 -04:00
|
|
|
|
user: { _id: userId },
|
2024-04-03 10:12:09 -04:00
|
|
|
|
setGroupUserAlert: sinon.stub(),
|
|
|
|
|
}
|
2024-04-04 08:29:28 -04:00
|
|
|
|
window.metaAttributesCache.set('ol-groupId', groupId)
|
2024-04-03 10:12:09 -04:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
afterEach(function () {
|
2024-04-04 08:29:28 -04:00
|
|
|
|
window.metaAttributesCache = new Map()
|
2024-04-03 10:12:09 -04:00
|
|
|
|
fetchMock.reset()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('displays the modal', async function () {
|
|
|
|
|
renderWithContext(<UnlinkUserModal {...defaultProps} />)
|
|
|
|
|
await screen.findByRole('heading', {
|
|
|
|
|
name: 'Unlink user',
|
|
|
|
|
})
|
2024-04-04 08:29:28 -04:00
|
|
|
|
screen.getByText('You’re about to remove the SSO login option for', {
|
|
|
|
|
exact: false,
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('closes the modal on success', async function () {
|
|
|
|
|
fetchMock.post(`/manage/groups/${groupId}/unlink-user/${userId}`, 200)
|
|
|
|
|
|
|
|
|
|
renderWithContext(<UnlinkUserModal {...defaultProps} />)
|
|
|
|
|
await screen.findByRole('heading', {
|
|
|
|
|
name: 'Unlink user',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const confirmButton = screen.getByRole('button', { name: 'Unlink user' })
|
|
|
|
|
fireEvent.click(confirmButton)
|
|
|
|
|
|
|
|
|
|
await waitFor(() => expect(defaultProps.onClose).to.have.been.called)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('handles errors', async function () {
|
|
|
|
|
fetchMock.post(`/manage/groups/${groupId}/unlink-user/${userId}`, 500)
|
|
|
|
|
|
|
|
|
|
renderWithContext(<UnlinkUserModal {...defaultProps} />)
|
|
|
|
|
await screen.findByRole('heading', {
|
|
|
|
|
name: 'Unlink user',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const confirmButton = screen.getByRole('button', { name: 'Unlink user' })
|
|
|
|
|
fireEvent.click(confirmButton)
|
|
|
|
|
|
|
|
|
|
await waitFor(() => screen.findByText('Sorry, something went wrong'))
|
2024-04-03 10:12:09 -04:00
|
|
|
|
})
|
|
|
|
|
})
|