Skip to content

Commit

Permalink
feat(test-utils): add receiver api
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 22, 2020
1 parent da65008 commit a0fd831
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 17 deletions.
42 changes: 27 additions & 15 deletions packages/test-utils/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppOptions, App, Sender, Server, ContextType, Meta } from 'koishi-core'
import { AppOptions, App, Sender, Server, ContextType, Meta, FileInfo } from 'koishi-core'
import { MockedServer, RequestParams, RequestData, RequestHandler } from './mocks'
import { Session, createMessageMeta } from './session'
import { BASE_SELF_ID } from './utils'
Expand Down Expand Up @@ -49,23 +49,35 @@ export class MockedApp extends App {
}

receiveFriendRequest (userId: number, flag = 'flag') {
this.receive({
postType: 'request',
requestType: 'friend',
userId,
flag,
})
this.receive({ postType: 'request', requestType: 'friend', userId, flag })
}

receiveGroupRequest (userId: number, subType: 'add' | 'invite', groupId = 10000, flag = 'flag') {
this.receive({
postType: 'request',
requestType: 'group',
subType,
userId,
groupId,
flag,
})
this.receive({ postType: 'request', requestType: 'group', subType, userId, groupId, flag })
}

receiveGroupUpload (file: FileInfo, userId: number, groupId = 10000) {
this.receive({ postType: 'notice', noticeType: 'group_upload', file, userId, groupId })
}

receiveGroupAdmin (subType: 'set' | 'unset', userId: number, groupId = 10000) {
this.receive({ postType: 'notice', noticeType: 'group_admin', subType, userId, groupId })
}

receiveGroupIncrease (subType: 'approve' | 'invite', userId: number, groupId = 10000, operatorId = 1000) {
this.receive({ postType: 'notice', noticeType: 'group_increase', subType, userId, groupId, operatorId })
}

receiveGroupDecrease (subType: 'leave' | 'kick' | 'kick_me', userId: number, groupId = 10000, operatorId = 1000) {
this.receive({ postType: 'notice', noticeType: 'group_decrease', subType, userId, groupId, operatorId })
}

receiveGroupBan (subType: 'ban' | 'lift_ban', duration: number, userId: number, groupId = 10000, operatorId = 1000) {
this.receive({ postType: 'notice', noticeType: 'group_ban', subType, userId, groupId, operatorId, duration })
}

receiveFriendAdd (userId: number) {
this.receive({ postType: 'notice', noticeType: 'friend_add', userId })
}

receiveMessage (meta: Meta): Promise<void>
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/tests/__snapshots__/app.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Mocked Server Implementation shouldMatchSnapshot 1`] = `
exports[`Sender shouldMatchSnapshot 1`] = `
Array [
"send_private_msg",
Object {
Expand Down
130 changes: 129 additions & 1 deletion packages/test-utils/tests/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ beforeAll(() => app.start())

afterAll(() => app.stop())

describe('Mocked Server Implementation', () => {
describe('Sender', () => {
test('shouldHaveLastRequest', async () => {
await app.sender.sendPrivateMsgAsync(123, 'foo')
app.shouldHaveLastRequest('send_private_msg')
Expand Down Expand Up @@ -45,3 +45,131 @@ describe('Mocked Server Implementation', () => {
await expect(app.sender.sendPrivateMsg(123, 'foo')).resolves.toBe(321)
})
})

describe('Receiver', () => {
test('receiveFriendRequest', async () => {
const mock = jest.fn()
app.receiver.on('request/friend', mock)
app.receiveFriendRequest(123)
expect(mock).toBeCalledTimes(1)
expect(mock).toBeCalledWith({
$approve: expect.anything(),
$reject: expect.anything(),
postType: 'request',
requestType: 'friend',
userId: 123,
selfId: 514,
flag: 'flag',
})
})

test('receiveGroupRequest', async () => {
const mock = jest.fn()
app.receiver.on('request/group/add', mock)
app.receiveGroupRequest(123, 'add')
expect(mock).toBeCalledTimes(1)
expect(mock).toBeCalledWith({
$approve: expect.anything(),
$reject: expect.anything(),
postType: 'request',
requestType: 'group',
subType: 'add',
userId: 123,
selfId: 514,
groupId: 10000,
flag: 'flag',
})
})

test('receiveGroupUpload', async () => {
const mock = jest.fn()
app.receiver.on('group-upload', mock)
app.receiveGroupUpload({} as any, 123)
expect(mock).toBeCalledTimes(1)
expect(mock).toBeCalledWith({
postType: 'notice',
noticeType: 'group_upload',
userId: 123,
selfId: 514,
groupId: 10000,
file: {},
})
})

test('receiveGroupAdmin', async () => {
const mock = jest.fn()
app.receiver.on('group-admin/set', mock)
app.receiveGroupAdmin('set', 123)
expect(mock).toBeCalledTimes(1)
expect(mock).toBeCalledWith({
postType: 'notice',
noticeType: 'group_admin',
subType: 'set',
userId: 123,
selfId: 514,
groupId: 10000,
})
})

test('receiveGroupIncrease', async () => {
const mock = jest.fn()
app.receiver.on('group-increase/invite', mock)
app.receiveGroupIncrease('invite', 123)
expect(mock).toBeCalledTimes(1)
expect(mock).toBeCalledWith({
postType: 'notice',
noticeType: 'group_increase',
subType: 'invite',
userId: 123,
selfId: 514,
groupId: 10000,
operatorId: 1000,
})
})

test('receiveGroupDecrease', async () => {
const mock = jest.fn()
app.receiver.on('group-decrease/kick', mock)
app.receiveGroupDecrease('kick', 123)
expect(mock).toBeCalledTimes(1)
expect(mock).toBeCalledWith({
postType: 'notice',
noticeType: 'group_decrease',
subType: 'kick',
userId: 123,
selfId: 514,
groupId: 10000,
operatorId: 1000,
})
})

test('receiveGroupBan', async () => {
const mock = jest.fn()
app.receiver.on('group-ban/ban', mock)
app.receiveGroupBan('ban', 60, 123)
expect(mock).toBeCalledTimes(1)
expect(mock).toBeCalledWith({
postType: 'notice',
noticeType: 'group_ban',
subType: 'ban',
userId: 123,
selfId: 514,
groupId: 10000,
operatorId: 1000,
duration: 60,
})
})

test('receiveFriendAdd', async () => {
const mock = jest.fn()
app.receiver.on('friend-add', mock)
app.receiveFriendAdd(123)
expect(mock).toBeCalledTimes(1)
expect(mock).toBeCalledWith({
postType: 'notice',
noticeType: 'friend_add',
userId: 123,
selfId: 514,
})
})
})

0 comments on commit a0fd831

Please sign in to comment.