WXL
9 天以前 2895b4ea66e09cb355aeb4e030ca0de297bf8ce3
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
import ClipboardActionCopy from '../../src/actions/copy';
 
describe('ClipboardActionCopy', () => {
  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('#selectText', () => {
    it('should select its value based on input target', () => {
      const selectedText = ClipboardActionCopy(
        document.querySelector('#input'),
        {
          container: document.body,
        }
      );
 
      assert.equal(selectedText, document.querySelector('#input').value);
    });
 
    it('should select its value based on element target', () => {
      const selectedText = ClipboardActionCopy(
        document.querySelector('#paragraph'),
        {
          container: document.body,
        }
      );
 
      assert.equal(
        selectedText,
        document.querySelector('#paragraph').textContent
      );
    });
 
    it('should select its value based on text', () => {
      const text = 'abc';
      const selectedText = ClipboardActionCopy(text, {
        container: document.body,
      });
 
      assert.equal(selectedText, text);
    });
 
    it('should select its value in a input number based on text', () => {
      const value = 1;
      document.querySelector('#input').setAttribute('type', 'number');
      document.querySelector('#input').setAttribute('value', value);
      const selectedText = ClipboardActionCopy(
        document.querySelector('#input'),
        {
          container: document.body,
        }
      );
 
      assert.equal(Number(selectedText), value);
    });
  });
});