forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
observablestring.spec.ts
163 lines (146 loc) · 5.17 KB
/
observablestring.spec.ts
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { expect } from 'chai';
import { ObservableString } from '@jupyterlab/observables/src';
describe('@jupyterlab/observables', () => {
describe('ObservableString', () => {
describe('#constructor()', () => {
it('should accept no arguments', () => {
const value = new ObservableString();
expect(value instanceof ObservableString).to.equal(true);
});
it('should accept a string argument', () => {
const value = new ObservableString('hello');
expect(value instanceof ObservableString).to.equal(true);
});
it('should initialize the string value', () => {
const value = new ObservableString('hello');
expect(value.text).to.deep.equal('hello');
});
});
describe('#type', () => {
it('should return `String`', () => {
const value = new ObservableString();
expect(value.type).to.equal('String');
});
});
describe('#changed', () => {
it('should be emitted when the string changes', () => {
let called = false;
const value = new ObservableString();
value.changed.connect(() => {
called = true;
});
value.text = 'change';
expect(called).to.equal(true);
});
it('should have value changed args', () => {
let called = false;
const value = new ObservableString();
value.changed.connect((sender, args) => {
expect(sender).to.equal(value);
expect(args.type).to.equal('set');
expect(args.start).to.equal(0);
expect(args.end).to.equal(3);
expect(args.value).to.equal('new');
called = true;
});
value.text = 'new';
expect(called).to.equal(true);
});
});
describe('#isDisposed', () => {
it('should test whether the string is disposed', () => {
const value = new ObservableString();
expect(value.isDisposed).to.equal(false);
value.dispose();
expect(value.isDisposed).to.equal(true);
});
});
describe('#setter()', () => {
it('should set the item at a specific index', () => {
const value = new ObservableString('old');
value.text = 'new';
expect(value.text).to.deep.equal('new');
});
it('should trigger a changed signal', () => {
let called = false;
const value = new ObservableString('old');
value.changed.connect((sender, args) => {
expect(sender).to.equal(value);
expect(args.type).to.equal('set');
expect(args.start).to.equal(0);
expect(args.end).to.equal(3);
expect(args.value).to.equal('new');
called = true;
});
value.text = 'new';
expect(called).to.equal(true);
});
});
describe('#insert()', () => {
it('should insert an substring into the string at a specific index', () => {
const value = new ObservableString('one three');
value.insert(4, 'two ');
expect(value.text).to.deep.equal('one two three');
});
it('should trigger a changed signal', () => {
let called = false;
const value = new ObservableString('one three');
value.changed.connect((sender, args) => {
expect(sender).to.equal(value);
expect(args.type).to.equal('insert');
expect(args.start).to.equal(4);
expect(args.end).to.equal(8);
expect(args.value).to.equal('two ');
called = true;
});
value.insert(4, 'two ');
expect(called).to.equal(true);
});
});
describe('#remove()', () => {
it('should remove a substring from the string', () => {
const value = new ObservableString('one two two three');
value.remove(4, 8);
expect(value.text).to.deep.equal('one two three');
});
it('should trigger a changed signal', () => {
let called = false;
const value = new ObservableString('one two two three');
value.changed.connect((sender, args) => {
expect(sender).to.equal(value);
expect(args.type).to.equal('remove');
expect(args.start).to.equal(4);
expect(args.end).to.equal(8);
expect(args.value).to.equal('two ');
called = true;
});
value.remove(4, 8);
expect(called).to.equal(true);
});
});
describe('#clear()', () => {
it('should empty the string', () => {
const value = new ObservableString('full');
value.clear();
expect(value.text.length).to.equal(0);
expect(value.text).to.equal('');
});
it('should trigger a changed signal', () => {
let called = false;
const value = new ObservableString('full');
value.changed.connect((sender, args) => {
expect(sender).to.equal(value);
expect(args.type).to.equal('set');
expect(args.start).to.equal(0);
expect(args.end).to.equal(0);
expect(args.value).to.equal('');
called = true;
});
value.clear();
expect(called).to.equal(true);
});
});
});
});