Skip to content

Commit 43acdbf

Browse files
committed
feat: 🎸 bugfix and add test
1 parent cb12fca commit 43acdbf

10 files changed

Lines changed: 2279 additions & 347 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ package-lock.json
77
data.json
88
lib/develop.ts
99
dev
10+
coverage

‎index.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ function initOcto (ctx: picgo) {
1616
throw new Error("Can't find github-plus config")
1717
}
1818
const ins = getIns(options)
19-
ins.authenticate()
2019
return ins
2120
}
2221

@@ -150,7 +149,6 @@ async function onRemove (files: ImgType[]) {
150149
if (rms.length === 0) return
151150
const self: picgo = this
152151
const ins = initOcto(self)
153-
ins.authenticate()
154152
const fail = []
155153
for (let i = 0; i < rms.length; i++) {
156154
const each = rms[i]

‎lib/helper.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import dayjs from 'dayjs'
22
import { ImgType, ImgZipType } from './interface'
3+
import slash from 'normalize-path'
4+
import { join } from 'path'
5+
36
export function getNow () {
47
return dayjs().format('YYYY-MM-DD hh:mm:ss')
58
}
@@ -30,3 +33,7 @@ export function unzip (
3033
type: ''
3134
}
3235
}
36+
37+
export function pathJoin (...arg) {
38+
return slash(join.apply(null, arg))
39+
}

‎lib/octokit.ts‎

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
import Octokit from '@octokit/rest'
2-
import { getNow } from './helper'
2+
import { getNow, pathJoin } from './helper'
33
import { PluginConfig, ImgType } from './interface'
44
import { join } from 'path'
55
import urlJoin from 'url-join'
66
import { ImgInfo } from 'picgo/dist/utils/interfaces'
7-
import slash from 'slash'
8-
// export default octokit
97

10-
function pathJoin (...arg) {
11-
return slash(join.apply(null, arg))
12-
}
13-
14-
class Octo {
8+
export class Octo {
159
owner: string = ''
1610
repo: string = ''
1711
branch: string = ''
1812
path: string = ''
1913
token: string = ''
2014
customUrl: string = ''
21-
octokit = new Octokit()
15+
octokit: Octokit = null
2216
constructor ({
2317
repo,
2418
branch,
@@ -34,11 +28,8 @@ class Octo {
3428
this.path = path
3529
this.token = token
3630
this.customUrl = customUrl
37-
}
38-
authenticate () {
39-
this.octokit.authenticate({
40-
type: 'token',
41-
token: this.token
31+
this.octokit = new Octokit({
32+
auth: token ? `token ${token}` : undefined
4233
})
4334
}
4435

@@ -66,9 +57,9 @@ class Octo {
6657
return { sha, tree }
6758
}
6859
async getDataJson (): Promise<{
69-
lastSync: string;
70-
data: any[];
71-
sha?: string;
60+
lastSync: string
61+
data: any[]
62+
sha?: string
7263
}> {
7364
const { owner, repo } = this
7465
const defaultRet = {
@@ -107,7 +98,6 @@ class Octo {
10798
}
10899
createDataJson (data) {
109100
const { owner, repo, branch, path } = this
110-
console.log(pathJoin(path, 'data.json'))
111101
return this.octokit.repos.createFile({
112102
owner,
113103
repo,
@@ -118,6 +108,7 @@ class Octo {
118108
})
119109
}
120110
async upload (img: ImgInfo) {
111+
/* istanbul ignore next */
121112
const { owner, repo, branch, path = '' } = this
122113
const { fileName } = img
123114
const d = await this.octokit.repos.createFile({
@@ -134,6 +125,7 @@ class Octo {
134125
sha: d.data.content.sha
135126
}
136127
}
128+
/* istanbul ignore next */
137129
throw d
138130
}
139131
removeFile (img: ImgType) {
@@ -152,13 +144,27 @@ class Octo {
152144
if (customUrl) {
153145
return urlJoin(customUrl, path, fileName)
154146
}
155-
return urlJoin(`https://raw.githubusercontent.com/`, owner, repo, branch, path, fileName)
147+
return urlJoin(
148+
`https://raw.githubusercontent.com/`,
149+
owner,
150+
repo,
151+
branch,
152+
path,
153+
fileName
154+
)
156155
}
157156
}
158157

159158
let ins: Octo = null
160159

161160
export function getIns (config: PluginConfig): Octo {
162161
if (ins) return ins
163-
return new Octo(config)
162+
ins = new Octo(config)
163+
return ins
164+
}
165+
166+
/* istanbul ignore next */
167+
export function clearIns () {
168+
// just for test
169+
ins = null
164170
}

‎package.json‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"build": "tsc -p .",
2121
"dev": "tsc -w -p .",
2222
"clean": "rm -rf dist",
23+
"test": "jest",
2324
"pub": "yarn clean && yarn build && release-it",
2425
"changelog": "npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0"
2526
},
@@ -31,19 +32,39 @@
3132
"author": "zzwing <zhengzwing@gmail.com>",
3233
"license": "MIT",
3334
"devDependencies": {
35+
"@types/jest": "^24.0.11",
3436
"@types/node": "^10.10.1",
35-
"electron": "^4.0.1",
36-
"nodemon": "^1.18.9",
37+
"electron": "^4.1.0",
38+
"jest": "^24.5.0",
39+
"nock": "^10.0.6",
3740
"picgo": "^1.2.1",
3841
"release-it": "^9.8.1",
42+
"ts-jest": "^24.0.0",
3943
"tslint": "^5.10.0",
4044
"tslint-config-standard": "^7.1.0",
4145
"typescript": "^3.0.3"
4246
},
4347
"dependencies": {
44-
"@octokit/rest": "^16.8.1",
48+
"@octokit/rest": "^16.18.1",
4549
"dayjs": "^1.8.0",
46-
"slash": "^2.0.0",
50+
"normalize-path": "^3.0.0",
4751
"url-join": "^4.0.0"
52+
},
53+
"jest": {
54+
"moduleFileExtensions": [
55+
"ts",
56+
"js"
57+
],
58+
"collectCoverageFrom": [
59+
"lib/*.ts"
60+
],
61+
"coverageDirectory": "./coverage/",
62+
"collectCoverage": true,
63+
"transform": {
64+
"^.+\\.(ts|tsx)$": "ts-jest"
65+
},
66+
"testMatch": [
67+
"**/test/*.test.(ts|tsx)"
68+
]
4869
}
4970
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`test getNow test getNow: now 1`] = `"2019-02-22 12:10:20"`;
4+
5+
exports[`test helper test unzip: unzip 1`] = `
6+
Object {
7+
"extname": "filename",
8+
"fileName": "filename",
9+
"height": 1000,
10+
"id": "fdsafdsafdsafasdf",
11+
"imgUrl": "",
12+
"sha": "fooiuiouiouio",
13+
"type": "",
14+
"width": 1000,
15+
}
16+
`;
17+
18+
exports[`test helper test zip: zip 1`] = `
19+
Object {
20+
"f": "filename",
21+
"h": 1000,
22+
"id": "fdsafdsafdsafasdf",
23+
"s": "fooiuiouiouio",
24+
"w": 1000,
25+
}
26+
`;

‎test/helper.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { pathJoin, zip, unzip, getNow } from '../lib/helper'
2+
describe('test helper', () => {
3+
it('test pathJson', () => {
4+
expect(pathJoin('path', 'to', 'dir')).toEqual('path/to/dir')
5+
expect(pathJoin('path', '../to', 'dir')).toEqual('to/dir')
6+
expect(pathJoin('path', '啦啦啦', '中文')).toEqual('path/啦啦啦/中文')
7+
})
8+
9+
it('test zip', () => {
10+
const str = zip({
11+
fileName: 'filename',
12+
width: 1000,
13+
height: 1000,
14+
id: 'fdsafdsafdsafasdf',
15+
sha: 'fooiuiouiouio'
16+
} as any)
17+
expect(str).toMatchSnapshot('zip')
18+
})
19+
it('test unzip', () => {
20+
const str = unzip({
21+
f: 'filename',
22+
w: 1000,
23+
h: 1000,
24+
id: 'fdsafdsafdsafasdf',
25+
s: 'fooiuiouiouio'
26+
} as any)
27+
expect(str).toMatchSnapshot('unzip')
28+
})
29+
})
30+
describe('test getNow', () => {
31+
const date = global.Date
32+
beforeEach(() => {
33+
const mockedDate = new Date(Date.UTC(2019, 1, 21, 16, 10, 20))
34+
global.Date = class extends Date {
35+
constructor () {
36+
super()
37+
return mockedDate
38+
}
39+
} as any
40+
})
41+
afterEach(() => {
42+
global.Date = date
43+
})
44+
it('test getNow', () => {
45+
const now = getNow()
46+
expect(now).toMatchSnapshot('now')
47+
})
48+
})

0 commit comments

Comments
 (0)