-
-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#833@trivial: Adds unit tests for Clipboard.
- Loading branch information
1 parent
83f5269
commit 3b97d1a
Showing
7 changed files
with
209 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
enum PermissionNameEnum { | ||
geolocation = 'geolocation', | ||
notifications = 'notifications', | ||
push = 'push', | ||
midi = 'midi', | ||
camera = 'camera', | ||
microphone = 'microphone', | ||
backgroundFetch = 'background-fetch', | ||
backgroundSync = 'background-sync', | ||
persistentStorage = 'persistent-storage', | ||
ambientLightSensor = 'ambient-light-sensor', | ||
accelerometer = 'accelerometer', | ||
gyroscope = 'gyroscope', | ||
magnetometer = 'magnetometer', | ||
screenWakeLock = 'screen-wake-lock', | ||
nfc = 'nfc', | ||
displayCapture = 'display-capture', | ||
accessibilityEvents = 'accessibility-events', | ||
clipboardRead = 'clipboard-read', | ||
clipboardWrite = 'clipboard-write', | ||
paymentHandler = 'payment-handler', | ||
idleDetection = 'idle-detection', | ||
periodicBackgroundSync = 'periodic-background-sync', | ||
systemWakeLock = 'system-wake-lock', | ||
storageAccess = 'storage-access', | ||
windowManagement = 'window-management', | ||
windowPlacement = 'window-placement', | ||
localFonts = 'local-fonts', | ||
topLevelStorageAccess = 'top-level-storage-access' | ||
} | ||
|
||
export default PermissionNameEnum; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import ClipboardItem from '../../src/clipboard/ClipboardItem.js'; | ||
import Blob from '../../src/file/Blob.js'; | ||
import Window from '../../src/window/Window.js'; | ||
import IWindow from '../../src/window/IWindow.js'; | ||
import { beforeEach, describe, it, expect } from 'vitest'; | ||
|
||
describe('Clipboard', () => { | ||
let window: IWindow; | ||
|
||
beforeEach(() => { | ||
window = new Window(); | ||
}); | ||
|
||
describe('read()', () => { | ||
it('Reads from the clipboard.', async () => { | ||
const items = [ | ||
new ClipboardItem({ | ||
'text/plain': new Blob(['test'], { type: 'text/plain' }) | ||
}), | ||
new ClipboardItem({ | ||
'text/html': new Blob(['<b>test</b>'], { type: 'text/html' }) | ||
}) | ||
]; | ||
await window.navigator.clipboard.write(items); | ||
const data = await window.navigator.clipboard.read(); | ||
expect(data).toEqual(items); | ||
}); | ||
|
||
it('Throws an error if the permission is denied.', async () => { | ||
const permissionStatus = await window.navigator.permissions.query({ | ||
name: 'clipboard-read' | ||
}); | ||
permissionStatus.state = 'denied'; | ||
|
||
let error: Error | null = null; | ||
|
||
try { | ||
await window.navigator.clipboard.read(); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error?.message).toBe( | ||
"Failed to execute 'read' on 'Clipboard': The request is not allowed" | ||
); | ||
}); | ||
}); | ||
|
||
describe('readText()', () => { | ||
it('Reads text from the clipboard.', async () => { | ||
const items = [ | ||
new ClipboardItem({ | ||
'text/plain': new Blob(['test'], { type: 'text/plain' }) | ||
}), | ||
new ClipboardItem({ | ||
'text/html': new Blob(['<b>test</b>'], { type: 'text/html' }) | ||
}) | ||
]; | ||
await window.navigator.clipboard.write(items); | ||
const data = await window.navigator.clipboard.readText(); | ||
expect(data).toBe('test'); | ||
}); | ||
|
||
it('Throws an error if the permission is denied.', async () => { | ||
const permissionStatus = await window.navigator.permissions.query({ | ||
name: 'clipboard-read' | ||
}); | ||
permissionStatus.state = 'denied'; | ||
|
||
let error: Error | null = null; | ||
|
||
try { | ||
await window.navigator.clipboard.readText(); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error?.message).toBe( | ||
"Failed to execute 'readText' on 'Clipboard': The request is not allowed" | ||
); | ||
}); | ||
}); | ||
|
||
describe('write()', () => { | ||
it('Writes to the clipboard.', async () => { | ||
const items = [ | ||
new ClipboardItem({ | ||
'text/plain': new Blob(['test'], { type: 'text/plain' }) | ||
}), | ||
new ClipboardItem({ | ||
'text/html': new Blob(['<b>test</b>'], { type: 'text/html' }) | ||
}) | ||
]; | ||
await window.navigator.clipboard.write(items); | ||
const data = await window.navigator.clipboard.read(); | ||
expect(data).toEqual(items); | ||
}); | ||
|
||
it('Throws an error if the permission is denied.', async () => { | ||
const permissionStatus = await window.navigator.permissions.query({ | ||
name: 'clipboard-write' | ||
}); | ||
permissionStatus.state = 'denied'; | ||
|
||
let error: Error | null = null; | ||
|
||
try { | ||
await window.navigator.clipboard.write([]); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error?.message).toBe( | ||
"Failed to execute 'write' on 'Clipboard': The request is not allowed" | ||
); | ||
}); | ||
}); | ||
|
||
describe('writeText()', () => { | ||
it('Writes text to the clipboard.', async () => { | ||
const text = 'test'; | ||
await window.navigator.clipboard.writeText(text); | ||
const data = await window.navigator.clipboard.readText(); | ||
expect(data).toBe(text); | ||
}); | ||
|
||
it('Throws an error if the permission is denied.', async () => { | ||
const permissionStatus = await window.navigator.permissions.query({ | ||
name: 'clipboard-write' | ||
}); | ||
permissionStatus.state = 'denied'; | ||
|
||
let error: Error | null = null; | ||
|
||
try { | ||
await window.navigator.clipboard.writeText('test'); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error?.message).toBe( | ||
"Failed to execute 'writeText' on 'Clipboard': The request is not allowed" | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters