You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

108 line
3.4 KiB

  1. import { container } from 'tsyringe';
  2. import path from 'path';
  3. import { promises as fs } from 'fs';
  4. import { Readable } from 'stream';
  5. import AppError from '../../../erros/AppError';
  6. import ExtractDataPDFMapaFolhaService from '../../../services/mapaFolha/ExtractDataPDFMapaFolhaService';
  7. import reciboPagamentoData from '../../resources/recibo-pagamento-data.json';
  8. describe('Mapa Folha - ExtractDataPDFMapaFolhaService', () => {
  9. afterEach(() => {
  10. jest.restoreAllMocks();
  11. });
  12. it('Deve ser capaz de extrair os dados do PDF do mapa da folha ', async () => {
  13. const extractDataPDFMapaFolha = container.resolve(ExtractDataPDFMapaFolhaService);
  14. const filePath = path.resolve(__dirname, '..', '..', 'resources', 'recibo-pagamento.pdf');
  15. const file = await fs.readFile(filePath);
  16. const { size } = await fs.stat(filePath);
  17. const readableStream = new Readable();
  18. const fileMulter: Express.Multer.File = {
  19. fieldname: 'pdf',
  20. originalname: 'report-mapa-folha.pdf',
  21. encoding: 'utf-8',
  22. mimetype: 'application/pdf',
  23. size,
  24. stream: readableStream,
  25. destination: filePath,
  26. filename: 'report-mapa-folha.pdf',
  27. path: filePath,
  28. buffer: file,
  29. };
  30. const data = await extractDataPDFMapaFolha.execute({
  31. files: [fileMulter],
  32. });
  33. expect(data).toEqual(expect.arrayContaining(reciboPagamentoData));
  34. });
  35. it('Deve gerar uma exceção caso não seja passado o arquivo ', async () => {
  36. const params = {} as { files: Express.Multer.File[] };
  37. const extractDataPDFMapaFolha = container.resolve(ExtractDataPDFMapaFolhaService);
  38. await expect(extractDataPDFMapaFolha.execute(params)).rejects.toBeInstanceOf(AppError);
  39. });
  40. it('Deve gerar uma exceção caso o arquivo não seja um PDF 1', async () => {
  41. const extractDataPDFMapaFolha = container.resolve(ExtractDataPDFMapaFolhaService);
  42. const filePath = path.resolve(__dirname, '..', '..', 'resources', 'recibo-pagamento-data.json');
  43. const file = await fs.readFile(filePath);
  44. const { size } = await fs.stat(filePath);
  45. const readableStream = new Readable();
  46. const fileMulter: Express.Multer.File = {
  47. fieldname: 'pdf',
  48. originalname: 'recibo-pagamento-data.json',
  49. encoding: 'utf-8',
  50. mimetype: 'application/json',
  51. size,
  52. stream: readableStream,
  53. destination: filePath,
  54. filename: 'recibo-pagamento-data.json',
  55. path: filePath,
  56. buffer: file,
  57. };
  58. await expect(extractDataPDFMapaFolha.execute({ files: [fileMulter] })).rejects.toBeInstanceOf(
  59. AppError,
  60. );
  61. });
  62. it('Não deve gerar uma exceção caso receba um PDF diferente do experado ', async () => {
  63. const extractDataPDFMapaFolha = container.resolve(ExtractDataPDFMapaFolhaService);
  64. const filePath = path.resolve(__dirname, '..', '..', 'resources', 'empty-pdf.pdf');
  65. const file = await fs.readFile(filePath);
  66. const { size } = await fs.stat(filePath);
  67. const readableStream = new Readable();
  68. const fileMulter: Express.Multer.File = {
  69. fieldname: 'pdf',
  70. originalname: 'empty-pdf.pdf',
  71. encoding: 'utf-8',
  72. mimetype: 'application/pdf',
  73. size,
  74. stream: readableStream,
  75. destination: filePath,
  76. filename: 'empty-pdf.pdf',
  77. path: filePath,
  78. buffer: file,
  79. };
  80. const data = await extractDataPDFMapaFolha.execute({
  81. files: [fileMulter],
  82. });
  83. expect(data).toEqual([]);
  84. });
  85. });