Skip to content

Commit 8da6269

Browse files
committed
CCM-13768: update unit tests
1 parent b7dc210 commit 8da6269

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

utils/utils/src/__tests__/s3-utils/put-data-s3.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('putDataS3', () => {
2323
Body: '{\n "value1": "1a",\n "value2": "2a"\n}',
2424
});
2525
});
26+
2627
it('throws an error when there is an issue puts data in S3', async () => {
2728
const s3Client = mockClient(S3Client);
2829
s3Client.rejectsOnce(new Error('It broke!'));
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { mockClient } from 'aws-sdk-client-mock';
2+
import 'aws-sdk-client-mock-jest';
3+
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
4+
import { putFileS3 } from '../../s3-utils';
5+
6+
describe('putFileS3', () => {
7+
it('puts buffer in S3', async () => {
8+
const s3Client = mockClient(S3Client);
9+
const testBuffer = Buffer.from('test pdf content');
10+
11+
await putFileS3(testBuffer, {
12+
Bucket: 'bucket-name',
13+
Key: 'file.pdf',
14+
});
15+
16+
expect(s3Client).toHaveReceivedCommandWith(PutObjectCommand, {
17+
Bucket: 'bucket-name',
18+
Key: 'file.pdf',
19+
Body: testBuffer,
20+
Metadata: {},
21+
});
22+
});
23+
24+
it('puts buffer in S3 with ContentType', async () => {
25+
const s3Client = mockClient(S3Client);
26+
const testBuffer = Buffer.from('test pdf content');
27+
28+
await putFileS3(
29+
testBuffer,
30+
{
31+
Bucket: 'bucket-name',
32+
Key: 'file.pdf',
33+
},
34+
{},
35+
'application/pdf',
36+
);
37+
38+
expect(s3Client).toHaveReceivedCommandWith(PutObjectCommand, {
39+
Bucket: 'bucket-name',
40+
Key: 'file.pdf',
41+
Body: testBuffer,
42+
Metadata: {},
43+
ContentType: 'application/pdf',
44+
});
45+
});
46+
47+
it('puts buffer in S3 with metadata', async () => {
48+
const s3Client = mockClient(S3Client);
49+
const testBuffer = Buffer.from('test pdf content');
50+
51+
await putFileS3(
52+
testBuffer,
53+
{
54+
Bucket: 'bucket-name',
55+
Key: 'file.pdf',
56+
},
57+
{ 'x-custom-metadata': 'value' },
58+
);
59+
60+
expect(s3Client).toHaveReceivedCommandWith(PutObjectCommand, {
61+
Bucket: 'bucket-name',
62+
Key: 'file.pdf',
63+
Body: testBuffer,
64+
Metadata: { 'x-custom-metadata': 'value' },
65+
});
66+
});
67+
68+
it('throws an error when there is an issue putting buffer in S3', async () => {
69+
const s3Client = mockClient(S3Client);
70+
s3Client.rejectsOnce(new Error('It broke!'));
71+
const testBuffer = Buffer.from('test pdf content');
72+
73+
await expect(
74+
putFileS3(testBuffer, {
75+
Bucket: 'bucket-name',
76+
Key: 'file.pdf',
77+
}),
78+
).rejects.toThrow(
79+
'Upload to bucket-name/file.pdf failed, error: Error: It broke!',
80+
);
81+
});
82+
});

0 commit comments

Comments
 (0)