This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy pathwatch-workspace-item.test.js
168 lines (126 loc) · 6.29 KB
/
watch-workspace-item.test.js
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
164
165
166
167
168
import {watchWorkspaceItem} from '../lib/watch-workspace-item';
import URIPattern from '../lib/atom/uri-pattern';
import {registerGitHubOpener} from './helpers';
describe('watchWorkspaceItem', function() {
let sub, atomEnv, workspace, component;
beforeEach(function() {
atomEnv = global.buildAtomEnvironment();
workspace = atomEnv.workspace;
component = {
state: {},
setState: sinon.stub().callsFake((updater, cb) => cb && cb()),
};
registerGitHubOpener(atomEnv);
});
afterEach(function() {
sub && sub.dispose();
atomEnv.destroy();
});
describe('initial state', function() {
it('creates component state if none is present', function() {
component.state = undefined;
sub = watchWorkspaceItem(workspace, 'atom-github://item', component, 'aKey');
assert.deepEqual(component.state, {aKey: false});
});
it('is false when the pane is not open', async function() {
await workspace.open('atom-github://nonmatching');
sub = watchWorkspaceItem(workspace, 'atom-github://item', component, 'someKey');
assert.isFalse(component.state.someKey);
});
it('is false when the pane is open but not active', async function() {
await workspace.open('atom-github://item/one');
await workspace.open('atom-github://item/two');
sub = watchWorkspaceItem(workspace, 'atom-github://item/one', component, 'theKey');
assert.isFalse(component.state.theKey);
});
it('is true when the pane is already open and active', async function() {
await workspace.open('atom-github://item/two');
await workspace.open('atom-github://item/one');
sub = watchWorkspaceItem(workspace, 'atom-github://item/one', component, 'theKey');
assert.isTrue(component.state.theKey);
});
it('is true when the pane is open and active in any pane', async function() {
await workspace.open('atom-github://some-item', {location: 'right'});
await workspace.open('atom-github://nonmatching');
assert.strictEqual(workspace.getRightDock().getActivePaneItem().getURI(), 'atom-github://some-item');
assert.strictEqual(workspace.getActivePaneItem().getURI(), 'atom-github://nonmatching');
sub = watchWorkspaceItem(workspace, 'atom-github://some-item', component, 'someKey');
assert.isTrue(component.state.someKey);
});
it('accepts a preconstructed URIPattern', async function() {
await workspace.open('atom-github://item/one');
const u = new URIPattern('atom-github://item/{pattern}');
sub = watchWorkspaceItem(workspace, u, component, 'theKey');
assert.isTrue(component.state.theKey);
});
});
describe('workspace events', function() {
it('becomes true when the pane is opened', async function() {
sub = watchWorkspaceItem(workspace, 'atom-github://item/{pattern}', component, 'theKey');
assert.isFalse(component.state.theKey);
await workspace.open('atom-github://item/match');
assert.isTrue(component.setState.calledWith({theKey: true}));
});
it('remains true if another matching pane is opened', async function() {
await workspace.open('atom-github://item/match0');
sub = watchWorkspaceItem(workspace, 'atom-github://item/{pattern}', component, 'theKey');
assert.isTrue(component.state.theKey);
await workspace.open('atom-github://item/match1');
assert.isFalse(component.setState.called);
});
it('becomes false if a nonmatching pane is opened', async function() {
await workspace.open('atom-github://item/match0');
sub = watchWorkspaceItem(workspace, 'atom-github://item/{pattern}', component, 'theKey');
assert.isTrue(component.state.theKey);
await workspace.open('atom-github://other-item/match1');
assert.isTrue(component.setState.calledWith({theKey: false}));
});
it('becomes false if the last matching pane is closed', async function() {
await workspace.open('atom-github://item/match0');
await workspace.open('atom-github://item/match1');
sub = watchWorkspaceItem(workspace, 'atom-github://item/{pattern}', component, 'theKey');
assert.isTrue(component.state.theKey);
assert.isTrue(workspace.hide('atom-github://item/match1'));
assert.isFalse(component.setState.called);
assert.isTrue(workspace.hide('atom-github://item/match0'));
assert.isTrue(component.setState.calledWith({theKey: false}));
});
});
it('stops updating when disposed', async function() {
sub = watchWorkspaceItem(workspace, 'atom-github://item', component, 'theKey');
assert.isFalse(component.state.theKey);
sub.dispose();
await workspace.open('atom-github://item');
assert.isFalse(component.setState.called);
await workspace.hide('atom-github://item');
assert.isFalse(component.setState.called);
});
describe('setPattern', function() {
it('immediately updates the state based on the new pattern', async function() {
sub = watchWorkspaceItem(workspace, 'atom-github://item0/{pattern}', component, 'theKey');
assert.isFalse(component.state.theKey);
await workspace.open('atom-github://item1/match');
assert.isFalse(component.setState.called);
await sub.setPattern('atom-github://item1/{pattern}');
assert.isFalse(component.state.theKey);
assert.isTrue(component.setState.calledWith({theKey: true}));
});
it('uses the new pattern to keep state up to date', async function() {
sub = watchWorkspaceItem(workspace, 'atom-github://item0/{pattern}', component, 'theKey');
await sub.setPattern('atom-github://item1/{pattern}');
await workspace.open('atom-github://item0/match');
assert.isFalse(component.setState.called);
await workspace.open('atom-github://item1/match');
assert.isTrue(component.setState.calledWith({theKey: true}));
});
it('accepts a preconstructed URIPattern', async function() {
sub = watchWorkspaceItem(workspace, 'atom-github://item0/{pattern}', component, 'theKey');
assert.isFalse(component.state.theKey);
await workspace.open('atom-github://item1/match');
assert.isFalse(component.setState.called);
await sub.setPattern(new URIPattern('atom-github://item1/{pattern}'));
assert.isFalse(component.state.theKey);
assert.isTrue(component.setState.calledWith({theKey: true}));
});
});
});