-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/custom-error-message-in-retrieveClie…
…ntToken
- Loading branch information
Showing
10 changed files
with
110 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
// TextEncoder and TextDecoder are not defined in Jest dom environment, | ||
// but they are available everywhere else. | ||
// See https://stackoverflow.com/questions/68468203/why-am-i-getting-textencoder-is-not-defined-in-jest | ||
const { TextEncoder, TextDecoder } = require('node:util'); | ||
|
||
Object.assign(global, { TextDecoder, TextEncoder }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import undici from 'undici'; | ||
import { upload } from './client'; | ||
|
||
// can't use undici mocking utilities because jsdom does not support performance.markResourceTiming | ||
jest.mock('undici', () => ({ | ||
fetch: jest | ||
.fn() | ||
.mockResolvedValueOnce({ | ||
status: 200, | ||
ok: true, | ||
json: () => | ||
Promise.resolve({ | ||
type: 'blob.generate-client-token', | ||
clientToken: 'fake-token-for-test', | ||
}), | ||
}) | ||
.mockResolvedValueOnce({ | ||
status: 200, | ||
ok: true, | ||
json: () => | ||
Promise.resolve({ | ||
url: `https://storeId.public.blob.vercel-storage.com/superfoo.txt`, | ||
pathname: 'foo.txt', | ||
contentType: 'text/plain', | ||
contentDisposition: 'attachment; filename="foo.txt"', | ||
}), | ||
}), | ||
})); | ||
|
||
describe('upload()', () => { | ||
beforeEach(() => { | ||
process.env.BLOB_READ_WRITE_TOKEN = | ||
'vercel_blob_rw_12345fakeStoreId_30FakeRandomCharacters12345678'; | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should upload a file from the client', async () => { | ||
await expect( | ||
upload('foo.txt', 'Test file data', { | ||
access: 'public', | ||
handleUploadUrl: '/api/upload', | ||
}), | ||
).resolves.toMatchInlineSnapshot(` | ||
{ | ||
"contentDisposition": "attachment; filename="foo.txt"", | ||
"contentType": "text/plain", | ||
"pathname": "foo.txt", | ||
"url": "https://storeId.public.blob.vercel-storage.com/superfoo.txt", | ||
} | ||
`); | ||
|
||
const fetchMock = undici.fetch as jest.Mock; | ||
expect(fetchMock).toHaveBeenCalledTimes(2); | ||
expect(fetchMock).toHaveBeenNthCalledWith( | ||
1, | ||
'http://localhost:3000/api/upload', | ||
{ | ||
body: '{"type":"blob.generate-client-token","payload":{"pathname":"foo.txt","callbackUrl":"http://localhost:3000/api/upload"}}', | ||
headers: { 'content-type': 'application/json' }, | ||
method: 'POST', | ||
}, | ||
); | ||
expect(fetchMock).toHaveBeenNthCalledWith( | ||
2, | ||
'https://blob.vercel-storage.com/foo.txt', | ||
{ | ||
body: 'Test file data', | ||
duplex: 'half', | ||
headers: { | ||
authorization: 'Bearer fake-token-for-test', | ||
'x-api-version': '5', | ||
}, | ||
method: 'PUT', | ||
}, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters