Skip to content

Commit

Permalink
test: mock utils
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 23, 2020
1 parent 5d4d7fe commit c46f9eb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
8 changes: 4 additions & 4 deletions packages/koishi-core/src/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ export class Sender {
protected _get: (action: string, params: Record<string, any>) => Promise<CQResponse>

constructor (public app: App) {
const { type } = app.options
const { type, token, server } = app.options
if (type === 'http') {
this._get = async (action, params) => {
const headers = {} as any
if (app.options.token) {
headers.Authorization = `Token ${app.options.token}`
if (token) {
headers.Authorization = `Token ${token}`
}
const uri = new URL(action, this.app.options.server).href
const uri = new URL(action, server).href
const { data } = await axios.get(uri, { params, headers })
return data
}
Expand Down
2 changes: 2 additions & 0 deletions packages/test-utils/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,5 @@ export class MockedApp extends App {
return new Session(this, type, userId, ctxId)
}
}

export { MockedApp as App }
4 changes: 2 additions & 2 deletions packages/test-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as utils from './random'
import * as utils from './koishi'

export { utils }

Expand All @@ -11,6 +11,6 @@ export * from './session'

jest.mock('koishi-utils', () => {
const utils1 = jest.requireActual('koishi-utils')
const utils2 = jest.requireActual('./random')
const utils2 = jest.requireActual('./koishi')
return { ...utils1, ...utils2 }
})
File renamed without changes.
26 changes: 26 additions & 0 deletions packages/test-utils/tests/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { utils } from '../src'
import * as _utils from 'koishi-utils'

test('randomPick', () => {
utils.randomPick.mockIndex(1)
utils.randomPick.mockIndexOnce(0)
const source = ['a', 'b', 'c']
expect(_utils.randomPick(source)).toEqual('a')
expect(_utils.randomPick(source)).toEqual('b')
})

test('randomSplice', () => {
utils.randomSplice.mockIndex(1)
utils.randomSplice.mockIndexOnce(0)
const source = ['a', 'b', 'c']
expect(_utils.randomSplice(source)).toEqual('a')
expect(_utils.randomSplice(source)).toEqual('c')
})

test('randomMultiPick', () => {
utils.randomMultiPick.mockIndices(0)
utils.randomMultiPick.mockIndicesOnce(1, 2)
const source = ['a', 'b', 'c']
expect(_utils.randomMultiPick(source, 2)).toEqual(['b', 'c'])
expect(_utils.randomMultiPick(source, 1)).toEqual(['a'])
})

0 comments on commit c46f9eb

Please sign in to comment.