2022-05-15 14:57:28 +00:00
|
|
|
import { expect, it } from 'vitest'
|
2022-07-25 10:25:35 +00:00
|
|
|
import UnitTestCase from '@/__tests__/UnitTestCase'
|
|
|
|
import factory from '@/__tests__/factory'
|
2022-09-15 09:07:25 +00:00
|
|
|
import { http } from '@/services'
|
2024-10-13 17:37:01 +00:00
|
|
|
import type { CreateUserData, UpdateUserData } from '.'
|
|
|
|
import { userStore } from '.'
|
2022-05-15 14:57:28 +00:00
|
|
|
|
2024-06-01 18:02:27 +00:00
|
|
|
const currentUser = factory('user', {
|
2022-07-25 10:25:35 +00:00
|
|
|
id: 1,
|
|
|
|
name: 'John Doe',
|
|
|
|
email: 'john@doe.com',
|
2024-10-13 17:37:01 +00:00
|
|
|
is_admin: true,
|
2022-07-25 10:25:35 +00:00
|
|
|
})
|
2022-05-15 14:57:28 +00:00
|
|
|
|
|
|
|
new class extends UnitTestCase {
|
|
|
|
protected beforeEach () {
|
2022-07-25 10:25:35 +00:00
|
|
|
super.beforeEach(() => {
|
|
|
|
userStore.vault.clear()
|
|
|
|
userStore.init(currentUser)
|
|
|
|
})
|
2022-05-15 14:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected test () {
|
2022-07-25 10:25:35 +00:00
|
|
|
it('initializes with current user', () => {
|
|
|
|
expect(userStore.current).toEqual(currentUser)
|
|
|
|
expect(userStore.vault.size).toBe(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('syncs with vault', () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const user = factory('user')
|
2022-07-25 10:25:35 +00:00
|
|
|
|
|
|
|
expect(userStore.syncWithVault(user)).toEqual([user])
|
|
|
|
expect(userStore.vault.size).toBe(2)
|
|
|
|
expect(userStore.vault.get(user.id)).toEqual(user)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('fetches users', async () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const users = factory('user', 3)
|
2022-09-15 09:07:25 +00:00
|
|
|
const getMock = this.mock(http, 'get').mockResolvedValue(users)
|
2022-07-25 10:25:35 +00:00
|
|
|
|
|
|
|
await userStore.fetch()
|
|
|
|
|
|
|
|
expect(getMock).toHaveBeenCalledWith('users')
|
|
|
|
expect(userStore.vault.size).toBe(4)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('gets user by id', () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const user = factory('user', { id: 2 })
|
2022-07-25 10:25:35 +00:00
|
|
|
userStore.syncWithVault(user)
|
|
|
|
|
|
|
|
expect(userStore.byId(2)).toEqual(user)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('creates a user', async () => {
|
|
|
|
const data: CreateUserData = {
|
|
|
|
is_admin: false,
|
|
|
|
password: 'bratwurst',
|
|
|
|
name: 'Jane Doe',
|
2024-10-13 17:37:01 +00:00
|
|
|
email: 'jane@doe.com',
|
2022-07-25 10:25:35 +00:00
|
|
|
}
|
|
|
|
|
2024-06-01 18:02:27 +00:00
|
|
|
const user = factory('user', data)
|
2022-09-15 09:07:25 +00:00
|
|
|
const postMock = this.mock(http, 'post').mockResolvedValue(user)
|
2022-07-25 10:25:35 +00:00
|
|
|
|
|
|
|
expect(await userStore.store(data)).toEqual(user)
|
|
|
|
expect(postMock).toHaveBeenCalledWith('users', data)
|
|
|
|
expect(userStore.vault.size).toBe(2)
|
|
|
|
expect(userStore.state.users).toHaveLength(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('updates a user', async () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const user = factory('user', { id: 2 })
|
2022-07-25 10:25:35 +00:00
|
|
|
userStore.state.users.push(...userStore.syncWithVault(user))
|
|
|
|
|
|
|
|
const data: UpdateUserData = {
|
|
|
|
is_admin: true,
|
|
|
|
password: 'bratwurst',
|
|
|
|
name: 'Jane Doe',
|
2024-10-13 17:37:01 +00:00
|
|
|
email: 'jane@doe.com',
|
2022-07-25 10:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const updated = { ...user, ...data }
|
2022-09-15 09:07:25 +00:00
|
|
|
const putMock = this.mock(http, 'put').mockResolvedValue(updated)
|
2022-07-25 10:25:35 +00:00
|
|
|
|
|
|
|
await userStore.update(user, data)
|
|
|
|
|
|
|
|
expect(putMock).toHaveBeenCalledWith('users/2', data)
|
|
|
|
expect(userStore.vault.get(2)).toEqual(updated)
|
2022-05-15 14:57:28 +00:00
|
|
|
})
|
|
|
|
|
2022-07-25 10:25:35 +00:00
|
|
|
it('deletes a user', async () => {
|
2022-09-15 09:07:25 +00:00
|
|
|
const deleteMock = this.mock(http, 'delete')
|
2022-07-25 10:25:35 +00:00
|
|
|
|
2024-06-01 18:02:27 +00:00
|
|
|
const user = factory('user', { id: 2 })
|
2022-07-25 10:25:35 +00:00
|
|
|
userStore.state.users.push(...userStore.syncWithVault(user))
|
|
|
|
expect(userStore.vault.has(2)).toBe(true)
|
|
|
|
|
|
|
|
expect(await userStore.destroy(user))
|
2022-05-15 14:57:28 +00:00
|
|
|
|
2022-07-25 10:25:35 +00:00
|
|
|
expect(deleteMock).toHaveBeenCalledWith('users/2')
|
|
|
|
expect(userStore.vault.size).toBe(1)
|
|
|
|
expect(userStore.state.users).toHaveLength(1)
|
|
|
|
expect(userStore.vault.has(2)).toBe(false)
|
2022-05-15 14:57:28 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|