forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadSource.spec.js
More file actions
110 lines (88 loc) · 3.65 KB
/
Copy pathloadSource.spec.js
File metadata and controls
110 lines (88 loc) · 3.65 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
/* 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/>. */
import {
actions,
selectors,
createStore,
makeSource
} from "../../../utils/test-head";
import * as I from "immutable";
import { sourceThreadClient } from "../../tests/helpers/threadClient.js";
describe("loadSourceText", async () => {
it("should load source text", async () => {
const store = createStore(sourceThreadClient);
const { dispatch, getState } = store;
await dispatch(actions.loadSourceText(I.Map({ id: "foo1" })));
const fooSource = selectors.getSource(getState(), "foo1");
expect(fooSource.text.indexOf("return foo1")).not.toBe(-1);
await dispatch(actions.loadSourceText(I.Map({ id: "foo2" })));
const foo2Source = selectors.getSource(getState(), "foo2");
expect(foo2Source.text.indexOf("return foo2")).not.toBe(-1);
});
it("loads two sources w/ one request", async () => {
let resolve;
let count = 0;
const { dispatch, getState } = createStore({
sourceContents: () =>
new Promise(r => {
count++;
resolve = r;
})
});
const id = "foo";
let source = makeSource(id, { loadedState: "unloaded" });
await dispatch(actions.newSource(source));
source = selectors.getSource(getState(), id);
dispatch(actions.loadSourceText(source));
source = selectors.getSource(getState(), id);
const loading = dispatch(actions.loadSourceText(source));
resolve({ source: "yay", contentType: "text/javascript" });
await loading;
expect(count).toEqual(1);
expect(selectors.getSource(getState(), id).text).toEqual("yay");
});
it("doesn't re-load loaded sources", async () => {
let resolve;
let count = 0;
const { dispatch, getState } = createStore({
sourceContents: () =>
new Promise(r => {
count++;
resolve = r;
})
});
const id = "foo";
let source = makeSource(id, { loadedState: "unloaded" });
await dispatch(actions.newSource(source));
source = selectors.getSource(getState(), id);
const loading = dispatch(actions.loadSourceText(source));
resolve({ source: "yay", contentType: "text/javascript" });
await loading;
source = selectors.getSource(getState(), id);
await dispatch(actions.loadSourceText(source));
expect(count).toEqual(1);
expect(selectors.getSource(getState(), id).text).toEqual("yay");
});
it("should cache subsequent source text loads", async () => {
const { dispatch, getState } = createStore(sourceThreadClient);
await dispatch(actions.loadSourceText(I.Map({ id: "foo1" })));
const prevSource = selectors.getSource(getState(), "foo1");
await dispatch(actions.loadSourceText(prevSource));
const curSource = selectors.getSource(getState(), "foo1");
expect(prevSource === curSource).toBeTruthy();
});
it("should indicate a loading source", async () => {
const { dispatch, getState } = createStore(sourceThreadClient);
// Don't block on this so we can check the loading state.
dispatch(actions.loadSourceText(I.Map({ id: "foo1" })));
const fooSource = selectors.getSource(getState(), "foo1");
expect(fooSource.get("loadedState")).toEqual("loading");
});
it("should indicate an errored source text", async () => {
const { dispatch, getState } = createStore(sourceThreadClient);
await dispatch(actions.loadSourceText(I.Map({ id: "bad-id" })));
const badSource = selectors.getSource(getState(), "bad-id");
expect(badSource.get("error").indexOf("unknown source")).not.toBe(-1);
});
});