WebSocket.test.js 974 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import WebSocket from '../src/WebSocket'
  2. let connectSocket, onOpen, onClose, onMessage, onError
  3. beforeEach(() => {
  4. onOpen = jest.fn()
  5. onClose = jest.fn()
  6. onMessage = jest.fn()
  7. onError = jest.fn()
  8. connectSocket = jest.fn(() => {
  9. return {
  10. onOpen,
  11. onClose,
  12. onMessage,
  13. onError
  14. }
  15. })
  16. global.wx = {
  17. connectSocket
  18. }
  19. })
  20. describe('WebSocket', () => {
  21. it('constructor check invalid url', () => {
  22. expect(() => {
  23. const ws = new WebSocket()
  24. }).toThrow()
  25. expect(() => {
  26. const ws = new WebSocket('http://foo.bar')
  27. }).toThrow()
  28. })
  29. it('contructor basic', () => {
  30. const url = 'wss://foo.bar'
  31. const ws = new WebSocket(url)
  32. expect(onOpen).toHaveBeenCalledTimes(1)
  33. expect(onClose).toHaveBeenCalledTimes(1)
  34. expect(onMessage).toHaveBeenCalledTimes(1)
  35. expect(onClose).toHaveBeenCalledTimes(1)
  36. expect(connectSocket).toBeCalledWith({
  37. url,
  38. protocols: []
  39. })
  40. })
  41. })