Skip to content

Commit df07bd7

Browse files
committed
Add tests
1 parent 64317f4 commit df07bd7

3 files changed

Lines changed: 99 additions & 0 deletions

File tree

karma.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = function(config) {
2+
config.set({
3+
frameworks: ['mocha', 'chai'],
4+
files: [{pattern: 'dist/index.esm.js', type: 'module'}, {pattern: 'test/test.js', type: 'module'}],
5+
reporters: ['mocha'],
6+
port: 9876,
7+
colors: true,
8+
logLevel: config.LOG_INFO,
9+
browsers: ['ChromeHeadless'],
10+
autoWatch: false,
11+
singleRun: true,
12+
concurrency: Infinity
13+
})
14+
}

test/.eslintrc.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"rules": {
3+
"flowtype/require-valid-file-annotation": "off"
4+
},
5+
"env": {
6+
"mocha": true
7+
},
8+
"globals": {
9+
"assert": true
10+
},
11+
"extends": "../.eslintrc.json"
12+
}

test/test.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import subscribe from '../dist/index.esm.js'
2+
3+
describe('paste-markdown', function() {
4+
describe('with quotable selection', function() {
5+
let subscription, textarea
6+
beforeEach(function() {
7+
document.body.innerHTML = `
8+
<textarea data-paste-markdown></textarea>
9+
`
10+
11+
textarea = document.querySelector('textarea[data-paste-markdown]')
12+
subscription = subscribe(textarea)
13+
})
14+
15+
afterEach(function() {
16+
subscription.unsubscribe()
17+
document.body.innerHTML = ''
18+
})
19+
20+
it('pastes image uris as markdown', function() {
21+
paste(textarea, {'text/uri-list': 'https://github.com/github.png\r\nhttps://github.com/hubot.png'})
22+
assert.include(textarea.value, '![](https://github.com/github.png)\n\n![](https://github.com/hubot.png)')
23+
})
24+
25+
it('pastes gfm into markdown', function() {
26+
paste(textarea, {'text/plain': 'hello', 'text/x-gfm': '# hello'})
27+
assert.include(textarea.value, '# hello')
28+
})
29+
30+
it('pastes html tables as markdown', function() {
31+
const data = {
32+
'text/html': `
33+
<table>
34+
<thead><tr><th>name</th><th>origin</th></tr></thead>
35+
<tbody>
36+
<tr><td>hubot</td><td>github</td></tr>
37+
<tr><td>bender</td><td>futurama</td></tr>
38+
</tbody>
39+
</table>
40+
`
41+
}
42+
paste(textarea, data)
43+
assert.include(textarea.value, 'name | origin\n-- | --\nhubot | github\nbender | futurama')
44+
})
45+
46+
it('does not paste excluded content as markdown', function() {
47+
const data = {
48+
'text/html': `
49+
<table class="js-comment">
50+
<thead><tr><th>name</th><th>origin</th></tr></thead>
51+
<tbody>
52+
<tr><td>hubot</td><td>github</td></tr>
53+
<tr><td>bender</td><td>futurama</td></tr>
54+
</tbody>
55+
</table>
56+
`
57+
}
58+
paste(textarea, data)
59+
assert.equal(textarea.value, '')
60+
})
61+
})
62+
})
63+
64+
function paste(textarea, data) {
65+
const dataTransfer = new DataTransfer()
66+
for (const key in data) {
67+
dataTransfer.setData(key, data[key])
68+
}
69+
const event = new ClipboardEvent('paste', {
70+
clipboardData: dataTransfer
71+
})
72+
textarea.dispatchEvent(event)
73+
}

0 commit comments

Comments
 (0)