Skip to content

Commit

Permalink
Fix web socket test
Browse files Browse the repository at this point in the history
  • Loading branch information
h3poteto committed Oct 5, 2023
1 parent 6135bc6 commit df10bcc
Showing 1 changed file with 58 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Parser } from '@/mastodon/web_socket'
import WS from 'isomorphic-ws'
import Entity from '@/entity'

const account: Entity.Account = {
Expand Down Expand Up @@ -87,53 +88,69 @@ describe('Parser', () => {
describe('parse', () => {
describe('message is heartbeat', () => {
describe('message is an object', () => {
const message = Buffer.alloc(0)
const message: WS.MessageEvent = {
type: 'message',
target: '' as any,
data: Buffer.alloc(0)
}

it('should be called', () => {
const spy = jest.fn()
parser.once('heartbeat', spy)
parser.parse(message, true)
parser.parse(message)
expect(spy).toHaveBeenCalledWith({})
})
})
describe('message is empty string', () => {
const message: string = ''
const message: WS.MessageEvent = {
type: 'message',
target: '' as any,
data: ''
}

it('should be called', () => {
const spy = jest.fn()
parser.once('heartbeat', spy)
parser.parse(Buffer.from(message), false)
parser.parse(message)
expect(spy).toHaveBeenCalledWith({})
})
})
})

describe('message is not json', () => {
describe('event is delete', () => {
const message = JSON.stringify({
event: 'delete',
payload: '12asdf34'
})
const message: WS.MessageEvent = {
type: 'message',
target: '' as any,
data: JSON.stringify({
event: 'delete',
payload: '12asdf34'
})
}

it('should be called', () => {
const spy = jest.fn()
parser.once('delete', spy)
parser.parse(Buffer.from(message), false)
parser.parse(message)
expect(spy).toHaveBeenCalledWith('12asdf34')
})
})
describe('event is not delete', () => {
const message = JSON.stringify({
event: 'event',
payload: '12asdf34'
})
const message: WS.MessageEvent = {
type: 'message',
target: '' as any,
data: JSON.stringify({
event: 'event',
payload: '12asdf34'
})
}

it('should be called', () => {
const error = jest.fn()
const deleted = jest.fn()
parser.once('error', error)
parser.once('delete', deleted)
parser.parse(Buffer.from(message), false)
parser.parse(message)
expect(error).toHaveBeenCalled()
expect(deleted).not.toHaveBeenCalled()
})
Expand All @@ -142,40 +159,52 @@ describe('Parser', () => {

describe('message is json', () => {
describe('event is update', () => {
const message = JSON.stringify({
event: 'update',
payload: JSON.stringify(status)
})
const message: WS.MessageEvent = {
type: 'message',
target: '' as any,
data: JSON.stringify({
event: 'update',
payload: JSON.stringify(status)
})
}
it('should be called', () => {
const spy = jest.fn()
parser.once('update', spy)
parser.parse(Buffer.from(message), false)
parser.parse(message)
expect(spy).toHaveBeenCalledWith(status)
})
})

describe('event is notification', () => {
const message = JSON.stringify({
event: 'notification',
payload: JSON.stringify(notification)
})
const message: WS.MessageEvent = {
type: 'message',
target: '' as any,
data: JSON.stringify({
event: 'notification',
payload: JSON.stringify(notification)
})
}
it('should be called', () => {
const spy = jest.fn()
parser.once('notification', spy)
parser.parse(Buffer.from(message), false)
parser.parse(message)
expect(spy).toHaveBeenCalledWith(notification)
})
})

describe('event is conversation', () => {
const message = JSON.stringify({
event: 'conversation',
payload: JSON.stringify(conversation)
})
const message: WS.MessageEvent = {
type: 'message',
target: '' as any,
data: JSON.stringify({
event: 'conversation',
payload: JSON.stringify(conversation)
})
}
it('should be called', () => {
const spy = jest.fn()
parser.once('conversation', spy)
parser.parse(Buffer.from(message), false)
parser.parse(message)
expect(spy).toHaveBeenCalledWith(conversation)
})
})
Expand Down

0 comments on commit df10bcc

Please sign in to comment.