From 63b690f7afe6d1892d55699f681a063b0760e539 Mon Sep 17 00:00:00 2001 From: Andre Christoga Pramaditya Date: Fri, 11 Mar 2022 23:49:04 +0800 Subject: [PATCH] feat(jest): 58% coverage for TextileFileSystem.ts --- .../Files/test/TextileFileSystem.test.ts | 81 +++++++++++-------- utilities/assets/svg-image.svg | 5 ++ 2 files changed, 54 insertions(+), 32 deletions(-) create mode 100644 utilities/assets/svg-image.svg diff --git a/libraries/Files/test/TextileFileSystem.test.ts b/libraries/Files/test/TextileFileSystem.test.ts index 931392a43a..a62c84b3ff 100644 --- a/libraries/Files/test/TextileFileSystem.test.ts +++ b/libraries/Files/test/TextileFileSystem.test.ts @@ -1,43 +1,60 @@ +import fs from 'fs' import Vue from 'vue' import * as TFS from '../TextileFileSystem' import { Fil } from '../Fil' import TextileManager from '~/libraries/Textile/TextileManager' -// Vue.prototype.$TextileManager = new TextileManager() +Vue.prototype.$TextileManager = new TextileManager() describe('', () => { - // const TFSConstructor = new TFS.TextileFileSystem() - // const inst = TFSConstructor - // const state = inst.bucket + const TFSConstructor = new TFS.TextileFileSystem() + const inst = TFSConstructor + const state = inst.bucket test('get bucket TFS', () => { expect('').toEqual('') - // const result: any = inst.bucket - // expect(result).toEqual(state) + const result: any = inst.bucket + expect(result).toEqual(state) + }) + test('upload file TFS', async () => { + const TMConstructor = Vue.prototype.$TextileManager + TMConstructor.bucket = jest.fn() + TMConstructor.bucket.pushFile = jest.fn() + TMConstructor.bucket.pushFile.mockReturnValueOnce({ + path: { + path: '0x0aef', + }, + }) + const testFile = new File(['hello'], 'test_fil.txt', { + type: 'text/plain', + }) + const file = new Fil(testFile) + await inst.uploadFile(file) + expect(TMConstructor.bucket.pushFile).toHaveBeenCalled() + }) + test('remove file TFS', async () => { + const TMConstructor = Vue.prototype.$TextileManager + TMConstructor.bucket = jest.fn() + TMConstructor.bucket.removeFile = jest.fn() + await inst.removeFile('TestFile.png') + expect(TMConstructor.bucket.removeFile).toHaveBeenCalled() + }) + test.skip('upload file TFS svg', async () => { + const TMConstructor = Vue.prototype.$TextileManager + TMConstructor.bucket = jest.fn() + TMConstructor.bucket.pushFile = jest.fn() + TMConstructor.bucket.pushFile.mockReturnValueOnce({ + path: { + path: '0x0aef', + }, + }) + const fileBuffer = fs.readFileSync('utilities/assets/svg-image.svg', { + flag: 'r', + }) + const testFile = new File([fileBuffer as BlobPart], 'svg-image.svg', { + type: 'image/svg', + }) + const file = new Fil(testFile) + await inst.uploadFile(file) + expect(TMConstructor.bucket.pushFile).toHaveBeenCalled() }) - // test('upload file TFS', async () => { - // const TMConstructor = Vue.prototype.$TextileManager - // TMConstructor.bucket = jest.fn() - // TMConstructor.bucket.pushFile = jest.fn() - // TMConstructor.bucket.pushFile.mockReturnValueOnce({ - // path: { - // path: '0x0aef', - // }, - // }) - // const mockFileData = { - // name: 'TestFile.png', - // hash: '0x0aef', - // size: 42345, - // description: 'Test file description', - // } - // const file = new Fil(mockFileData) - // await inst.uploadFile(file) - // expect(TMConstructor.bucket.pushFile).toHaveBeenCalled() - // }) - // test('remove file TFS', async () => { - // const TMConstructor = Vue.prototype.$TextileManager - // TMConstructor.bucket = jest.fn() - // TMConstructor.bucket.removeFile = jest.fn() - // await inst.removeFile('TestFile.png') - // expect(TMConstructor.bucket.removeFile).toHaveBeenCalled() - // }) }) diff --git a/utilities/assets/svg-image.svg b/utilities/assets/svg-image.svg new file mode 100644 index 0000000000..e6093091c4 --- /dev/null +++ b/utilities/assets/svg-image.svg @@ -0,0 +1,5 @@ + + + + 410 +