1 import { WebsocketState } from './WebsocketState'
3 describe('WebsocketState', () => {
4 let state: WebsocketState
7 state = new WebsocketState()
14 describe('attempt count', () => {
15 it('should increment attempts on close', () => {
18 expect(state.attemptCount).toBe(1)
22 expect(state.attemptCount).toBe(2)
25 it('should increment attempts on fail to fetch token', () => {
26 state.didFailToFetchToken()
28 expect(state.attemptCount).toBe(1)
30 state.didFailToFetchToken()
32 expect(state.attemptCount).toBe(2)
36 describe('connection', () => {
37 it('should not be connected initially', () => {
38 expect(state.isConnected).toBe(false)
41 it('should set connected on open', () => {
44 expect(state.isConnected).toBe(true)
48 describe('exponential backoff', () => {
49 it('should have minimum 2 second backoff if no attempts', () => {
50 expect(state.getBackoffWithoutJitter()).toBe(2000)
53 it('should return backoff time and limit to max backoff time', () => {
55 expect(state.getBackoffWithoutJitter()).toBe(2000)
58 expect(state.getBackoffWithoutJitter()).toBe(4000)
61 expect(state.getBackoffWithoutJitter()).toBe(8000)
64 expect(state.getBackoffWithoutJitter()).toBe(16000)
67 expect(state.getBackoffWithoutJitter()).toBe(32000)
70 expect(state.getBackoffWithoutJitter()).toBe(32000)
73 it('should reset attempts', () => {
75 expect(state.getBackoffWithoutJitter()).toBe(2000)
78 expect(state.getBackoffWithoutJitter()).toBe(4000)
81 expect(state.getBackoffWithoutJitter()).toBe(2000)
84 it('should get backoff with jitter', () => {
85 state.getJitterFactor = jest.fn().mockReturnValue(1)
88 expect(state.getBackoff()).toBe(2000)
90 state.getJitterFactor = jest.fn().mockReturnValue(1.5)
93 expect(state.getBackoff()).toBe(6000)