|
| 1 | +import { StreamableFile } from '@nestjs/common'; |
1 | 2 | import { Test, TestingModule } from '@nestjs/testing';
|
| 3 | +import { Readable } from 'stream'; |
2 | 4 |
|
3 | 5 | import { AppController } from './app.controller';
|
4 | 6 | import { AppService } from './app.service';
|
5 | 7 |
|
6 | 8 | describe('AppController', () => {
|
7 | 9 | let app: TestingModule;
|
| 10 | + let appController: AppController; |
8 | 11 |
|
9 | 12 | beforeAll(async () => {
|
10 | 13 | app = await Test.createTestingModule({
|
11 | 14 | controllers: [AppController],
|
12 |
| - providers: [AppService], |
| 15 | + providers: [ |
| 16 | + { |
| 17 | + provide: AppService, |
| 18 | + useValue: { |
| 19 | + getData: jest |
| 20 | + .fn() |
| 21 | + .mockReturnValue({ message: 'AppService#getData' }), |
| 22 | + getFileStream: jest.fn().mockImplementation(() => { |
| 23 | + const readable = new Readable(); |
| 24 | + readable.push(Buffer.from('Hello World!')); |
| 25 | + readable.push(null); |
| 26 | + return readable; |
| 27 | + }), |
| 28 | + }, |
| 29 | + }, |
| 30 | + ], |
13 | 31 | }).compile();
|
| 32 | + appController = app.get<AppController>(AppController); |
14 | 33 | });
|
15 | 34 |
|
16 | 35 | describe('getData', () => {
|
17 | 36 | it('should return "Welcome to file-up-and-down-sample!"', () => {
|
18 |
| - const appController = app.get<AppController>(AppController); |
19 | 37 | expect(appController.getData()).toEqual({
|
20 |
| - message: 'Welcome to file-up-and-down-sample!', |
| 38 | + message: 'AppService#getData', |
21 | 39 | });
|
22 | 40 | });
|
23 | 41 | });
|
| 42 | + describe('postFile', () => { |
| 43 | + it('should return the file.originalname property', () => { |
| 44 | + expect( |
| 45 | + appController.postFile({ |
| 46 | + originalname: 'testfile.json', |
| 47 | + buffer: Buffer.from('Hello'), |
| 48 | + destination: '', |
| 49 | + fieldname: 'file', |
| 50 | + filename: 'new-file.json', |
| 51 | + mimetype: 'text/plain', |
| 52 | + encoding: '7bit', |
| 53 | + path: '', |
| 54 | + size: 5091, |
| 55 | + stream: new Readable(), |
| 56 | + }), |
| 57 | + ).toEqual('testfile.json'); |
| 58 | + }); |
| 59 | + }); |
| 60 | + describe('getStreamableFile', () => { |
| 61 | + it('should return a streamable file', () => { |
| 62 | + expect(appController.getStreamableFile()).toEqual( |
| 63 | + new StreamableFile(Buffer.from('Hello World!')), |
| 64 | + ); |
| 65 | + }); |
| 66 | + }); |
| 67 | + describe('getFileViaResStream', () => { |
| 68 | + it('should pipe the response object through the readStream', (done) => { |
| 69 | + // mocking all of the writable methods |
| 70 | + const resMock: NodeJS.WritableStream = { |
| 71 | + end: done, |
| 72 | + write: jest.fn(), |
| 73 | + addListener: jest.fn(), |
| 74 | + emit: jest.fn(), |
| 75 | + eventNames: jest.fn(), |
| 76 | + getMaxListeners: jest.fn(), |
| 77 | + listenerCount: jest.fn(), |
| 78 | + listeners: jest.fn(), |
| 79 | + off: jest.fn(), |
| 80 | + on: jest.fn(), |
| 81 | + once: jest.fn(), |
| 82 | + prependListener: jest.fn(), |
| 83 | + prependOnceListener: jest.fn(), |
| 84 | + rawListeners: jest.fn(), |
| 85 | + removeAllListeners: jest.fn(), |
| 86 | + removeListener: jest.fn(), |
| 87 | + setMaxListeners: jest.fn(), |
| 88 | + writable: true, |
| 89 | + }; |
| 90 | + // keep in mind this is the absolute **minimum** for testing, and should be expanded upon with better tests later |
| 91 | + appController.getFileViaResStream(resMock); |
| 92 | + expect(resMock.on).toBeCalled(); |
| 93 | + }); |
| 94 | + }); |
24 | 95 | });
|
0 commit comments