Skip to content

Commit

Permalink
finish service tests
Browse files Browse the repository at this point in the history
Signed-off-by: Danilo Romano <[email protected]>
  • Loading branch information
Danilo Romano committed Mar 16, 2022
1 parent 193b930 commit 0af8ddf
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions tests/unit/server/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,50 @@ describe("#Service - test service implementation", () => {

})

test("#createFileStream", async () => {
test("#getFileInfo", async () => {
const filename = "test.png"
const mockFilePath = `${publicDirectory}/${filename}`

const service = new Service();

jest.spyOn(fsPromises, fsPromises.access.name as "access").mockResolvedValue()

const result = await service.getFileInfo(filename)

const expectedResult = {
type: ".png",
name: mockFilePath
}

expect(expectedResult).toStrictEqual(result)



})

test("#getFileStream", async () => {
const filename = "video.mp4"
const readableStream = TestUtil.generateReadableStream([filename])
const [, extension] = filename.split(".")

const mockFilePath = `${publicDirectory}/${extension}`

const mockFilePath = `${publicDirectory}/${filename}`
const service = new Service();
jest.spyOn(fsPromises, fsPromises.access.name as "access").mockResolvedValue()
jest.spyOn(path, path.extname.name as "extname").mockReturnValue(extension)
jest.spyOn(path, path.join.name as "join").mockReturnValue(mockFilePath)

const service = new Service();

const { name, type } = await service.getFileInfo(filename)

jest.spyOn(fs, fs.createReadStream.name as "createReadStream").mockReturnValue(readableStream)

const expectedResult = {
stream:readableStream,
type: extension
}

const streamResult = await service.getFileStream(filename)

expect(path.extname).toHaveBeenCalledWith(mockFilePath)
expect(name).toEqual(mockFilePath)
expect(type).toEqual(extension)
expect(streamResult).toStrictEqual(expectedResult)
expect(fs.createReadStream).toHaveBeenCalledWith(mockFilePath)


})
Expand Down

0 comments on commit 0af8ddf

Please sign in to comment.