-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
public-path.test.js
94 lines (77 loc) · 2.76 KB
/
public-path.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
const { setPublicPath } = require("./public-path.js");
describe("setPublicPath", () => {
beforeEach(() => {
window.System = {
resolve: jest.fn()
};
global.__webpack_public_path__ = undefined;
});
it("can properly set the public path with default directory level", () => {
expect(__webpack_public_path__).toBe(undefined);
window.System.resolve.mockReturnValue("http://localhost:8080/foo.js");
setPublicPath("foo");
expect(__webpack_public_path__).toBe("http://localhost:8080/");
});
it("can properly set public path for urls with multiple directories in the path", () => {
expect(__webpack_public_path__).toBe(undefined);
window.System.resolve.mockReturnValueOnce(
"http://localhost:8080/some-dir/foo.js"
);
setPublicPath("foo");
expect(__webpack_public_path__).toBe("http://localhost:8080/some-dir/");
window.System.resolve.mockReturnValueOnce(
"http://localhost:8080/some-dir/sub-dir/foo.js"
);
setPublicPath("foo");
expect(__webpack_public_path__).toBe(
"http://localhost:8080/some-dir/sub-dir/"
);
window.System.resolve.mockReturnValueOnce(
"http://localhost:8080/some-dir/sub-dir/final-dir/foo.js"
);
setPublicPath("foo");
expect(__webpack_public_path__).toBe(
"http://localhost:8080/some-dir/sub-dir/final-dir/"
);
});
it("can properly set public path two directories up", () => {
expect(__webpack_public_path__).toBe(undefined);
const rootDirLevel = 2;
window.System.resolve.mockReturnValueOnce(
"http://localhost:8080/some-dir/sub-dir/foo.js"
);
setPublicPath("foo", rootDirLevel);
expect(__webpack_public_path__).toBe("http://localhost:8080/some-dir/");
window.System.resolve.mockReturnValueOnce(
"http://localhost:8080/some-dir/sub-dir/final-dir/foo.js"
);
setPublicPath("foo", rootDirLevel);
expect(__webpack_public_path__).toBe(
"http://localhost:8080/some-dir/sub-dir/"
);
});
it("throws if given a bad module name", () => {
expect(() => {
setPublicPath();
}).toThrowError(`must be called with a non-empty string`);
});
it(`throws if you provide a bogus rootDirectoryLevel`, () => {
expect(() => {
setPublicPath("foo", "not a number");
}).toThrowError(`positive integer`);
});
it("throws if System.resolve throws", () => {
window.System.resolve.mockImplementation(name => {
throw Error(`SystemJS can't resolve ${name}`);
});
expect(() => {
setPublicPath("foo");
}).toThrowError(`There is no such module`);
});
it(`throws if System.resolve returns a falsy value`, () => {
window.System.resolve.mockReturnValue(undefined);
expect(() => {
setPublicPath("foo");
}).toThrowError(`There is no such module`);
});
});