2024-01-18 11:13:05 +00:00
|
|
|
import Router from '@/router'
|
2022-05-13 12:56:13 +00:00
|
|
|
import { expect, it } from 'vitest'
|
|
|
|
import factory from '@/__tests__/factory'
|
2022-05-13 17:58:38 +00:00
|
|
|
import UnitTestCase from '@/__tests__/UnitTestCase'
|
2022-11-29 10:18:58 +00:00
|
|
|
import { screen } from '@testing-library/vue'
|
2022-07-22 16:31:07 +00:00
|
|
|
import { eventBus } from '@/utils'
|
2022-11-29 10:18:58 +00:00
|
|
|
import { userStore } from '@/stores'
|
|
|
|
import { DialogBoxStub } from '@/__tests__/stubs'
|
2023-08-20 22:35:58 +00:00
|
|
|
import { invitationService } from '@/services'
|
2024-01-18 11:13:05 +00:00
|
|
|
import UserCard from './UserCard.vue'
|
2022-05-13 12:56:13 +00:00
|
|
|
|
2022-05-13 17:58:38 +00:00
|
|
|
new class extends UnitTestCase {
|
2022-05-13 12:56:13 +00:00
|
|
|
private renderComponent (user: User) {
|
|
|
|
return this.render(UserCard, {
|
|
|
|
props: {
|
|
|
|
user
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
protected test () {
|
|
|
|
it('has different behaviors for current user', () => {
|
|
|
|
const user = factory<User>('user')
|
2024-01-15 22:26:50 +00:00
|
|
|
this.be(user).renderComponent(user)
|
2022-05-13 12:56:13 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
screen.getByTitle('This is you!')
|
|
|
|
screen.getByText('Your Profile')
|
2022-05-13 12:56:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('edits user', async () => {
|
|
|
|
const user = factory<User>('user')
|
2022-07-22 16:31:07 +00:00
|
|
|
const emitMock = this.mock(eventBus, 'emit')
|
2022-11-29 10:18:58 +00:00
|
|
|
this.renderComponent(user)
|
2022-05-13 12:56:13 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByRole('button', { name: 'Edit' }))
|
2022-05-13 12:56:13 +00:00
|
|
|
|
2022-07-22 16:31:07 +00:00
|
|
|
expect(emitMock).toHaveBeenCalledWith('MODAL_SHOW_EDIT_USER_FORM', user)
|
2022-05-13 12:56:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('redirects to Profile screen if edit current user', async () => {
|
2024-01-18 11:13:05 +00:00
|
|
|
const mock = this.mock(Router, 'go')
|
2022-05-13 12:56:13 +00:00
|
|
|
const user = factory<User>('user')
|
2024-01-15 22:26:50 +00:00
|
|
|
this.be(user).renderComponent(user)
|
2022-05-13 12:56:13 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByRole('button', { name: 'Your Profile' }))
|
2022-05-13 12:56:13 +00:00
|
|
|
|
|
|
|
expect(mock).toHaveBeenCalledWith('profile')
|
|
|
|
})
|
2022-11-29 10:18:58 +00:00
|
|
|
|
|
|
|
it('deletes user if confirmed', async () => {
|
|
|
|
this.mock(DialogBoxStub.value, 'confirm').mockResolvedValue(true)
|
|
|
|
const user = factory<User>('user')
|
2024-01-15 22:26:50 +00:00
|
|
|
this.beAdmin().renderComponent(user)
|
2022-11-29 10:18:58 +00:00
|
|
|
const destroyMock = this.mock(userStore, 'destroy')
|
|
|
|
|
|
|
|
await this.user.click(screen.getByRole('button', { name: 'Delete' }))
|
|
|
|
|
|
|
|
expect(destroyMock).toHaveBeenCalledWith(user)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not delete user if not confirmed', async () => {
|
|
|
|
this.mock(DialogBoxStub.value, 'confirm').mockResolvedValue(false)
|
|
|
|
const user = factory<User>('user')
|
2024-01-15 22:26:50 +00:00
|
|
|
this.beAdmin().renderComponent(user)
|
2022-11-29 10:18:58 +00:00
|
|
|
const destroyMock = this.mock(userStore, 'destroy')
|
|
|
|
|
|
|
|
await this.user.click(screen.getByRole('button', { name: 'Delete' }))
|
|
|
|
|
|
|
|
expect(destroyMock).not.toHaveBeenCalled()
|
|
|
|
})
|
2023-08-20 22:35:58 +00:00
|
|
|
|
|
|
|
it('revokes invite for prospects', async () => {
|
|
|
|
this.mock(DialogBoxStub.value, 'confirm').mockResolvedValue(true)
|
|
|
|
const prospect = factory.states('prospect')<User>('user')
|
2024-01-15 22:26:50 +00:00
|
|
|
this.beAdmin().renderComponent(prospect)
|
2023-08-20 22:35:58 +00:00
|
|
|
const revokeMock = this.mock(invitationService, 'revoke')
|
|
|
|
|
|
|
|
await this.user.click(screen.getByRole('button', { name: 'Revoke' }))
|
|
|
|
|
|
|
|
expect (revokeMock).toHaveBeenCalledWith(prospect)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not revoke invite for prospects if not confirmed', async () => {
|
|
|
|
this.mock(DialogBoxStub.value, 'confirm').mockResolvedValue(false)
|
|
|
|
const prospect = factory.states('prospect')<User>('user')
|
2024-01-15 22:26:50 +00:00
|
|
|
this.beAdmin().renderComponent(prospect)
|
2023-08-20 22:35:58 +00:00
|
|
|
const revokeMock = this.mock(invitationService, 'revoke')
|
|
|
|
|
|
|
|
await this.user.click(screen.getByRole('button', { name: 'Revoke' }))
|
|
|
|
|
|
|
|
expect(revokeMock).not.toHaveBeenCalled()
|
|
|
|
})
|
2022-05-13 12:56:13 +00:00
|
|
|
}
|
|
|
|
}
|