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 402
/
Copy pathhelpers.js
143 lines (126 loc) · 4.08 KB
/
helpers.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
import {mount} from 'enzyme';
import {getTempDir} from '../../lib/helpers';
import {cloneRepository} from '../helpers';
import GithubPackage from '../../lib/github-package';
import GithubLoginModel from '../../lib/models/github-login-model';
import {InMemoryStrategy} from '../../lib/shared/keytar-strategy';
import metadata from '../../package.json';
/**
* Perform shared setup for integration tests.
*
* Usage:
* ```js
* beforeEach(async function() {
* context = await setup();
* wrapper = context.wrapper;
* })
*
* afterEach(async function() {
* await teardown(context)
* })
* ```
*
* Options:
* * initialRoots - Describe the root folders that should be open in the Atom environment's project before the package
* is initialized. Array elements are passed to `cloneRepository` directly.
* * initAtomEnv - Callback invoked with the Atom environment before the package is created.
* * initConfigDir - Callback invoked with the config dir path for this Atom environment.
* * isolateConfigDir - Use a temporary directory for the package configDir used by this test. Implied if initConfigDir
* is defined.
* * state - Simulate package state serialized by a previous Atom run.
*/
export async function setup(options = {}) {
const opts = {
initialRoots: [],
isolateConfigDir: options.initAtomEnv !== undefined,
initConfigDir: () => Promise.resolve(),
initAtomEnv: () => Promise.resolve(),
state: {},
...options,
};
const atomEnv = global.buildAtomEnvironment();
let suiteRoot = document.getElementById('github-IntegrationSuite');
if (!suiteRoot) {
suiteRoot = document.createElement('div');
suiteRoot.id = 'github-IntegrationSuite';
document.body.appendChild(suiteRoot);
}
const workspaceElement = atomEnv.workspace.getElement();
suiteRoot.appendChild(workspaceElement);
workspaceElement.focus();
await opts.initAtomEnv(atomEnv);
const projectDirs = await Promise.all(
opts.initialRoots.map(fixture => {
return cloneRepository(fixture);
}),
);
atomEnv.project.setPaths(projectDirs, {mustExist: true, exact: true});
const loginModel = new GithubLoginModel(InMemoryStrategy);
let configDirPath = null;
if (opts.isolateConfigDir) {
configDirPath = await getTempDir();
} else {
configDirPath = atomEnv.getConfigDirPath();
}
await opts.initConfigDir(configDirPath);
let domRoot = null;
let wrapper = null;
const githubPackage = new GithubPackage({
workspace: atomEnv.workspace,
project: atomEnv.project,
commands: atomEnv.commands,
notificationManager: atomEnv.notifications,
tooltips: atomEnv.tooltips,
styles: atomEnv.styles,
grammars: atomEnv.grammars,
config: atomEnv.config,
keymaps: atomEnv.keymaps,
deserializers: atomEnv.deserializers,
loginModel,
confirm: atomEnv.confirm.bind(atomEnv),
getLoadSettings: atomEnv.getLoadSettings.bind(atomEnv),
configDirPath,
renderFn: (component, node, callback) => {
if (!domRoot && node) {
domRoot = node;
document.body.appendChild(domRoot);
}
if (!wrapper) {
wrapper = mount(component, {
attachTo: node,
});
} else {
wrapper.setProps(component.props);
}
if (callback) {
process.nextTick(callback);
}
},
});
for (const deserializerName in metadata.deserializers) {
const methodName = metadata.deserializers[deserializerName];
atomEnv.deserializers.add({
name: deserializerName,
deserialize: githubPackage[methodName],
});
}
githubPackage.getContextPool().set(projectDirs, opts.state);
await githubPackage.activate(opts.state);
await Promise.all(
projectDirs.map(projectDir => githubPackage.getContextPool().getContext(projectDir).getObserverStartedPromise()),
);
return {
atomEnv,
githubPackage,
loginModel,
wrapper,
domRoot,
suiteRoot,
workspaceElement,
};
}
export async function teardown(context) {
await context.githubPackage.deactivate();
context.suiteRoot.removeChild(context.atomEnv.workspace.getElement());
context.atomEnv.destroy();
}