forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.spec.js
More file actions
198 lines (164 loc) · 6.29 KB
/
ast.spec.js
File metadata and controls
198 lines (164 loc) · 6.29 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* eslint max-nested-callbacks: ["error", 6] */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// @flow
import {
createStore,
selectors,
actions,
makeSource,
makeOriginalSource,
makeFrame,
waitForState
} from "../../utils/test-head";
import readFixture from "./helpers/readFixture";
const {
getSymbols,
getOutOfScopeLocations,
getInScopeLines,
isSymbolsLoading,
getFramework
} = selectors;
import { prefs } from "../../utils/prefs";
const threadClient = {
sourceContents: async ({ source }) => ({
source: sourceTexts[source],
contentType: "text/javascript"
}),
getFrameScopes: async () => {},
evaluate: async expression => ({ result: evaluationResult[expression] }),
evaluateExpressions: async expressions =>
expressions.map(expression => ({ result: evaluationResult[expression] })),
getBreakpointPositions: async () => ({}),
getBreakableLines: async () => []
};
const sourceMaps = {
getOriginalSourceText: async ({ id }) => ({
id,
text: sourceTexts[id],
contentType: "text/javascript"
}),
getGeneratedRangesForOriginal: async () => [],
getOriginalLocations: async items => items
};
const sourceTexts = {
"base.js": "function base(boo) {}",
"foo.js": "function base(boo) { return this.bazz; } outOfScope",
"scopes.js": readFixture("scopes.js"),
"reactComponent.js/originalSource": readFixture("reactComponent.js"),
"reactFuncComponent.js/originalSource": readFixture("reactFuncComponent.js")
};
const evaluationResult = {
"this.bazz": { actor: "bazz", preview: {} },
this: { actor: "this", preview: {} }
};
describe("ast", () => {
describe("setSymbols", () => {
describe("when the source is loaded", () => {
it("should be able to set symbols", async () => {
const store = createStore(threadClient);
const { dispatch, getState, cx } = store;
const base = await dispatch(
actions.newGeneratedSource(makeSource("base.js"))
);
await dispatch(actions.loadSourceText({ cx, source: base }));
const loadedSource = selectors.getSourceFromId(getState(), base.id);
await dispatch(actions.setSymbols({ cx, source: loadedSource }));
await waitForState(store, state => !isSymbolsLoading(state, base));
const baseSymbols = getSymbols(getState(), base);
expect(baseSymbols).toMatchSnapshot();
});
});
describe("when the source is not loaded", () => {
it("should return null", async () => {
const { getState, dispatch } = createStore(threadClient);
const base = await dispatch(
actions.newGeneratedSource(makeSource("base.js"))
);
const baseSymbols = getSymbols(getState(), base);
expect(baseSymbols).toEqual(null);
});
});
describe("when there is no source", () => {
it("should return null", async () => {
const { getState } = createStore(threadClient);
const baseSymbols = getSymbols(getState());
expect(baseSymbols).toEqual(null);
});
});
describe("frameworks", () => {
it("should detect react components", async () => {
const store = createStore(threadClient, {}, sourceMaps);
const { cx, dispatch, getState } = store;
const genSource = await dispatch(
actions.newGeneratedSource(makeSource("reactComponent.js"))
);
const source = await dispatch(
actions.newOriginalSource(makeOriginalSource(genSource))
);
await dispatch(actions.loadSourceText({ cx, source }));
const loadedSource = selectors.getSourceFromId(getState(), source.id);
await dispatch(actions.setSymbols({ cx, source: loadedSource }));
expect(getFramework(getState(), source)).toBe("React");
});
it("should not give false positive on non react components", async () => {
const store = createStore(threadClient);
const { cx, dispatch, getState } = store;
const base = await dispatch(
actions.newGeneratedSource(makeSource("base.js"))
);
await dispatch(actions.loadSourceText({ cx, source: base }));
await dispatch(actions.setSymbols({ cx, source: base }));
expect(getFramework(getState(), base)).toBe(undefined);
});
});
});
describe("getOutOfScopeLocations", () => {
beforeEach(async () => {
prefs.autoPrettyPrint = false;
});
it("with selected line", async () => {
const store = createStore(threadClient);
const { dispatch, getState, cx } = store;
const source = await dispatch(
actions.newGeneratedSource(makeSource("scopes.js"))
);
await dispatch(
actions.selectLocation(cx, { sourceId: "scopes.js", line: 5 })
);
// Make sure the state has finished updating before pausing.
await waitForState(store, state => {
const symbols = getSymbols(state, source);
return symbols && !symbols.loading && getOutOfScopeLocations(state);
});
const frame = makeFrame({ id: "1", sourceId: "scopes.js" });
await dispatch(
actions.paused({
thread: "FakeThread",
why: { type: "debuggerStatement" },
frame,
frames: [frame]
})
);
const ncx = selectors.getThreadContext(getState());
await dispatch(actions.setOutOfScopeLocations(ncx));
await waitForState(store, state => getOutOfScopeLocations(state));
const locations = getOutOfScopeLocations(getState());
const lines = getInScopeLines(getState());
expect(locations).toMatchSnapshot();
expect(lines).toMatchSnapshot();
});
it("without a selected line", async () => {
const { dispatch, getState, cx } = createStore(threadClient);
await dispatch(actions.newGeneratedSource(makeSource("base.js")));
await dispatch(actions.selectSource(cx, "base.js"));
const locations = getOutOfScopeLocations(getState());
// const lines = getInScopeLines(getState());
expect(locations).toEqual(null);
// This check is disabled as locations that are in/out of scope may not
// have completed yet when the selectSource promise finishes.
// expect(lines).toEqual([1]);
});
});
});