Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
802 views
in Technique[技术] by (71.8m points)

jestjs - Test doesn't pass - project using typescript

I have a typescript project that I'm using TDD I have a test doesn't not pass I don't know how to solve it the code in the github: https://github.com/tarcisiocorte/maskNotificationApi I will appreciate if somebody give me a help The error:

● Upload Controller ? should calls UploadFile when valid data is provided

expect(jest.fn()).toHaveBeenCalledWith(...expected)

Expected: "image.png"

Number of calls: 0

  111 |     }
  112 |     const httpResponse = await sut.handle(httpRequest)
> 113 |     expect(uploadSpy).toHaveBeenCalledWith('image.png')
      |                       ^
  114 |     //expect(resultHash).toBe('valid_hash')
  115 |   })
  116 |   test('should return statusCode 200 and save data if valid data is fully provided', async () => {

  at Object.<anonymous> (tests/upload.spec.ts:113:23)

The upload.spec.ts

import { TokenExpiredError } from 'jsonwebtoken'
import { UploadController } from '../src/presentation/controller/UploadController'
import { PhotoDataModel } from './domain/models/photodatamodel'
import { AddPhotoData, AddPhotoDataModel } from './domain/usecases/add-photodata'
import { UploadFile } from './domain/usecases/upload-file'

const makeAddPhotoData = (): AddPhotoData => {
  class AddPhotoDataStub implements AddPhotoData {
    async add(photoData: AddPhotoDataModel): Promise<PhotoDataModel> {
      const fakePhotoData = {
        id: 'valid_id',
        companyKey: 'any_company_key',
        userKey: 'an_user_key',
        image: 'image.png'
      }
      return new Promise(resolve => resolve(fakePhotoData))
    }
  }
  return new AddPhotoDataStub()
}

const makeUploadFile = (): UploadFile => {
  class UploadFileStub implements UploadFile {
    async upload(file: string): Promise<string> {
      const fakeFileName: string = "valid_hash"
      return new Promise(resolve => resolve(fakeFileName))
    }
  }
  return new UploadFileStub()
}

interface CreateSutTypes {
  sut: UploadController
  addPhotoDataStub: AddPhotoData
  uploadFileStub: UploadFile
}

const createSut = (): CreateSutTypes => {
  const addPhotoDataStub = makeAddPhotoData()
  const uploadFileStub = makeUploadFile()
  const sut = new UploadController(addPhotoDataStub, uploadFileStub)
  return {
    sut,
    addPhotoDataStub,
    uploadFileStub
  }
}

describe('Upload Controller', () => {
  test('should return 400 if no image is provided', async () => {
    const { sut } = createSut()
    const httpRequest = {
      body: {
        companyKey: 'a_company_key',
        userKey: 'an_user_key'
      }
    }
    const httpResponse = await sut.handle(httpRequest)
    expect(httpResponse.statusCode).toBe(400)
    expect(httpResponse.body).toEqual(new Error('image'))
  })

  test('should return 400 if no companyKey is provided', async () => {
    const { sut } = createSut()
    const httpRequest = {
      body: {
        userKey: 'an_user_key',
        image: 'any_image'
      }
    }
    const httpResponse = await sut.handle(httpRequest)
    expect(httpResponse.statusCode).toBe(400)
    expect(httpResponse.body).toEqual(new Error('companyKey'))
  })

  test('should return 400 if no userKey is provided', async () => {
    const { sut } = createSut()
    const httpRequest = {
      body: {
        companyKey: 'a_company_key',
        image: 'any_image'
      }
    }
    const httpResponse = await sut.handle(httpRequest)
    expect(httpResponse.statusCode).toBe(400)
    expect(httpResponse.body).toEqual(new Error('userKey'))
  })

  test('should return 400 if the image is not .JPG or .PNG', async () => {
    const { sut } = createSut()
    const httpRequest = {
      body: {
        companyKey: 'any_company_key',
        userKey: 'any_user',
        image: 'image.bmp'
      }
    }
    const httpResponse = await sut.handle(httpRequest)
    expect(httpResponse.statusCode).toBe(400)
    expect(httpResponse.body).toEqual(new Error('The image has not the correct format'))
  })
  test('should calls UploadFile when valid data is provided', async () => {
    const { sut, uploadFileStub } = createSut()
    const uploadSpy = jest.spyOn(uploadFileStub, 'upload')
    const httpRequest = {
      body: {
        companyKey: 'any_company_key',
        userKey: 'any_user',
        image: 'image.png'
      }
    }
    const httpResponse = await sut.handle(httpRequest)
    expect(uploadSpy).toHaveBeenCalledWith('image.png')
    //expect(resultHash).toBe('valid_hash')
  })
  test('should return statusCode 200 and save data if valid data is fully provided', async () => {
    const { sut, addPhotoDataStub } = createSut()
    const photoData = jest.spyOn(addPhotoDataStub, 'add')
    const httpRequest = {
      body: {
        companyKey: 'any_company_key',
        userKey: 'any_user',
        image: 'image.jpg'
      }
    }
    var httpResponse = await sut.handle(httpRequest)
    expect(httpResponse.statusCode).toBe(200)
    expect(photoData).toHaveBeenCalledWith({
      companyKey: 'any_company_key',
      userKey: 'any_user',
      image: 'image.jpg'
    })
  })
})

the production file uploadController.ts

import { AddPhotoData } from '@/domain/usecases/add-photodata'
import { UploadFile } from '@/domain/usecases/upload-file'
import { badRequest, HttpRequest, HttpResponse, ok } from '../web'

export class UploadController {
  private readonly addPhotoData: AddPhotoData
  private readonly uploadFile: UploadFile
  constructor(addPhotoData: AddPhotoData, uploadFile: UploadFile) {
    this.addPhotoData = addPhotoData
    this.uploadFile = uploadFile
  }

  async handle(httpRequest: HttpRequest): Promise<HttpResponse> {
    if (!httpRequest.body.companyKey) {
      return badRequest(new Error('companyKey'))
    }
    if (!httpRequest.body.image) {
      return badRequest(new Error('image'))
    }

    if (!httpRequest.body.userKey) {
      return badRequest(new Error('userKey'))
    }

    const { companyKey, userKey, image } = httpRequest.body
    if (!httpRequest.body.image.includes('.jpg', '.png')) {
      return badRequest(new Error('The image has not the correct format'))
    }

    const hashFileName = this.uploadFile.upload(httpRequest.body.image)

    const photoData = await this.addPhotoData.add({ companyKey, userKey, image })
    return ok(photoData)
  }
}
question from:https://stackoverflow.com/questions/65911417/test-doesnt-pass-project-using-typescript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...