Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,25 @@ const mocks = vi.hoisted(() => {
showError: vi.fn(),
emit: vi.fn(),
blob: null as Blob | null,
imageScale: 1,
image: {
$ready: vi.fn(),
$getTransform: vi.fn(),
getBoundingClientRect: () => new DOMRect(10.5, 0, 200, 300),
},
selection: {
width: 300,
addEventListener: vi.fn(),
$change: vi.fn(),
$toCanvas: vi.fn(),
},
cropper: {
replace: vi.fn(),
destroy: vi.fn(),
getCroppedCanvas: vi.fn(() => ({ width: 1024 })),
scale: vi.fn(),
getCropperCanvas: () => ({ getBoundingClientRect: () => new DOMRect(0, 0, 300, 300) }),
getCropperImage: () => mocks.image,
getCropperSelection: () => mocks.selection,
},
Cropper: vi.fn(),
}
})

Expand All @@ -53,12 +66,7 @@ vi.mock('@nextcloud/dialogs', () => {
}
return { FilePickerClosed: mocks.FilePickerClosed, getFilePickerBuilder: () => builder, showError: mocks.showError }
})
vi.mock('cropperjs', () => ({
default: vi.fn(function() {
return mocks.cropper
}),
}))
vi.mock('cropperjs/dist/cropper.css', () => ({}))
vi.mock('cropperjs', () => ({ default: mocks.Cropper }))
vi.mock('./shared/VisibilityScopeControl.vue', () => ({ default: { render: () => h('div') } }))

const flushPromises = () => new Promise((resolve) => setTimeout(resolve))
Expand All @@ -81,6 +89,43 @@ async function click(wrapper: VueWrapper, label: string) {
await flushPromises()
}

/**
* @param wrapper The mounted section
* @param type Mime type of the uploaded file
*/
async function upload(wrapper: VueWrapper, type = 'image/png') {
const input = wrapper.find('input[type="file"]')
Object.defineProperty(input.element, 'files', { value: [new File(['png'], 'avatar', { type })] })
await input.trigger('change')
}

/**
* @param wrapper The mounted section
*/
async function startCropping(wrapper: VueWrapper) {
await upload(wrapper)
await vi.waitFor(() => expect(mocks.selection.addEventListener).toHaveBeenCalled())
}

/**
* @param x Horizontal position of the selection
* @param y Vertical position of the selection
* @param size Width and height of the selection
*/
function changeSelection(x: number, y: number, size: number) {
const [, listener] = mocks.selection.addEventListener.mock.lastCall! as [string, (event: Event) => void]
const event = new CustomEvent('change', { cancelable: true, detail: { x, y, width: size, height: size } })
listener(event)
return event.defaultPrevented
}

/**
* Source of the picture passed to the cropper
*/
function croppedPicture() {
return (mocks.Cropper.mock.lastCall![0] as HTMLImageElement).src
}

/**
* @param wrapper The mounted section
*/
Expand All @@ -91,48 +136,110 @@ function isCropperShown(wrapper: VueWrapper) {
describe('AvatarSection', () => {
beforeEach(() => {
vi.clearAllMocks()
mocks.cropper.scale.mockReturnValue({ getCroppedCanvas: () => ({ toBlob: (callback: (blob: Blob | null) => void) => callback(mocks.blob) }) })
mocks.Cropper.mockImplementation(function() {
return mocks.cropper
})
mocks.image.$ready.mockResolvedValue({})
mocks.image.$getTransform.mockImplementation(() => [mocks.imageScale, 0, 0, mocks.imageScale, 0, 0])
mocks.selection.$toCanvas.mockResolvedValue({ toBlob: (callback: (blob: Blob | null) => void) => callback(mocks.blob) })
mocks.blob = new Blob(['avatar'], { type: 'image/png' })
mocks.imageScale = 1
})

it('rejects uploads that are no png or jpg', async () => {
const wrapper = await mountAvatarSection()
const input = wrapper.find('input[type="file"]')
Object.defineProperty(input.element, 'files', { value: [new File(['gif'], 'avatar.gif', { type: 'image/gif' })] })

await input.trigger('change')
await upload(wrapper, 'image/gif')

expect(mocks.showError).toHaveBeenCalledWith('Please select a valid png or jpg file')
expect(mocks.cropper.replace).not.toHaveBeenCalled()
expect(mocks.Cropper).not.toHaveBeenCalled()
expect(isCropperShown(wrapper)).toBe(false)
})

it('crops an uploaded picture', async () => {
const wrapper = await mountAvatarSection()
const input = wrapper.find('input[type="file"]')
Object.defineProperty(input.element, 'files', { value: [new File(['png'], 'avatar.png', { type: 'image/png' })] })

await input.trigger('change')
await vi.waitFor(() => expect(mocks.cropper.replace).toHaveBeenCalled())
await startCropping(wrapper)

expect(croppedPicture()).toBe('data:image/png;base64,cG5n')
expect(mocks.selection.$change).toHaveBeenCalledWith(11, 50, 199, 199)
expect(isCropperShown(wrapper)).toBe(true)
})

it('saves only once the picture is loaded', async () => {
mocks.image.$ready.mockReturnValue(new Promise(() => {}))
const wrapper = await mountAvatarSection()

await upload(wrapper)
await vi.waitFor(() => expect(mocks.image.$ready).toHaveBeenCalled())

const save = wrapper.findAll('button').find((button) => button.text() === 'Set as profile picture')!
expect(save.attributes('disabled')).toBeDefined()
})

it('keeps the selection inside the picture', async () => {
const wrapper = await mountAvatarSection()
await startCropping(wrapper)

expect(changeSelection(11, 0, 199)).toBe(false)
expect(changeSelection(11, 101, 199)).toBe(false)
expect(changeSelection(11, 102, 199)).toBe(true)
expect(changeSelection(10, 50, 199)).toBe(true)
expect(changeSelection(100, 50, 199)).toBe(true)
})

it('reports a picture that cannot be loaded', async () => {
mocks.image.$ready.mockRejectedValue(new Error('Failed to load the image source'))
const wrapper = await mountAvatarSection()

await upload(wrapper)
await vi.waitFor(() => expect(mocks.showError).toHaveBeenCalledWith('Error cropping profile picture'))

expect(mocks.selection.addEventListener).not.toHaveBeenCalled()
expect(isCropperShown(wrapper)).toBe(false)
})

it('replaces the previous cropper when cropping another picture', async () => {
const wrapper = await mountAvatarSection()
await startCropping(wrapper)
await click(wrapper, 'Cancel')

await upload(wrapper)
await vi.waitFor(() => expect(mocks.Cropper).toHaveBeenCalledTimes(2))

expect(mocks.cropper.destroy).toHaveBeenCalledOnce()
})

it('saves the cropped picture scaled down', async () => {
mocks.post.mockResolvedValue({ data: {} })
mocks.imageScale = 0.25
const wrapper = await mountAvatarSection(true)
await startCropping(wrapper)

await click(wrapper, 'Set as profile picture')

expect(mocks.cropper.scale).toHaveBeenCalledWith(0.5, 0.5)
expect(mocks.selection.$toCanvas).toHaveBeenCalledWith({ width: 512 })
expect(mocks.post).toHaveBeenCalledWith(expect.any(String), expect.any(FormData))
expect(mocks.emit).toHaveBeenCalledWith('settings:avatar:updated', expect.any(Number))
expect(window.oc_userconfig.avatar.generated).toBe(false)
})

it('saves a small cropped picture in its original size', async () => {
mocks.post.mockResolvedValue({ data: {} })
mocks.imageScale = 1.5
const wrapper = await mountAvatarSection()
await startCropping(wrapper)

await click(wrapper, 'Set as profile picture')

expect(mocks.selection.$toCanvas).toHaveBeenCalledWith({ width: 200 })
expect(mocks.post).toHaveBeenCalled()
})

it('keeps the previous picture when saving the cropped one fails', async () => {
mocks.post.mockRejectedValue(new Error('500'))
const wrapper = await mountAvatarSection(true)
await startCropping(wrapper)

await click(wrapper, 'Set as profile picture')

Expand All @@ -144,11 +251,24 @@ describe('AvatarSection', () => {
it('reports a picture that cannot be cropped', async () => {
mocks.blob = null
const wrapper = await mountAvatarSection()
await startCropping(wrapper)

await click(wrapper, 'Set as profile picture')

expect(mocks.showError).toHaveBeenCalledWith('Error cropping profile picture')
expect(mocks.post).not.toHaveBeenCalled()
})

it('reports a picture that fails to render', async () => {
mocks.selection.$toCanvas.mockRejectedValue(new Error('Failed to load the image source'))
const wrapper = await mountAvatarSection()
await startCropping(wrapper)

await click(wrapper, 'Set as profile picture')

expect(mocks.showError).toHaveBeenCalledWith('Error cropping profile picture')
expect(mocks.post).not.toHaveBeenCalled()
expect(isCropperShown(wrapper)).toBe(false)
})

it('does nothing when the file picker is closed', async () => {
Expand Down Expand Up @@ -180,7 +300,7 @@ describe('AvatarSection', () => {

await click(wrapper, 'Choose from Nextcloud Files')

expect(mocks.cropper.replace).toHaveBeenCalledWith('data:image/png;base64,')
expect(croppedPicture()).toBe('data:image/png;base64,')
expect(isCropperShown(wrapper)).toBe(true)
})

Expand Down
Loading
Loading