WXL
3 天以前 2cc85c64f1c64a2dbaeae276a3e2ca8420de76b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import ClipboardActionDefault from '../../src/actions/default';
 
describe('ClipboardActionDefault', () => {
  before(() => {
    global.input = document.createElement('input');
    global.input.setAttribute('id', 'input');
    global.input.setAttribute('value', 'abc');
    document.body.appendChild(global.input);
 
    global.paragraph = document.createElement('p');
    global.paragraph.setAttribute('id', 'paragraph');
    global.paragraph.textContent = 'abc';
    document.body.appendChild(global.paragraph);
  });
 
  after(() => {
    document.body.innerHTML = '';
  });
 
  describe('#resolveOptions', () => {
    it('should set base properties', () => {
      const selectedText = ClipboardActionDefault({
        container: document.body,
        text: 'foo',
      });
 
      assert.equal(selectedText, 'foo');
    });
  });
 
  describe('#set action', () => {
    it('should throw an error since "action" is invalid', (done) => {
      try {
        let clip = ClipboardActionDefault({
          text: 'foo',
          action: 'paste',
        });
      } catch (e) {
        assert.equal(
          e.message,
          'Invalid "action" value, use either "copy" or "cut"'
        );
        done();
      }
    });
  });
 
  describe('#set target', () => {
    it('should throw an error since "target" do not match any element', (done) => {
      try {
        let clip = ClipboardActionDefault({
          target: document.querySelector('#foo'),
        });
      } catch (e) {
        assert.equal(e.message, 'Invalid "target" value, use a valid Element');
        done();
      }
    });
  });
 
  describe('#selectedText', () => {
    it('should select text from editable element', () => {
      const selectedText = ClipboardActionDefault({
        container: document.body,
        target: document.querySelector('#input'),
      });
 
      assert.equal(selectedText, 'abc');
    });
 
    it('should select text from non-editable element', () => {
      const selectedText = ClipboardActionDefault({
        container: document.body,
        target: document.querySelector('#paragraph'),
      });
 
      assert.equal(selectedText, 'abc');
    });
  });
});