-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4a8bc3
commit c059cdc
Showing
4 changed files
with
103 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,19 +6,35 @@ on: | |
jobs: | ||
build-deploy: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: write | ||
id-token: write | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: 16 | ||
node-version: 18 | ||
registry-url: 'https://registry.npmjs.org' | ||
|
||
- run: npm install | ||
- run: npm run build | ||
- run: npm run coverage | ||
|
||
- run: npm install [email protected] -g | ||
- run: idoc -s "Copy Text to Clipboard {{version}}" | ||
|
||
- name: Create Coverage Badges | ||
uses: jaywcjlove/coverage-badges-cli@main | ||
with: | ||
output: dist/badges.svg | ||
|
||
- name: Generate Contributors Images | ||
uses: jaywcjlove/github-action-contributors@main | ||
with: | ||
filter-author: (renovate\[bot\]|renovate-bot|dependabot\[bot\]) | ||
output: dist/CONTRIBUTORS.svg | ||
avatarSize: 42 | ||
|
||
- name: Deploy | ||
uses: peaceiris/actions-gh-pages@v3 | ||
with: | ||
|
@@ -63,7 +79,7 @@ jobs: | |
# - run: npm-publish --token="${{ secrets.NPM_TOKEN }}" ./package.json | ||
|
||
- name: 📦 @uiw/copy-to-clipboard publish to NPM | ||
run: npm publish | ||
run: npm publish --access public --provenance | ||
continue-on-error: true | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | ||
|
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,69 @@ | ||
import copyTextToClipboard from '../src/main'; | ||
|
||
test('copyTextToClipboard should copy the text to the clipboard and return true', () => { | ||
const text = 'Hello, world!'; | ||
const copied = jest.fn(); | ||
document.execCommand = jest.fn(() => true); | ||
const cb = isCopy => { | ||
copied(isCopy); | ||
expect(isCopy).toBe(true); | ||
}; | ||
|
||
copyTextToClipboard(text, cb); | ||
|
||
expect(copied).toHaveBeenCalledWith(true); | ||
}); | ||
|
||
test('copyTextToClipboard should not copy the text to the clipboard and return false', () => { | ||
const text = 'Hello, world!'; | ||
const copied = jest.fn(); | ||
const cb = isCopy => { | ||
copied(isCopy); | ||
expect(isCopy).toBe(false); | ||
}; | ||
|
||
// Simulating an error during `document.execCommand('copy')` | ||
document.execCommand = jest.fn(() => { | ||
throw new Error('Copy failed'); | ||
}); | ||
|
||
copyTextToClipboard(text, cb); | ||
|
||
expect(copied).toHaveBeenCalledWith(false); | ||
}); | ||
|
||
|
||
it('should copy text to clipboard', () => { | ||
let mockCb = jest.fn(); | ||
const text = 'Hello, world!'; | ||
const mockRange = { | ||
commonAncestorContainer: 'mockContainer', | ||
startContainer: 'mockContainer', | ||
endContainer: 'mockContainer', | ||
startOffset: 0, | ||
endOffset: 0, | ||
}; | ||
|
||
document.execCommand = jest.fn(() => true); | ||
document.getSelection = jest.fn(() => ({ | ||
rangeCount: 1, | ||
getRangeAt: jest.fn(() => mockRange), | ||
removeAllRanges: jest.fn(), | ||
addRange: jest.fn(), | ||
})); | ||
document.createElement = jest.fn(() => ({ | ||
value: '', | ||
setAttribute: jest.fn(), | ||
select: jest.fn(), | ||
})); | ||
document.body.appendChild = jest.fn(); | ||
document.body.removeChild = jest.fn(); | ||
|
||
copyTextToClipboard(text, mockCb); | ||
|
||
expect(document.createElement).toHaveBeenCalledWith('textarea'); | ||
expect(document.body.appendChild).toHaveBeenCalled(); | ||
expect(document.execCommand).toHaveBeenCalledWith('copy'); | ||
expect(document.body.removeChild).toHaveBeenCalled(); | ||
expect(mockCb).toHaveBeenCalledWith(true); | ||
}); |