summaryrefslogtreecommitdiffstats
path: root/test/fetch/file.js
blob: 59015413eb042f3ab295597f600ea0228941f8b4 (plain)
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
190
'use strict'

const { Blob } = require('buffer')
const { test } = require('tap')
const { File, FileLike } = require('../../lib/fetch/file')

test('args validation', (t) => {
  t.plan(14)

  t.throws(() => {
    File.prototype.name.toString()
  }, TypeError)
  t.throws(() => {
    File.prototype.lastModified.toString()
  }, TypeError)
  t.doesNotThrow(() => {
    File.prototype[Symbol.toStringTag].charAt(0)
  }, TypeError)

  t.throws(() => {
    FileLike.prototype.stream.call(null)
  }, TypeError)
  t.throws(() => {
    FileLike.prototype.arrayBuffer.call(null)
  }, TypeError)
  t.throws(() => {
    FileLike.prototype.slice.call(null)
  }, TypeError)
  t.throws(() => {
    FileLike.prototype.text.call(null)
  }, TypeError)
  t.throws(() => {
    FileLike.prototype.size.toString()
  }, TypeError)
  t.throws(() => {
    FileLike.prototype.type.toString()
  }, TypeError)
  t.throws(() => {
    FileLike.prototype.name.toString()
  }, TypeError)
  t.throws(() => {
    FileLike.prototype.lastModified.toString()
  }, TypeError)
  t.doesNotThrow(() => {
    FileLike.prototype[Symbol.toStringTag].charAt(0)
  }, TypeError)

  t.equal(File.prototype[Symbol.toStringTag], 'File')
  t.equal(FileLike.prototype[Symbol.toStringTag], 'File')
})

test('return value of File.lastModified', (t) => {
  t.plan(2)

  const f = new File(['asd1'], 'filename123')
  const lastModified = f.lastModified
  t.ok(typeof lastModified === typeof Date.now())
  t.ok(lastModified >= 0 && lastModified <= Date.now())
})

test('Symbol.toStringTag', (t) => {
  t.plan(2)
  t.equal(new File([], '')[Symbol.toStringTag], 'File')
  t.equal(new FileLike()[Symbol.toStringTag], 'File')
})

test('arguments', (t) => {
  t.throws(() => {
    new File() // eslint-disable-line no-new
  }, TypeError)

  t.throws(() => {
    new File([]) // eslint-disable-line no-new
  }, TypeError)

  t.end()
})

test('lastModified', (t) => {
  const file = new File([], '')
  const lastModified = Date.now() - 69_000

  t.notOk(file === 0)

  const file1 = new File([], '', { lastModified })
  t.equal(file1.lastModified, lastModified)

  t.equal(new File([], '', { lastModified: 0 }).lastModified, 0)

  t.equal(
    new File([], '', {
      lastModified: true
    }).lastModified,
    1
  )

  t.end()
})

test('File.prototype.text', async (t) => {
  t.test('With Blob', async (t) => {
    const blob1 = new Blob(['hello'])
    const blob2 = new Blob([' '])
    const blob3 = new Blob(['world'])

    const file = new File([blob1, blob2, blob3], 'hello_world.txt')

    t.equal(await file.text(), 'hello world')
    t.end()
  })

  /* eslint-disable camelcase */
  t.test('With TypedArray', async (t) => {
    const uint8_1 = new Uint8Array(Buffer.from('hello'))
    const uint8_2 = new Uint8Array(Buffer.from(' '))
    const uint8_3 = new Uint8Array(Buffer.from('world'))

    const file = new File([uint8_1, uint8_2, uint8_3], 'hello_world.txt')

    t.equal(await file.text(), 'hello world')
    t.end()
  })

  t.test('With TypedArray range', async (t) => {
    const uint8_1 = new Uint8Array(Buffer.from('hello world'))
    const uint8_2 = new Uint8Array(uint8_1.buffer, 1, 4)

    const file = new File([uint8_2], 'hello_world.txt')

    t.equal(await file.text(), 'ello')
    t.end()
  })
  /* eslint-enable camelcase */

  t.test('With ArrayBuffer', async (t) => {
    const uint8 = new Uint8Array([65, 66, 67])
    const ab = uint8.buffer

    const file = new File([ab], 'file.txt')

    t.equal(await file.text(), 'ABC')
    t.end()
  })

  t.test('With string', async (t) => {
    const string = 'hello world'
    const file = new File([string], 'hello_world.txt')

    t.equal(await file.text(), 'hello world')
    t.end()
  })

  t.test('With Buffer', async (t) => {
    const buffer = Buffer.from('hello world')

    const file = new File([buffer], 'hello_world.txt')

    t.equal(await file.text(), 'hello world')
    t.end()
  })

  t.test('Mixed', async (t) => {
    const blob = new Blob(['Hello, '])
    const uint8 = new Uint8Array(Buffer.from('world! This'))
    const string = ' is a test! Hope it passes!'

    const file = new File([blob, uint8, string], 'mixed-messages.txt')

    t.equal(
      await file.text(),
      'Hello, world! This is a test! Hope it passes!'
    )
    t.end()
  })

  t.end()
})

test('endings=native', async (t) => {
  const file = new File(['Hello\nWorld'], 'text.txt', { endings: 'native' })
  const text = await file.text()

  if (process.platform === 'win32') {
    t.equal(text, 'Hello\r\nWorld', 'on windows, LF is replace with CRLF')
  } else {
    t.equal(text, 'Hello\nWorld', `on ${process.platform} LF stays LF`)
  }

  t.end()
})