jest-clipboard
Advanced tools
Comparing version 0.0.10 to 0.0.11
@@ -37,3 +37,76 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}; | ||
import { Blob } from 'buffer'; | ||
import 'blob-polyfill'; | ||
var Clipboard = /** @class */ (function () { | ||
function Clipboard() { | ||
this.clipboardItems = []; | ||
} | ||
Clipboard.prototype.write = function (data) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _i, data_1, clipboardItem, type; | ||
return __generator(this, function (_a) { | ||
for (_i = 0, data_1 = data; _i < data_1.length; _i++) { | ||
clipboardItem = data_1[_i]; | ||
for (type in clipboardItem) { | ||
this.clipboardItems = [clipboardItem]; | ||
} | ||
} | ||
return [2 /*return*/, Promise.resolve()]; | ||
}); | ||
}); | ||
}; | ||
Clipboard.prototype.writeText = function (text) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var clipboardItem; | ||
return __generator(this, function (_a) { | ||
clipboardItem = { | ||
presentationStyle: 'inline', | ||
types: ['text/plain'], | ||
getType: function (type) { | ||
return new Promise(function (resolve) { | ||
resolve(new Blob([text], { type: 'text/plain' })); | ||
}); | ||
} | ||
}; | ||
this.clipboardItems = [clipboardItem]; | ||
return [2 /*return*/, text]; | ||
}); | ||
}); | ||
}; | ||
Clipboard.prototype.read = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, Promise.resolve(this.clipboardItems)]; | ||
}); | ||
}); | ||
}; | ||
Clipboard.prototype.readText = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var blob; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (this.clipboardItems.length === 0) { | ||
return [2 /*return*/, Promise.resolve('')]; | ||
} | ||
return [4 /*yield*/, this.clipboardItems[0].getType('text/plain')]; | ||
case 1: | ||
blob = _a.sent(); | ||
return [2 /*return*/, blob.text()]; | ||
} | ||
}); | ||
}); | ||
}; | ||
return Clipboard; | ||
}()); | ||
var clipboard = new Clipboard(); | ||
export function setUpClipboard() { | ||
Object.assign(global.navigator, { | ||
clipboard: clipboard, | ||
}); | ||
} | ||
export function tearDownClipboard() { | ||
Object.assign(global.navigator, { | ||
clipboard: null | ||
}); | ||
} | ||
export var writeTextToClipboard = function (writeToClipboard) { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -43,3 +116,8 @@ return __generator(this, function (_a) { | ||
case 0: return [4 /*yield*/, navigator.clipboard.writeText(writeToClipboard)]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
case 1: | ||
_a.sent(); | ||
return [4 /*yield*/, clipboard.writeText(writeToClipboard)]; | ||
case 2: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
@@ -49,15 +127,40 @@ }); | ||
export var writeToClipboard = function (text) { return __awaiter(void 0, void 0, void 0, function () { | ||
var myBlob, clipboardItem; | ||
var a, data; | ||
return __generator(this, function (_a) { | ||
myBlob = new Blob([text], { type: 'text/plain' }); | ||
clipboardItem = { | ||
'text/plain': myBlob, | ||
}; | ||
// @ts-ignore | ||
return [2 /*return*/, navigator.clipboard.write([clipboardItem])]; | ||
switch (_a.label) { | ||
case 0: | ||
a = { | ||
presentationStyle: 'inline', | ||
types: ['text/plain'], | ||
getType: function (type) { | ||
var myBlob = new Blob([text], { type: 'text/plain' }); | ||
return Promise.resolve(myBlob); | ||
}, | ||
}; | ||
data = [a]; | ||
return [4 /*yield*/, clipboard.write(data)]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/, navigator.clipboard.write(data)]; | ||
} | ||
}); | ||
}); }; | ||
export var writeItemsToClipboard = function (items) { return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, clipboard.write(items)]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/, navigator.clipboard.write(items)]; | ||
} | ||
}); | ||
}); }; | ||
export var readFromClipboard = function () { return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, navigator.clipboard.read()]; | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, navigator.clipboard.read()]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/, clipboard.read()]; | ||
} | ||
}); | ||
@@ -69,61 +172,7 @@ }); }; | ||
case 0: return [4 /*yield*/, navigator.clipboard.readText()]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/, clipboard.readText()]; | ||
} | ||
}); | ||
}); }; | ||
export function setUpClipboard() { | ||
var clipboardItems = [new Blob([], { type: 'text/plain' })]; | ||
Object.assign(global.navigator, { | ||
clipboard: { | ||
write: function (data) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _i, data_1, clipboardItem, type, clipboardItemElement; | ||
return __generator(this, function (_a) { | ||
for (_i = 0, data_1 = data; _i < data_1.length; _i++) { | ||
clipboardItem = data_1[_i]; | ||
for (type in clipboardItem) { | ||
clipboardItemElement = clipboardItem[type]; | ||
clipboardItems = [clipboardItemElement]; | ||
} | ||
} | ||
return [2 /*return*/, Promise.resolve()]; | ||
}); | ||
}); | ||
}, | ||
writeText: function (text) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
clipboardItems = [new Blob([text], { type: 'text/plain' })]; | ||
return [2 /*return*/, text]; | ||
}); | ||
}); | ||
}, | ||
read: function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a; | ||
return __generator(this, function (_b) { | ||
return [2 /*return*/, Promise.resolve([ | ||
(_a = {}, | ||
_a[clipboardItems[0].type] = clipboardItems[0], | ||
_a.types = [clipboardItems[0].type], | ||
_a.getType = function () { return clipboardItems[0]; }, | ||
_a) | ||
])]; | ||
}); | ||
}); | ||
}, | ||
readText: function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, Promise.resolve(clipboardItems[0].text())]; | ||
}); | ||
}); | ||
}, | ||
}, | ||
}); | ||
} | ||
export function tearDownClipboard() { | ||
Object.assign(global.navigator, { | ||
clipboard: null | ||
}); | ||
} |
@@ -37,3 +37,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}; | ||
import { readFromClipboard, readTextFromClipboard, setUpClipboard, tearDownClipboard, writeTextToClipboard, writeToClipboard } from './clipboard'; | ||
import { readFromClipboard, readTextFromClipboard, setUpClipboard, tearDownClipboard, writeItemsToClipboard, writeTextToClipboard, writeToClipboard } from './clipboard'; | ||
describe('Clipboard', function () { | ||
@@ -73,3 +73,3 @@ beforeEach(function () { | ||
}); }); | ||
it('write to clipboard (write)', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
it('write text to clipboard with write method', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var items, type1, _a; | ||
@@ -131,2 +131,37 @@ return __generator(this, function (_b) { | ||
}); }); | ||
describe('write', function () { | ||
it('should write image to clipboard', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var imagePng, base64Image, buffer, blob, clipboardItem, clipboardItems, items, type1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
imagePng = 'image/png'; | ||
base64Image = 'iVBORw0KGgoAAAANSUhEUgAAAEYAAABACAIAAAAoFZbOAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAaElEQVRoge3PQQ3AIADAQMAQD4J/azOxZOlyp6Cd+9zxL+vrgPdZKrBUYKnAUoGlAksFlgosFVgqsFRgqcBSgaUCSwWWCiwVWCqwVGCpwFKBpQJLBZYKLBVYKrBUYKnAUoGlAksFlgoeg2ABFxfCv1QAAAAASUVORK5CYII='; | ||
buffer = Buffer.from(base64Image, 'base64'); | ||
blob = new Blob([buffer]); | ||
clipboardItem = { | ||
presentationStyle: 'inline', | ||
types: [imagePng], | ||
getType: function (type) { | ||
return new Promise(function (resolve) { | ||
resolve(blob); | ||
}); | ||
} | ||
}; | ||
clipboardItems = [clipboardItem]; | ||
return [4 /*yield*/, writeItemsToClipboard(clipboardItems)]; | ||
case 1: | ||
_a.sent(); | ||
return [4 /*yield*/, readFromClipboard()]; | ||
case 2: | ||
items = _a.sent(); | ||
return [4 /*yield*/, items[0].getType(imagePng)]; | ||
case 3: | ||
type1 = _a.sent(); | ||
expect(type1.size).toBe(182); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
}); | ||
}); |
@@ -0,6 +1,8 @@ | ||
import 'blob-polyfill'; | ||
export declare function setUpClipboard(): void; | ||
export declare function tearDownClipboard(): void; | ||
export declare const writeTextToClipboard: (writeToClipboard: string) => Promise<void>; | ||
export declare const writeToClipboard: (text: string) => Promise<void>; | ||
export declare const writeItemsToClipboard: (items: ClipboardItems) => Promise<void>; | ||
export declare const readFromClipboard: () => Promise<ClipboardItems>; | ||
export declare const readTextFromClipboard: () => Promise<string>; | ||
export declare function setUpClipboard(): void; | ||
export declare function tearDownClipboard(): void; |
{ | ||
"name": "jest-clipboard", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "jest-clipboard provides a easy way to test code against the clipboard API.", | ||
@@ -33,2 +33,5 @@ "main": "dist/clipboard.js", | ||
"homepage": "https://github.com/marabesi/jest-clipboard#readme", | ||
"dependencies": { | ||
"blob-polyfill": "^7.0.20220408" | ||
}, | ||
"devDependencies": { | ||
@@ -42,4 +45,4 @@ "@types/jest": "^29.0.3", | ||
"ts-jest": "^29.0.2", | ||
"typescript": "^4.8.3" | ||
"typescript": "^5.0.2" | ||
} | ||
} |
@@ -5,3 +5,3 @@ import { | ||
setUpClipboard, | ||
tearDownClipboard, | ||
tearDownClipboard, writeItemsToClipboard, | ||
writeTextToClipboard, | ||
@@ -34,3 +34,3 @@ writeToClipboard | ||
it('write to clipboard (write)', async () => { | ||
it('write text to clipboard with write method', async () => { | ||
await writeToClipboard('this is a blob'); | ||
@@ -50,3 +50,3 @@ | ||
expect(await actual.text()).toBe('text from clipboard'); | ||
}) | ||
}); | ||
@@ -58,3 +58,31 @@ it('should read from clipboard (readText)', async () => { | ||
expect(readFromClipboard).toBe('text from clipboard'); | ||
}) | ||
}); | ||
describe('write', () => { | ||
it('should write image to clipboard', async () => { | ||
const imagePng = 'image/png'; | ||
const base64Image ='iVBORw0KGgoAAAANSUhEUgAAAEYAAABACAIAAAAoFZbOAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAaElEQVRoge3PQQ3AIADAQMAQD4J/azOxZOlyp6Cd+9zxL+vrgPdZKrBUYKnAUoGlAksFlgosFVgqsFRgqcBSgaUCSwWWCiwVWCqwVGCpwFKBpQJLBZYKLBVYKrBUYKnAUoGlAksFlgoeg2ABFxfCv1QAAAAASUVORK5CYII=' | ||
const buffer = Buffer.from(base64Image, 'base64'); | ||
const blob = new Blob([buffer]); | ||
const clipboardItem: ClipboardItem = { | ||
presentationStyle: 'inline', | ||
types: [imagePng], | ||
getType(type: string): Promise<Blob> { | ||
return new Promise((resolve) => { | ||
resolve(blob) | ||
}); | ||
} | ||
}; | ||
const clipboardItems: ClipboardItems = [clipboardItem] | ||
await writeItemsToClipboard(clipboardItems); | ||
const items = await readFromClipboard(); | ||
const type1 = await items[0].getType(imagePng); | ||
expect(type1.size).toBe(182); | ||
}); | ||
}); | ||
}); |
@@ -1,61 +0,51 @@ | ||
import { Blob } from 'buffer'; | ||
import 'blob-polyfill'; | ||
export const writeTextToClipboard = async (writeToClipboard: string) => { | ||
return await navigator.clipboard.writeText(writeToClipboard); | ||
}; | ||
class Clipboard { | ||
private clipboardItems: ClipboardItem[] = [] | ||
export const writeToClipboard = async (text: string) => { | ||
const myBlob = new Blob([text], { type: 'text/plain' }) | ||
const clipboardItem = { | ||
'text/plain': myBlob, | ||
}; | ||
async write(data: ClipboardItems): Promise<void> { | ||
for (const clipboardItem of data) { | ||
for (const type in clipboardItem) { | ||
this.clipboardItems = [clipboardItem] | ||
} | ||
} | ||
// @ts-ignore | ||
return navigator.clipboard.write([ clipboardItem ]); | ||
}; | ||
return Promise.resolve() | ||
} | ||
export const readFromClipboard = async(): Promise<ClipboardItems> => { | ||
return navigator.clipboard.read(); | ||
} | ||
async writeText(text: string): Promise<string> { | ||
const clipboardItem: ClipboardItem = { | ||
presentationStyle: 'inline', | ||
types: ['text/plain'], | ||
getType(type: string): Promise<Blob> { | ||
return new Promise((resolve) => { | ||
resolve(new Blob([text], { type: 'text/plain' })) | ||
}); | ||
} | ||
}; | ||
this.clipboardItems = [clipboardItem]; | ||
export const readTextFromClipboard = async () => { | ||
return await navigator.clipboard.readText() | ||
return text; | ||
} | ||
async read() { | ||
return Promise.resolve(this.clipboardItems); | ||
} | ||
async readText(): Promise<string> { | ||
if (this.clipboardItems.length === 0) { | ||
return Promise.resolve('') | ||
} | ||
const blob = await this.clipboardItems[0].getType('text/plain'); | ||
return blob.text(); | ||
} | ||
} | ||
const clipboard = new Clipboard(); | ||
export function setUpClipboard() { | ||
let clipboardItems = [new Blob([], { type: 'text/plain' })] | ||
Object.assign(global.navigator, | ||
{ | ||
clipboard: { | ||
async write(data: ClipboardItems): Promise<void> { | ||
for (const clipboardItem of data) { | ||
for (const type in clipboardItem) { | ||
// @ts-ignore | ||
const clipboardItemElement: Blob = clipboardItem[type]; | ||
clipboardItems = [clipboardItemElement] | ||
} | ||
} | ||
return Promise.resolve() | ||
}, | ||
async writeText(text: string) { | ||
clipboardItems = [new Blob([text], { type: 'text/plain' })] | ||
return text; | ||
}, | ||
async read() { | ||
return Promise.resolve([ | ||
{ | ||
[clipboardItems[0].type]: clipboardItems[0], | ||
types: [ clipboardItems[0].type ], | ||
getType: () => clipboardItems[0] | ||
} | ||
]); | ||
}, | ||
async readText(): Promise<string> { | ||
return Promise.resolve( | ||
clipboardItems[0].text() | ||
) | ||
}, | ||
}, | ||
clipboard, | ||
}); | ||
@@ -69,1 +59,40 @@ } | ||
} | ||
export const writeTextToClipboard = async (writeToClipboard: string) => { | ||
await navigator.clipboard.writeText(writeToClipboard); | ||
await clipboard.writeText(writeToClipboard); | ||
}; | ||
export const writeToClipboard = async (text: string) => { | ||
const a: ClipboardItem = { | ||
presentationStyle: 'inline', | ||
types: ['text/plain'], | ||
getType(type: string): Promise<Blob> { | ||
const myBlob = new Blob([text], { type: 'text/plain' }) | ||
return Promise.resolve(myBlob); | ||
}, | ||
}; | ||
const data = [a]; | ||
await clipboard.write(data); | ||
return navigator.clipboard.write(data); | ||
}; | ||
export const writeItemsToClipboard = async (items: ClipboardItems) => { | ||
await clipboard.write(items); | ||
return navigator.clipboard.write(items); | ||
}; | ||
export const readFromClipboard = async(): Promise<ClipboardItems> => { | ||
await navigator.clipboard.read(); | ||
return clipboard.read(); | ||
} | ||
export const readTextFromClipboard = async () => { | ||
await navigator.clipboard.readText() | ||
return clipboard.readText() | ||
} |
Sorry, the diff of this file is not supported yet
26651
542
1
+ Addedblob-polyfill@^7.0.20220408
+ Addedblob-polyfill@7.0.20220408(transitive)