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
189 lines (158 loc) · 5.74 KB
/
ast.spec.js
File metadata and controls
189 lines (158 loc) · 5.74 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
/* eslint max-nested-callbacks: ["error", 6] */
import {
createStore,
selectors,
actions,
makeSource,
makeOriginalSource,
makeFrame,
waitForState
} from "../../utils/test-head";
import readFixture from "./helpers/readFixture";
const {
getSymbols,
getEmptyLines,
getOutOfScopeLocations,
getSourceMetaData,
getInScopeLines,
isSymbolsLoading
} = selectors;
import I from "immutable";
import { prefs } from "../../utils/prefs";
const threadClient = {
sourceContents: function(sourceId) {
return new Promise((resolve, reject) =>
resolve({
source: sourceTexts[sourceId],
contentType: "text/javascript"
})
);
},
setPausePoints: async () => {},
getFrameScopes: async () => {},
evaluate: function(expression) {
return new Promise((resolve, reject) =>
resolve({ result: evaluationResult[expression] })
);
}
};
const sourceTexts = {
"base.js": "function base(boo) {}",
"foo.js": "function base(boo) { return this.bazz; } outOfScope",
"scopes.js": readFixture("scopes.js"),
"reactComponent.js-original": readFixture("reactComponent.js")
};
const evaluationResult = {
"this.bazz": { actor: "bazz", preview: {} },
this: { actor: "this", preview: {} }
};
describe("ast", () => {
describe("setPausePoints", () => {
it("scopes", async () => {
const store = createStore(threadClient);
const { dispatch, getState } = store;
const source = makeSource("scopes.js");
await dispatch(actions.newSource(source));
await dispatch(actions.loadSourceText(I.Map({ id: "scopes.js" })));
await dispatch(actions.setPausePoints("scopes.js"));
await waitForState(store, state => {
const lines = getEmptyLines(state, source);
return lines && lines.length > 0;
});
const emptyLines = getEmptyLines(getState(), source);
expect(emptyLines).toMatchSnapshot();
});
});
describe("setSourceMetaData", () => {
it("should detect react components", async () => {
const store = createStore(threadClient);
const { dispatch, getState } = store;
const source = makeOriginalSource("reactComponent.js");
await dispatch(actions.newSource(source));
await dispatch(actions.loadSourceText(I.Map({ id: source.id })));
await dispatch(actions.setSourceMetaData(source.id));
await waitForState(store, state => {
const metaData = getSourceMetaData(state, source.id);
return metaData && metaData.framework;
});
const sourceMetaData = getSourceMetaData(getState(), source.id);
expect(sourceMetaData.framework).toBe("React");
});
it("should not give false positive on non react components", async () => {
const store = createStore(threadClient);
const { dispatch, getState } = store;
const base = makeSource("base.js");
await dispatch(actions.newSource(base));
await dispatch(actions.loadSourceText(I.Map({ id: "base.js" })));
await dispatch(actions.setSourceMetaData("base.js"));
const sourceMetaData = getSourceMetaData(getState(), base.id);
expect(sourceMetaData.framework).toBe(undefined);
});
});
describe("setSymbols", () => {
describe("when the source is loaded", () => {
it("should be able to set symbols", async () => {
const store = createStore(threadClient);
const { dispatch, getState } = store;
const base = makeSource("base.js");
await dispatch(actions.newSource(base));
await dispatch(actions.loadSourceText(I.Map({ id: "base.js" })));
await dispatch(actions.setSymbols("base.js"));
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 = makeSource("base.js");
await dispatch(actions.newSource(base));
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("getOutOfScopeLocations", () => {
beforeEach(async () => {
prefs.autoPrettyPrint = false;
});
it("with selected line", async () => {
const store = createStore(threadClient);
const { dispatch, getState } = store;
const source = makeSource("scopes.js");
await dispatch(actions.newSource(source));
await dispatch(
actions.selectLocation({ sourceId: "scopes.js", line: 5 })
);
await dispatch(
actions.paused({
frames: [makeFrame({ id: 1, sourceId: "scopes.js" })]
})
);
await dispatch(actions.setOutOfScopeLocations("scopes.js"));
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 } = createStore(threadClient);
const base = makeSource("base.js");
await dispatch(actions.newSource(base));
await dispatch(actions.selectSource("base.js"));
const locations = getOutOfScopeLocations(getState());
const lines = getInScopeLines(getState());
expect(locations).toEqual(null);
expect(lines).toEqual([1]);
});
});
});