Skip to content

Commit dd504c0

Browse files
committed
Fix instanceof FileAttachment.
1 parent 041ea0b commit dd504c0

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

src/fileAttachment.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ export function NoFileAttachments(name) {
4848
}
4949

5050
export default function FileAttachments(resolve) {
51-
return name => {
52-
const url = resolve(name += ""); // Returns a Promise, string, or null.
53-
if (url == null) throw new Error(`File not found: ${name}`);
54-
return new FileAttachment(url, name);
55-
};
51+
return Object.assign(
52+
name => {
53+
const url = resolve(name += ""); // Returns a Promise, string, or null.
54+
if (url == null) throw new Error(`File not found: ${name}`);
55+
return new FileAttachment(url, name);
56+
},
57+
{prototype: FileAttachment.prototype} // instanceof
58+
);
5659
}

test/fileAttachments-test.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,42 @@ test("FileAttachments is exported by stdlib", t => {
88
});
99

1010
test("FileAttachment ensures that URLs are strings", async t => {
11-
const fileAttachments = FileAttachments((name) =>
11+
const FileAttachment = FileAttachments((name) =>
1212
new URL(`https://example.com/${name}.js`)
1313
);
14-
const file = fileAttachments("filename");
14+
const file = FileAttachment("filename");
1515
t.equal(file.constructor.name, "FileAttachment");
1616
t.equal(await file.url(), "https://example.com/filename.js");
1717
t.end();
1818
});
1919

20+
test("FileAttachment returns instances of FileAttachment", async t => {
21+
const FileAttachment = FileAttachments((name) =>
22+
new URL(`https://example.com/${name}.js`)
23+
);
24+
const file = FileAttachment("filename");
25+
t.true(file instanceof FileAttachment);
26+
t.end();
27+
});
28+
29+
test("FileAttachment cannot be used as a constructor", async t => {
30+
const FileAttachment = FileAttachments((name) =>
31+
new URL(`https://example.com/${name}.js`)
32+
);
33+
try {
34+
new FileAttachment("filename");
35+
t.fail();
36+
} catch (error) {
37+
t.equal(error.message, "FileAttachment is not a constructor");
38+
}
39+
t.end();
40+
});
41+
2042
test("FileAttachment works with Promises that resolve to URLs", async t => {
21-
const fileAttachments = FileAttachments(async (name) =>
43+
const FileAttachment = FileAttachments(async (name) =>
2244
new URL(`https://example.com/${name}.js`)
2345
);
24-
const file = fileAttachments("otherfile");
46+
const file = FileAttachment("otherfile");
2547
t.equal(file.constructor.name, "FileAttachment");
2648
t.equal(await file.url(), "https://example.com/otherfile.js");
2749
t.end();

0 commit comments

Comments
 (0)