File: yfile.spec.ts

package info (click to toggle)
jupyterlab 4.0.11%2Bds1%2B~cs11.25.27-7
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 43,496 kB
  • sloc: javascript: 18,395; python: 8,932; sh: 399; makefile: 95; perl: 33; xml: 1
file content (137 lines) | stat: -rw-r--r-- 3,310 bytes parent folder | download | duplicates (2)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import { Delta, YFile } from '../src';

describe('@jupyter/ydoc', () => {
  describe('YFile', () => {
    describe('#constructor', () => {
      test('should create a document without arguments', () => {
        const file = new YFile();
        expect(file.source.length).toBe(0);
        file.dispose();
      });
    });

    describe('#disposed', () => {
      test('should be emitted when the document is disposed', () => {
        const file = new YFile();
        let disposed = false;
        file.disposed.connect(() => {
          disposed = true;
        });
        file.dispose();
        expect(disposed).toEqual(true);
      });
    });

    describe('source', () => {
      test('should set source', () => {
        const file = new YFile();

        const source = 'foo';
        file.setSource(source);

        expect(file.source).toEqual(source);
        file.dispose();
      });

      test('should get source', () => {
        const file = new YFile();

        const source = 'foo';
        file.setSource(source);

        expect(file.getSource()).toEqual(source);
        file.dispose();
      });

      test('should update source', () => {
        const file = new YFile();

        const source = 'fooo bar';
        file.setSource(source);
        expect(file.source).toBe(source);

        file.updateSource(3, 5, '/');
        expect(file.source).toBe('foo/bar');
        file.dispose();
      });

      test('should emit an insert source change', () => {
        const file = new YFile();
        expect(file.source).toBe('');

        const changes: Delta<string>[] = [];
        file.changed.connect((_, c) => {
          changes.push(c.sourceChange!);
        });
        const source = 'foo';
        file.setSource(source);

        expect(changes).toHaveLength(1);
        expect(changes).toEqual([
          [
            {
              insert: 'foo'
            }
          ]
        ]);

        file.dispose();
      });

      test('should emit a delete source change', () => {
        const file = new YFile();
        const source = 'foo';
        file.setSource(source);
        expect(file.source).toBe(source);

        const changes: Delta<string>[] = [];
        file.changed.connect((_, c) => {
          changes.push(c.sourceChange!);
        });
        file.setSource('');

        expect(changes).toHaveLength(1);
        expect(changes).toEqual([
          [
            {
              delete: 3
            }
          ]
        ]);

        file.dispose();
      });

      test('should emit an update source change', () => {
        const file = new YFile();
        const source1 = 'foo';
        file.setSource(source1);
        expect(file.source).toBe(source1);

        const changes: Delta<string>[] = [];
        file.changed.connect((_, c) => {
          changes.push(c.sourceChange!);
        });
        const source = 'bar';
        file.setSource(source);

        expect(changes).toHaveLength(1);
        expect(changes).toEqual([
          [
            {
              delete: 3
            },
            {
              insert: source
            }
          ]
        ]);

        file.dispose();
      });
    });
  });
});