Skip to content

Commit

Permalink
fix(test): mock getClientRect
Browse files Browse the repository at this point in the history
Also move mocks from setup.mjs into testHelpers.

Signed-off-by: Max <max@nextcloud.com>
  • Loading branch information
max-nextcloud committed Oct 14, 2024
1 parent 21fd77a commit e8960cb
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 83 deletions.
87 changes: 4 additions & 83 deletions src/tests/setup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,89 +4,10 @@
*/

import Vue from 'vue'

global.t = vi.fn().mockImplementation((app, text) => text)
global.n = vi.fn().mockImplementation((app, text) => text)

vi.mock('@nextcloud/auth', () => ({
getCurrentUser: vi.fn().mockImplementation(() => ({
uid: 'user1',
displayName: 'User 1',
isAdmin: false,
})),
getRequestToken: vi.fn().mockImplementation(() => '123456abcdef'),
onRequestTokenUpdate: vi.fn().mockImplementation(() => {}),
}))

vi.mock('@nextcloud/files', () => ({
formatFileSize: (size) => size,
Header: class {},
}))
vi.mock('@nextcloud/dialogs', () => ({
FilePickerType: {},
getFilePickerBuilder: () => {},
}))

global.OC = {
requestToken: '123',
coreApps: [
'core',
],
config: {
modRewriteWorking: true,
},
dialogs: {
},
isUserAdmin() {
return true
},
getLanguage() {
return 'en-GB'
},
getLocale() {
return 'en_GB'
},

MimeType: {
getIconUrl: vi.fn(),
},
L10N: {
translate: global.t,
},
}

global.OCA = {}
global._oc_webroot = ''
import './testHelpers/jsdomMocks.js'
import './testHelpers/nextcloudMocks.js'

Vue.prototype.t = global.t
Vue.prototype.n = global.n
Vue.prototype.OC = OC
Vue.prototype.OCA = OCA

// Mock ClipboardEvent and DragEvent as long as jsdom is used
// https://github.com/ueberdosis/tiptap/issues/4455
class ClipboardEventMock extends Event {

constructor(type, eventInitDict) {
super(type, eventInitDict)
this.clipboardData = {
getData: vi.fn(),
setData: vi.fn(),
}
}

}
global.ClipboardEvent = ClipboardEventMock

class DragEventMock extends Event {

constructor(type, eventInitDict) {
super(type, eventInitDict)
this.dataTransfer = {
getData: vi.fn(),
setData: vi.fn(),
}
}

}
global.DragEvent = DragEventMock
Vue.prototype.OC = global.OC
Vue.prototype.OCA = global.OCA
54 changes: 54 additions & 0 deletions src/tests/testHelpers/jsdomMocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

// Mocks for browser APIs missing in jsdom

import { vi } from 'vitest'

// Mock ClipboardEvent and DragEvent as long as jsdom is used
// https://github.com/ueberdosis/tiptap/issues/4455

class ClipboardEventMock extends Event {

constructor(type, eventInitDict) {
super(type, eventInitDict)
this.clipboardData = {
getData: vi.fn(),
setData: vi.fn(),
}
}

}

global.ClipboardEvent = ClipboardEventMock

class DragEventMock extends Event {

constructor(type, eventInitDict) {
super(type, eventInitDict)
this.dataTransfer = {
getData: vi.fn(),
setData: vi.fn(),
}
}

}
global.DragEvent = DragEventMock

// https://github.com/jsdom/jsdom/issues/3002
Range.prototype.getBoundingClientRect = vi.fn()
Range.prototype.getBoundingClientRect.mockReturnValue({
bottom: 0,
height: 0,
left: 0,
right: 0,
top: 0,
width: 0,
})
Range.prototype.getClientRects = () => ({
item: vi.fn(),
length: 0,
[Symbol.iterator]: vi.fn(),
})
59 changes: 59 additions & 0 deletions src/tests/testHelpers/nextcloudMocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { vi } from 'vitest'

global.t = vi.fn().mockImplementation((app, text) => text)
global.n = vi.fn().mockImplementation((app, text) => text)

vi.mock('@nextcloud/auth', () => ({
getCurrentUser: vi.fn().mockImplementation(() => ({
uid: 'user1',
displayName: 'User 1',
isAdmin: false,
})),
getRequestToken: vi.fn().mockImplementation(() => '123456abcdef'),
onRequestTokenUpdate: vi.fn().mockImplementation(() => {}),
}))

vi.mock('@nextcloud/files', () => ({
formatFileSize: (size) => size,
Header: class {},
}))
vi.mock('@nextcloud/dialogs', () => ({
FilePickerType: {},
getFilePickerBuilder: () => {},
}))

global.OC = {
requestToken: '123',
coreApps: [
'core',
],
config: {
modRewriteWorking: true,
},
dialogs: {
},
isUserAdmin() {
return true
},
getLanguage() {
return 'en-GB'
},
getLocale() {
return 'en_GB'
},

MimeType: {
getIconUrl: vi.fn(),
},
L10N: {
translate: global.t,
},
}

global.OCA = {}
global._oc_webroot = ''

0 comments on commit e8960cb

Please sign in to comment.