koel/resources/assets/js/components/screens/UserListScreen.spec.ts

75 lines
2.1 KiB
TypeScript
Raw Normal View History

import { expect, it } from 'vitest'
import UnitTestCase from '@/__tests__/UnitTestCase'
import factory from '@/__tests__/factory'
import { screen } from '@testing-library/vue'
import { http } from '@/services'
import { eventBus } from '@/utils'
2024-04-04 22:20:42 +00:00
import Btn from '@/components/ui/form/Btn.vue'
import BtnGroup from '@/components/ui/form/BtnGroup.vue'
import UserListScreen from './UserListScreen.vue'
new class extends UnitTestCase {
2024-02-25 19:32:53 +00:00
protected beforeEach (cb?: Closure) {
super.beforeEach(cb);
this.beAdmin()
}
protected test () {
it('displays a list of users', async () => {
await this.renderComponent()
2023-08-20 22:35:58 +00:00
expect(screen.getAllByTestId('user-card')).toHaveLength(6)
2023-08-20 22:35:58 +00:00
expect(screen.queryByTestId('prospects-heading')).toBeNull()
})
it('displays a list of user prospects', async () => {
const users = [...factory.states('prospect')<User>('user', 2), ...factory<User>('user', 3)]
await this.renderComponent(users)
expect(screen.getAllByTestId('user-card')).toHaveLength(5)
screen.getByTestId('prospects-heading')
})
it('triggers create user modal', async () => {
const emitMock = this.mock(eventBus, 'emit')
await this.renderComponent()
await this.user.click(screen.getByRole('button', { name: 'Add' }))
expect(emitMock).toHaveBeenCalledWith('MODAL_SHOW_ADD_USER_FORM')
})
2023-08-20 22:35:58 +00:00
it('triggers invite user modal', async () => {
const emitMock = this.mock(eventBus, 'emit')
await this.renderComponent()
await this.user.click(screen.getByRole('button', { name: 'Invite' }))
expect(emitMock).toHaveBeenCalledWith('MODAL_SHOW_INVITE_USER_FORM')
})
}
2024-04-23 21:01:27 +00:00
private async renderComponent (users: User[] = []) {
if (users.length === 0) {
users = factory<User>('user', 6)
}
const fetchMock = this.mock(http, 'get').mockResolvedValue(users)
this.render(UserListScreen, {
global: {
stubs: {
Btn,
BtnGroup,
UserCard: this.stub('user-card')
}
}
})
expect(fetchMock).toHaveBeenCalledWith('users')
await this.tick(2)
}
}