test: add basic memory leak test (#1650)

* test: add basic memory leak test

* try to fix test
This commit is contained in:
Nolan Lawson 2019-11-23 19:11:15 -08:00 committed by GitHub
parent 14762c4a5f
commit 95ef639b21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 6 deletions

View file

@ -2,8 +2,8 @@
const callbacks = {} const callbacks = {}
if (process.browser && process.env.NODE_ENV !== 'production') { if (process.browser) {
window.delegateCallbacks = callbacks window.__delegateCallbacks = callbacks
} }
function onEvent (e) { function onEvent (e) {

View file

@ -2,8 +2,8 @@ import EventEmitter from 'events-light'
export const eventBus = new EventEmitter() export const eventBus = new EventEmitter()
if (process.browser && process.env.NODE_ENV !== 'production') { if (process.browser) {
window.eventBus = eventBus window.__eventBus = eventBus
} }
export function on (eventName, component, method) { export function on (eventName, component, method) {

View file

@ -4,8 +4,8 @@ const DEBOUNCE_DELAY = 700
const listeners = new Set() const listeners = new Set()
if (process.browser && process.env.NODE_ENV !== 'production') { if (process.browser) {
window.resizeListeners = listeners window.__resizeListeners = listeners
} }
if (process.browser) { if (process.browser) {

View file

@ -0,0 +1,46 @@
import {
composeInput,
getNthAutosuggestionResult,
getNumSyntheticListeners,
getUrl,
homeNavButton,
scrollToStatus,
scrollToTop,
settingsNavButton, sleep
} from '../utils'
import { loginAsFoobar } from '../roles'
fixture`038-memory-leaks.js`
.page`http://localhost:4002`
async function goToStartPoint (t) {
await t
.click(settingsNavButton)
.expect(getUrl()).contains('/settings')
}
async function interactAndGoToEndPoint (t) {
await t
.click(homeNavButton)
.expect(getUrl()).eql('http://localhost:4002/')
await scrollToStatus(t, 15)
await scrollToTop()
await t
.typeText(composeInput, 'hey @qu')
.expect(getNthAutosuggestionResult(1).find('.sr-only').innerText).contains('@quux')
.click(settingsNavButton)
.expect(getUrl()).contains('/settings')
}
test('Does not leak synthetic listeners', async t => {
await loginAsFoobar(t)
await goToStartPoint(t)
await sleep(1000)
const numSyntheticListeners = await getNumSyntheticListeners()
await t
.expect(numSyntheticListeners).typeOf('number')
await interactAndGoToEndPoint(t)
await sleep(1000)
await t
.expect(getNumSyntheticListeners()).eql(numSyntheticListeners)
})

View file

@ -119,6 +119,13 @@ export const sleep = timeout => new Promise(resolve => setTimeout(resolve, timeo
export const getUrl = exec(() => window.location.href) export const getUrl = exec(() => window.location.href)
export const getNumSyntheticListeners = exec(() => {
return Object.keys(window.__eventBus.$e).map(key => window.__eventBus.listenerCount(key))
.concat(window.__resizeListeners.size)
.concat(Object.keys(window.__delegateCallbacks).length)
.reduce((a, b) => a + b, 0)
})
export const getMediaScrollLeft = exec(() => document.querySelector('.media-scroll').scrollLeft || 0) export const getMediaScrollLeft = exec(() => document.querySelector('.media-scroll').scrollLeft || 0)
export const getActiveElementClassList = exec(() => export const getActiveElementClassList = exec(() =>