Skip to content

Commit

Permalink
test: 更新 resource api 相关的单元测试验证单独修改 file 会重命名标题的错误修复
Browse files Browse the repository at this point in the history
  • Loading branch information
rxliuli committed Apr 12, 2022
1 parent e97ee75 commit a123aad
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions libs/joplin-api/test/api/ResourceApi.test.ts
@@ -1,5 +1,5 @@
import { resourceApi } from '../../src'
import { createReadStream, mkdirp, pathExistsSync, remove, writeFile, writeFileSync } from 'fs-extra'
import { createReadStream, mkdirp, pathExistsSync, remove, stat, writeFile, writeFileSync } from 'fs-extra'
import { resolve } from 'path'
import { createTestResource } from './CreateTestResource'
import * as path from 'path'
Expand All @@ -12,6 +12,11 @@ describe('test ResourceApi', () => {
afterAll(async () => {
await resourceApi.remove(id)
})
const tempPath = path.resolve(__dirname, '.temp')
beforeEach(async () => {
await remove(tempPath)
await mkdirp(tempPath)
})
it('test list', async () => {
const res = await resourceApi.list({ fields: ['id', 'title'] })
console.log(res)
Expand Down Expand Up @@ -43,11 +48,6 @@ describe('test ResourceApi', () => {
})
})
describe('test update', () => {
const tempPath = path.resolve(__dirname, '.temp')
beforeEach(async () => {
await remove(tempPath)
await mkdirp(tempPath)
})
it('basic example', async () => {
const title = `new title ${Date.now()}`
const updateRes = await resourceApi.update({ id, title })
Expand All @@ -71,6 +71,16 @@ describe('test ResourceApi', () => {
const res = await resourceApi.fileByResourceId(id)
expect(res.toString()).toBe(content)
})
it('update file only', async () => {
const content = 'test'
const txtPath = path.resolve(tempPath, 'test.txt')
await writeFile(txtPath, content)
const { title } = await resourceApi.get(id)
await resourceApi.update({ id, data: createReadStream(txtPath) })
const res = await resourceApi.fileByResourceId(id)
expect(res.toString()).toBe(content)
expect((await resourceApi.get(id)).title).toBe(title)
})
})
it('test remove ', async () => {
const id = (await createTestResource()).id
Expand All @@ -80,12 +90,13 @@ describe('test ResourceApi', () => {
it('test fileByResourceId', async () => {
const res = await resourceApi.fileByResourceId(id)
console.log(typeof res)
const path = resolve(__dirname, '../resource/resourcesByFileId.png')
writeFileSync(path, res)
const path = resolve(tempPath, 'resourcesByFileId.png')
await writeFile(path, res)
expect(pathExistsSync(path)).toBeTruthy()
})
it('测试获取附件资源的大小', async () => {
console.log(await resourceApi.get(id))
console.log(await resourceApi.get(id, ['id', 'title', 'size']))
const res = await resourceApi.get(id, ['id', 'title', 'size'])
const stats = await stat(path.resolve(__dirname, '../resource/resourcesByFileId.png'))
expect(res.size).toEqual(stats.size)
})
})

0 comments on commit a123aad

Please sign in to comment.