Skip to content

Commit

Permalink
configure client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
correttojs committed Jun 15, 2023
1 parent a7698d5 commit 994711d
Show file tree
Hide file tree
Showing 5 changed files with 1,156 additions and 820 deletions.
3 changes: 3 additions & 0 deletions packages/blob/jest/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { TextEncoder, TextDecoder } = require('node:util');

Object.assign(global, { TextDecoder, TextEncoder });
7 changes: 6 additions & 1 deletion packages/blob/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@
"prepublishOnly": "pnpm run build",
"prettier-check": "prettier --check .",
"publint": "npx publint",
"test": "pnpm run test:node && pnpm run test:edge",
"test": "pnpm run test:node && pnpm run test:edge && pnpm run test:browser",
"test:browser": "jest --env jsdom .browser.test.ts",
"test:edge": "jest --env @edge-runtime/jest-environment .edge.test.ts",
"test:node": "jest --env node .node.test.ts",
"type-check": "tsc --noEmit"
},
"jest": {
"preset": "ts-jest",
"setupFiles": [
"<rootDir>/jest/setup.js"
],
"testEnvironment": "node"
},
"dependencies": {
"jest-environment-jsdom": "29.5.0",
"undici": "5.22.0"
},
"devDependencies": {
Expand Down
70 changes: 70 additions & 0 deletions packages/blob/src/index.browser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { type Interceptable, MockAgent, setGlobalDispatcher } from 'undici';
import { put } from './index';

const BASE_URL = 'https://blob.vercel-storage.com';
const mockAgent = new MockAgent();
mockAgent.disableNetConnect();

setGlobalDispatcher(mockAgent);

const mockedFileMeta = {
url: `${BASE_URL}/storeid/foo-id.txt`,
size: 12345,
uploadedAt: '2023-05-04T15:12:07.818Z',
pathname: 'foo.txt',
contentType: 'text/plain',
contentDisposition: 'attachment; filename="foo.txt"',
};

describe('blob client', () => {
let mockClient: Interceptable;

beforeEach(() => {
mockClient = mockAgent.get(BASE_URL);
jest.resetAllMocks();
});

describe('put', () => {
beforeEach(() => {
mockClient = mockAgent.get(BASE_URL);
jest.resetAllMocks();
});

it('should upload a file from the client', async () => {
mockClient
.intercept({
path: () => true,
method: 'PUT',
})
.reply(200, mockedFileMeta);

await expect(
put('foo.txt', 'Test Body', {
access: 'public',
token: 'vercel_blob_client_123_token',
}),
).resolves.toMatchInlineSnapshot(`
{
"contentDisposition": "attachment; filename="foo.txt"",
"contentType": "text/plain",
"pathname": "foo.txt",
"size": 12345,
"uploadedAt": 2023-05-04T15:12:07.818Z,
"url": "https://blob.vercel-storage.com/storeid/foo-id.txt",
}
`);
});

it('should throw when calling `put()` with a server token', async () => {
await expect(
put('foo.txt', 'Test Body', {
access: 'public',
contentType: 'text/plain',
token: 'vercel_blob_rw_123_TEST_TOKEN',
}),
).rejects.toThrow(
new Error('Vercel Blob: client upload only supports client tokens'),
);
});
});
});
50 changes: 0 additions & 50 deletions packages/blob/src/index.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,56 +490,6 @@ describe('blob client', () => {
});
});

describe('client put', () => {
beforeEach(() => {
process.env.BLOB_READ_WRITE_TOKEN = undefined;
mockClient = mockAgent.get(BASE_URL);
jest.resetAllMocks();
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any
global.window = {} as any;
});
afterEach(() => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any
global.window = undefined as any;
});
it('should upload a file from the client', async () => {
mockClient
.intercept({
path: () => true,
method: 'PUT',
})
.reply(200, mockedFileMeta);

await expect(
put('foo.txt', 'Test Body', {
access: 'public',
token: 'vercel_blob_client_123_token',
}),
).resolves.toMatchInlineSnapshot(`
{
"contentDisposition": "attachment; filename="foo.txt"",
"contentType": "text/plain",
"pathname": "foo.txt",
"size": 12345,
"uploadedAt": 2023-05-04T15:12:07.818Z,
"url": "https://blob.vercel-storage.com/storeid/foo-id.txt",
}
`);
});

it('should throw when calling `put()` with a server token', async () => {
await expect(
put('foo.txt', 'Test Body', {
access: 'public',
contentType: 'text/plain',
token: 'vercel_blob_rw_123_TEST_TOKEN',
}),
).rejects.toThrow(
new Error('Vercel Blob: client upload only supports client tokens'),
);
});
});

describe('generateClientTokenFromReadWriteToken', () => {
afterEach(() => {
jest.runOnlyPendingTimers();
Expand Down
Loading

0 comments on commit 994711d

Please sign in to comment.