Implement account deletion handling and improve user feedback
Some checks failed
Chore App Build and Push Docker Images / build-and-push (push) Has been cancelled
Some checks failed
Chore App Build and Push Docker Images / build-and-push (push) Has been cancelled
- Added checks for accounts marked for deletion in signup, verification, and password reset processes. - Updated reward and task listing to sort user-created items first. - Enhanced user API to clear verification and reset tokens when marking accounts for deletion. - Introduced tests for marked accounts to ensure proper handling in various scenarios. - Updated profile and reward edit components to reflect changes in validation and data handling.
This commit is contained in:
@@ -283,3 +283,207 @@ describe('UserProfile - Delete Account', () => {
|
||||
expect(wrapper.vm.deleteErrorMessage).toBe('This account is already marked for deletion.')
|
||||
})
|
||||
})
|
||||
|
||||
describe('UserProfile - Profile Update', () => {
|
||||
let wrapper: VueWrapper<any>
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
;(global.fetch as any).mockClear()
|
||||
|
||||
// Mock fetch for profile loading in onMounted
|
||||
;(global.fetch as any).mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
image_id: 'initial-image-id',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test@example.com',
|
||||
}),
|
||||
})
|
||||
|
||||
// Mount component with router
|
||||
wrapper = mount(UserProfile, {
|
||||
global: {
|
||||
plugins: [mockRouter],
|
||||
stubs: {
|
||||
EntityEditForm: {
|
||||
template: '<div class="mock-form"><slot /></div>',
|
||||
props: ['initialData', 'fields', 'loading', 'error', 'isEdit', 'entityLabel', 'title'],
|
||||
emits: ['submit', 'cancel', 'add-image'],
|
||||
},
|
||||
ModalDialog: {
|
||||
template: '<div class="mock-modal"><slot /></div>',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it('updates initialData after successful profile save', async () => {
|
||||
await flushPromises()
|
||||
await nextTick()
|
||||
|
||||
// Initial image_id should be set from mount
|
||||
expect(wrapper.vm.initialData.image_id).toBe('initial-image-id')
|
||||
|
||||
// Mock successful save response
|
||||
;(global.fetch as any).mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({}),
|
||||
})
|
||||
|
||||
// Simulate form submission with new image_id
|
||||
const newFormData = {
|
||||
image_id: 'new-image-id',
|
||||
first_name: 'Updated',
|
||||
last_name: 'Name',
|
||||
email: 'test@example.com',
|
||||
}
|
||||
|
||||
await wrapper.vm.handleSubmit(newFormData)
|
||||
await flushPromises()
|
||||
|
||||
// initialData should now be updated to match the saved form
|
||||
expect(wrapper.vm.initialData.image_id).toBe('new-image-id')
|
||||
expect(wrapper.vm.initialData.first_name).toBe('Updated')
|
||||
expect(wrapper.vm.initialData.last_name).toBe('Name')
|
||||
})
|
||||
|
||||
it('allows dirty detection after save when reverting to original value', async () => {
|
||||
await flushPromises()
|
||||
await nextTick()
|
||||
|
||||
// Start with initial-image-id
|
||||
expect(wrapper.vm.initialData.image_id).toBe('initial-image-id')
|
||||
|
||||
// Mock successful save
|
||||
;(global.fetch as any).mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({}),
|
||||
})
|
||||
|
||||
// Change and save to new-image-id
|
||||
await wrapper.vm.handleSubmit({
|
||||
image_id: 'new-image-id',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test@example.com',
|
||||
})
|
||||
await flushPromises()
|
||||
|
||||
// initialData should now be new-image-id
|
||||
expect(wrapper.vm.initialData.image_id).toBe('new-image-id')
|
||||
|
||||
// Now if user changes back to initial-image-id, it should be detected as different
|
||||
// (because initialData is now new-image-id)
|
||||
const currentInitial = wrapper.vm.initialData.image_id
|
||||
expect(currentInitial).toBe('new-image-id')
|
||||
expect(currentInitial).not.toBe('initial-image-id')
|
||||
})
|
||||
|
||||
it('handles image upload during profile save', async () => {
|
||||
await flushPromises()
|
||||
await nextTick()
|
||||
|
||||
const mockFile = new File(['test'], 'test.png', { type: 'image/png' })
|
||||
wrapper.vm.localImageFile = mockFile
|
||||
|
||||
// Mock image upload response
|
||||
;(global.fetch as any).mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({ id: 'uploaded-image-id' }),
|
||||
})
|
||||
|
||||
// Mock profile update response
|
||||
;(global.fetch as any).mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({}),
|
||||
})
|
||||
|
||||
await wrapper.vm.handleSubmit({
|
||||
image_id: 'local-upload',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test@example.com',
|
||||
})
|
||||
await flushPromises()
|
||||
|
||||
// Should have called image upload
|
||||
expect(global.fetch).toHaveBeenCalledWith(
|
||||
'/api/image/upload',
|
||||
expect.objectContaining({
|
||||
method: 'POST',
|
||||
}),
|
||||
)
|
||||
|
||||
// initialData should be updated with uploaded image ID
|
||||
expect(wrapper.vm.initialData.image_id).toBe('uploaded-image-id')
|
||||
})
|
||||
|
||||
it('shows error message on failed image upload', async () => {
|
||||
await flushPromises()
|
||||
await nextTick()
|
||||
|
||||
const mockFile = new File(['test'], 'test.png', { type: 'image/png' })
|
||||
wrapper.vm.localImageFile = mockFile
|
||||
|
||||
// Mock failed image upload
|
||||
;(global.fetch as any).mockResolvedValueOnce({
|
||||
ok: false,
|
||||
status: 500,
|
||||
})
|
||||
|
||||
await wrapper.vm.handleSubmit({
|
||||
image_id: 'local-upload',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test@example.com',
|
||||
})
|
||||
await flushPromises()
|
||||
|
||||
expect(wrapper.vm.errorMsg).toBe('Failed to upload image.')
|
||||
expect(wrapper.vm.loading).toBe(false)
|
||||
})
|
||||
|
||||
it('shows success modal after profile update', async () => {
|
||||
await flushPromises()
|
||||
await nextTick()
|
||||
;(global.fetch as any).mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({}),
|
||||
})
|
||||
|
||||
await wrapper.vm.handleSubmit({
|
||||
image_id: 'some-image-id',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test@example.com',
|
||||
})
|
||||
await flushPromises()
|
||||
|
||||
expect(wrapper.vm.showModal).toBe(true)
|
||||
expect(wrapper.vm.modalTitle).toBe('Profile Updated')
|
||||
expect(wrapper.vm.modalMessage).toBe('Your profile was updated successfully.')
|
||||
})
|
||||
|
||||
it('shows error message on failed profile update', async () => {
|
||||
await flushPromises()
|
||||
await nextTick()
|
||||
;(global.fetch as any).mockResolvedValueOnce({
|
||||
ok: false,
|
||||
status: 500,
|
||||
})
|
||||
|
||||
await wrapper.vm.handleSubmit({
|
||||
image_id: 'some-image-id',
|
||||
first_name: 'Test',
|
||||
last_name: 'User',
|
||||
email: 'test@example.com',
|
||||
})
|
||||
await flushPromises()
|
||||
|
||||
expect(wrapper.vm.errorMsg).toBe('Failed to update profile.')
|
||||
expect(wrapper.vm.loading).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user