/
test-file.js
160 lines (134 loc) Β· 3.26 KB
/
test-file.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
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
'use strict';
const common = require('../common');
const assert = require('assert');
const { Blob, File } = require('buffer');
const { inspect } = require('util');
{
// ensure File extends Blob
assert.deepStrictEqual(Object.getPrototypeOf(File.prototype), Blob.prototype);
}
{
assert.throws(() => new File(), TypeError);
assert.throws(() => new File([]), TypeError);
}
{
const properties = ['name', 'lastModified'];
for (const prop of properties) {
const desc = Object.getOwnPropertyDescriptor(File.prototype, prop);
assert.notStrictEqual(desc, undefined);
// Ensure these properties are getters.
assert.strictEqual(desc.get?.name, `get ${prop}`);
assert.strictEqual(desc.set, undefined);
assert.strictEqual(desc.enumerable, true);
assert.strictEqual(desc.configurable, true);
}
}
{
const file = new File([], '');
assert.strictEqual(file[Symbol.toStringTag], 'File');
assert.strictEqual(File.prototype[Symbol.toStringTag], 'File');
}
{
assert.throws(() => File.prototype.name, TypeError);
assert.throws(() => File.prototype.lastModified, TypeError);
}
{
const keys = Object.keys(File.prototype).sort();
assert.deepStrictEqual(keys, ['lastModified', 'name']);
}
{
const file = new File([], 'dummy.txt.exe');
assert.strictEqual(file.name, 'dummy.txt.exe');
assert.strictEqual(file.size, 0);
assert.strictEqual(typeof file.lastModified, 'number');
assert(file.lastModified <= Date.now());
}
{
const emptyFile = new File([], 'empty.txt');
const blob = new Blob(['hello world']);
emptyFile.text.call(blob).then(common.mustCall((text) => {
assert.strictEqual(text, 'hello world');
}));
}
{
const toPrimitive = {
[Symbol.toPrimitive]() {
return 'NaN';
}
};
const invalidLastModified = [
null,
'string',
false,
toPrimitive,
];
for (const lastModified of invalidLastModified) {
const file = new File([], '', { lastModified });
assert.strictEqual(file.lastModified, 0);
}
}
{
const file = new File([], '', { lastModified: undefined });
assert.notStrictEqual(file.lastModified, 0);
}
{
const toPrimitive = {
[Symbol.toPrimitive]() {
throw new TypeError('boom');
}
};
const throwValues = [
BigInt(3n),
toPrimitive,
];
for (const lastModified of throwValues) {
assert.throws(() => new File([], '', { lastModified }), TypeError);
}
}
{
const valid = [
{
[Symbol.toPrimitive]() {
return 10;
}
},
new Number(10),
10,
];
for (const lastModified of valid) {
assert.strictEqual(new File([], '', { lastModified }).lastModified, 10);
}
}
{
const file = new File([], '');
assert(inspect(file).startsWith('File { size: 0, type: \'\', name: \'\', lastModified:'));
}
{
function MyClass() {}
MyClass.prototype.lastModified = 10;
const file = new File([], '', new MyClass());
assert.strictEqual(file.lastModified, 10);
}
{
let counter = 0;
new File([], '', {
get lastModified() {
counter++;
return 10;
}
});
assert.strictEqual(counter, 1);
}
{
const getter = Object.getOwnPropertyDescriptor(File.prototype, 'name').get;
[
undefined,
null,
true,
].forEach((invalidThis) => {
assert.throws(
() => getter.call(invalidThis),
TypeError
);
});
}