Skip to content

Commit

Permalink
test: add test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 4, 2023
1 parent c4a8bc3 commit c059cdc
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 3 deletions.
20 changes: 18 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 }}
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Copy Text to Clipboard
[![Open in unpkg](https://img.shields.io/badge/Open%20in-unpkg-blue)](https://uiwjs.github.io/npm-unpkg/#/pkg/@uiw/copy-to-clipboard/file/README.md)
[![npm version](https://img.shields.io/npm/v/@uiw/copy-to-clipboard.svg)](https://www.npmjs.com/package/@uiw/copy-to-clipboard)
[![Repo Dependents](https://badgen.net/github/dependents-repo/uiwjs/copy-to-clipboard)](https://github.com/uiwjs/copy-to-clipboard/network/dependents)
[![Coverage Status](https://uiwjs.github.io/copy-to-clipboard/badges.svg)](https://uiwjs.github.io/copy-to-clipboard/lcov-report/)

**This styling is an extra step which is likely not required.**

Expand Down Expand Up @@ -45,6 +46,16 @@ copyTextToClipboard('test', (isCopy) => {
</script>
```

## Contributors

As always, thanks to our amazing contributors!

<a href="https://github.com/uiwjs/copy-to-clipboard/graphs/contributors">
<img src="https://uiwjs.github.io/copy-to-clipboard/CONTRIBUTORS.svg" />
</a>

Made with [contributors](https://github.com/jaywcjlove/github-action-contributors).

### License

Licensed under the MIT License.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"homepage": "https://uiwjs.github.io/copy-to-clipboard",
"types": "index.d.ts",
"scripts": {
"test": "tsbb test",
"coverage": "tsbb test --coverage --bail",
"build": "rollup -c rollup.config.mjs",
"dev": "rollup -c rollup.config.mjs -w"
},
Expand Down Expand Up @@ -41,5 +43,7 @@
"bannerjs": "^3.0.1",
"rollup": "^3.20.2"
},
"dependencies": {}
"dependencies": {
"tsbb": "^4.2.3"
}
}
69 changes: 69 additions & 0 deletions test/main.test.js
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);
});

0 comments on commit c059cdc

Please sign in to comment.