@smartsoft001/mongo 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/jest.config.ts CHANGED
@@ -10,7 +10,7 @@ export default {
10
10
  ],
11
11
  },
12
12
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
13
- coverageDirectory: '../../../coverage/libs/shared/mongo',
13
+ coverageDirectory: '../../../coverage/packages/shared/mongo',
14
14
  displayName: 'shared-mongo',
15
15
  testEnvironment: 'node',
16
16
  preset: '../../../jest.preset.js',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smartsoft001/mongo",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "Utils to mongo",
5
5
  "repository": {
6
6
  "type": "git",
@@ -196,54 +196,63 @@ describe('shared-mongo: MongoAttachmentRepository', () => {
196
196
  expect(result).toBeNull(); // Expect null if no file is found
197
197
  });
198
198
 
199
- // it("upload() should upload file successfully", async () => {
200
- // const mockUrl = "mock-url";
201
- // jest.spyOn(model as any, "getUrl").mockImplementation(() => mockUrl);
202
- //
203
- // const mockStream = new Readable();
204
- // mockStream._read = jest.fn();
205
- //
206
- // // Set up the stream to simulate data being piped
207
- // data.stream = mockStream;
208
- //
209
- // const result = model.upload(data);
210
- //
211
- // // Ensure the MongoClient.connect method is called with the correct URL
212
- // expect(MongoClient.connect).toHaveBeenCalledWith(mockUrl);
213
- //
214
- // // Wait for the upload promise to resolve
215
- // await expect(result).resolves.toBeUndefined();
216
- //
217
- // // Ensure GridFSBucket and openUploadStreamWithId are called
218
- // expect(GridFSBucket).toHaveBeenCalledWith(expect.anything(), {
219
- // bucketName: mockConfig.collection,
220
- // });
221
- // expect(mockOpenUploadStreamWithId).toHaveBeenCalledWith(data.id, data.fileName, {
222
- // contentType: data.mimeType,
223
- // });
224
- //
225
- // // Ensure the stream callback is called if provided
226
- // expect(options.streamCallback).toHaveBeenCalledWith(mockWriteStream);
227
- //
228
- // // Ensure data is piped into the write stream
229
- // expect(mockStream.pipe).toHaveBeenCalledWith(mockWriteStream);
230
- // });
231
-
232
- // it("upload() should reject with error if upload fails", async () => {
233
- // const mockUrl = "mock-url";
234
- // jest.spyOn(model as any, "getUrl").mockImplementation(() => mockUrl);
235
- //
236
- // const result = model.upload({
237
- // ...data,
238
- // stream: mockStream
239
- // });
240
- //
241
- // if (mockWriteStream._errorCallback) {
242
- // mockWriteStream._errorCallback(new Error("Upload failed"));
243
- // }
244
- // // Ensure the upload rejects with the error
245
- // // await expect(result.catch(e => e)).rejects.toThrowError("Upload failed");
246
- //
247
- // await expect(result).rejects.toThrowError("Upload failed");
248
- // });
199
+ it('upload() should call MongoClient.connect with correct URL', async () => {
200
+ const mockUrl = 'mock-url';
201
+ jest.spyOn(model as any, 'getUrl').mockImplementation(() => mockUrl);
202
+ const mockStream = new Readable();
203
+ mockStream._read = jest.fn();
204
+ data.stream = mockStream;
205
+ model.upload(data);
206
+ expect(MongoClient.connect).toHaveBeenCalledWith(mockUrl);
207
+ });
208
+
209
+ it('upload() should resolve the upload promise', async () => {
210
+ const mockUrl = 'mock-url';
211
+ jest.spyOn(model as any, 'getUrl').mockImplementation(() => mockUrl);
212
+ const mockStream = new Readable();
213
+ mockStream._read = jest.fn();
214
+ data.stream = mockStream;
215
+ const result = model.upload(data);
216
+ await expect(result).resolves.toBeUndefined();
217
+ });
218
+
219
+ it('upload() should call GridFSBucket and openUploadStreamWithId', async () => {
220
+ const mockUrl = 'mock-url';
221
+ jest.spyOn(model as any, 'getUrl').mockImplementation(() => mockUrl);
222
+ const mockStream = new Readable();
223
+ mockStream._read = jest.fn();
224
+ data.stream = mockStream;
225
+ await model.upload(data);
226
+ expect(GridFSBucket).toHaveBeenCalledWith(expect.anything(), {
227
+ bucketName: mockConfig.collection,
228
+ });
229
+ expect(mockOpenUploadStreamWithId).toHaveBeenCalledWith(
230
+ data.id,
231
+ data.fileName,
232
+ {
233
+ contentType: data.mimeType,
234
+ },
235
+ );
236
+ });
237
+
238
+ it('upload() should call streamCallback if provided', async () => {
239
+ const mockUrl = 'mock-url';
240
+ jest.spyOn(model as any, 'getUrl').mockImplementation(() => mockUrl);
241
+ const mockStream = new Readable();
242
+ mockStream._read = jest.fn();
243
+ data.stream = mockStream;
244
+ await model.upload(data, options);
245
+ expect(options.streamCallback).toHaveBeenCalled();
246
+ });
247
+
248
+ it('upload() should call pipe of the provided data stream', async () => {
249
+ const mockUrl = 'mock-url';
250
+ jest.spyOn(model as any, 'getUrl').mockImplementation(() => mockUrl);
251
+ const mockStream = new Readable();
252
+ mockStream._read = jest.fn();
253
+ mockStream.pipe = jest.fn();
254
+ data.stream = mockStream;
255
+ await model.upload(data);
256
+ expect(mockStream.pipe).toHaveBeenCalled();
257
+ });
249
258
  });