-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23615 from storybookjs/norbert/fix-server-channel…
…-options Core: Fix channelOptions for serverChannel
- Loading branch information
Showing
2 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
code/lib/core-server/src/utils/__tests__/server-channel.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import type { Server } from 'http'; | ||
import { Channel } from '@storybook/channels'; | ||
|
||
import { EventEmitter } from 'events'; | ||
import { stringify } from 'telejson'; | ||
import { getServerChannel, ServerChannelTransport } from '../get-server-channel'; | ||
|
||
describe('getServerChannel', () => { | ||
test('should return a channel', () => { | ||
const server = { on: jest.fn() } as any as Server; | ||
const result = getServerChannel(server); | ||
expect(result).toBeInstanceOf(Channel); | ||
}); | ||
|
||
test('should attach to the http server', () => { | ||
const server = { on: jest.fn() } as any as Server; | ||
getServerChannel(server); | ||
expect(server.on).toHaveBeenCalledWith('upgrade', expect.any(Function)); | ||
}); | ||
}); | ||
|
||
describe('ServerChannelTransport', () => { | ||
test('parses simple JSON', () => { | ||
const server = new EventEmitter() as any as Server; | ||
const socket = new EventEmitter(); | ||
const transport = new ServerChannelTransport(server); | ||
const handler = jest.fn(); | ||
transport.setHandler(handler); | ||
|
||
// @ts-expect-error (an internal API) | ||
transport.socket.emit('connection', socket); | ||
socket.emit('message', '"hello"'); | ||
|
||
expect(handler).toHaveBeenCalledWith('hello'); | ||
}); | ||
test('parses object JSON', () => { | ||
const server = new EventEmitter() as any as Server; | ||
const socket = new EventEmitter(); | ||
const transport = new ServerChannelTransport(server); | ||
const handler = jest.fn(); | ||
transport.setHandler(handler); | ||
|
||
// @ts-expect-error (an internal API) | ||
transport.socket.emit('connection', socket); | ||
socket.emit('message', JSON.stringify({ type: 'hello' })); | ||
|
||
expect(handler).toHaveBeenCalledWith({ type: 'hello' }); | ||
}); | ||
test('supports telejson cyclical data', () => { | ||
const server = new EventEmitter() as any as Server; | ||
const socket = new EventEmitter(); | ||
const transport = new ServerChannelTransport(server); | ||
const handler = jest.fn(); | ||
transport.setHandler(handler); | ||
|
||
// @ts-expect-error (an internal API) | ||
transport.socket.emit('connection', socket); | ||
|
||
const input: any = { a: 1 }; | ||
input.b = input; | ||
socket.emit('message', stringify(input)); | ||
|
||
expect(handler.mock.calls[0][0]).toMatchInlineSnapshot(` | ||
Object { | ||
"a": 1, | ||
"b": [Circular], | ||
} | ||
`); | ||
}); | ||
test('skips telejson classes and functions in data', () => { | ||
const server = new EventEmitter() as any as Server; | ||
const socket = new EventEmitter(); | ||
const transport = new ServerChannelTransport(server); | ||
const handler = jest.fn(); | ||
transport.setHandler(handler); | ||
|
||
// @ts-expect-error (an internal API) | ||
transport.socket.emit('connection', socket); | ||
|
||
const input = { a() {}, b: class {} }; | ||
socket.emit('message', stringify(input)); | ||
|
||
expect(handler.mock.calls[0][0].a).toEqual(expect.any(String)); | ||
expect(handler.mock.calls[0][0].b).toEqual(expect.any(String)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters