Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable yjs connecting status in tests2 #2111 #2453

4 changes: 1 addition & 3 deletions src/e2e/puppeteer/setup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable import/prefer-default-export */
import chalk from 'chalk'
import { Browser, ConsoleMessage, Device, Page } from 'puppeteer'
import { WEBSOCKET_TIMEOUT } from '../../constants'
import sleep from '../../util/sleep'

// eslint-disable-next-line @typescript-eslint/no-namespace
Expand Down Expand Up @@ -78,9 +77,8 @@ const setup = async ({
await page.waitForFunction(() => !document.getElementById('skip-tutorial'))
}

// wait for YJS to give up connecting to WebsocketProvider
// add 500ms for hamburger-menu animation to complete
await sleep(WEBSOCKET_TIMEOUT + 500)
await sleep(500)
}

beforeEach(setup, 60000)
Expand Down
9 changes: 7 additions & 2 deletions src/stores/offlineStatusStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import { HocuspocusProviderWebsocket } from '@hocuspocus/provider'
import OfflineStatus from '../@types/OfflineStatus'
import WebsocketStatus from '../@types/WebsocketStatus'
import { WEBSOCKET_TIMEOUT } from '../constants'
import isE2E from '../util/isE2E'
import reactMinistore from './react-ministore'

/** A store that tracks a derived websocket connection status that includes special statuses for initialization (preconnecting), the first connection attempt (connecting), and offline mode (offline). There are a couple places where offlineStatusStore.update is called directly in order to skip preconnecting. See: OfflineStatus type for description of all possible statuses. */
export const offlineStatusStore = reactMinistore<OfflineStatus>('preconnecting')

/** Define timeouts based on whether we're in e2e tests or not. We're somewhat limited since these run post-build but there might be cleaner ways to do this. */
const initialOfflineTimeout = isE2E() ? 0 : 500
const offlineTimeout = isE2E() ? 0 : WEBSOCKET_TIMEOUT

/** Enter a connecting state and then switch to offline after a delay. */
const startConnecting = () => {
stopConnecting()
Expand All @@ -17,7 +22,7 @@ const startConnecting = () => {
offlineTimer = setTimeout(() => {
offlineTimer = null
offlineStatusStore.update('offline')
}, WEBSOCKET_TIMEOUT)
}, offlineTimeout)
}

/** Clears the timer, indicating either that we have connected to the websocket server, or have entered offline mode as the client continues connecting in the background. */
Expand Down Expand Up @@ -61,7 +66,7 @@ export const init = (websocket: HocuspocusProviderWebsocket) => {
// Start connecting to populate offlineStatusStore.
// This must done in an init function that is called in app initalize, otherwise @sinonjs/fake-timers are not yet set and createTestApp tests break.
// TODO: Why does deferring websocketProviderPermissions.connect() to init break tests?
offlineTimer = setTimeout(startConnecting, 500)
offlineTimer = setTimeout(startConnecting, initialOfflineTimeout)
}

export default offlineStatusStore
8 changes: 8 additions & 0 deletions src/util/__tests__/isE2E.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import isE2E from '../isE2E'

// Navigator.webdriver is readonly so we probably cannot test the true path.
describe('isE2e', () => {
it('should return false if webdriver is falsy', () => {
expect(isE2E()).toBe(false)
})
})
7 changes: 7 additions & 0 deletions src/util/isE2E.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Returns true if we're in the e2e tests.
* @returns Boolean.
*/
const isE2E = () => !!navigator.webdriver

export default isE2E