Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.5 KB
Raw Blame
import api from 'lib/api'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'

import { initKeaTests } from '~/test/init'

import { relatedGroupsLogic } from './relatedGroupsLogic'

jest.mock('lib/lemon-ui/LemonToast/LemonToast', () => ({
    lemonToast: { info: jest.fn(), error: jest.fn(), warning: jest.fn(), success: jest.fn() },
}))

describe('relatedGroupsLogic', () => {
    beforeEach(() => {
        initKeaTests()
        jest.mocked(lemonToast.error).mockClear()
    })

    afterEach(() => {
        jest.restoreAllMocks()
    })

    it('does not show an error toast when the request fails after unmount', async () => {
        let failRequest: () => void = () => {}
        let requestSignal: AbortSignal | undefined
        jest.spyOn(api, 'get').mockImplementation(
            (_url, options) =>
                new Promise((_resolve, reject) => {
                    requestSignal = options?.signal
                    options?.signal?.addEventListener('abort', () => reject(new DOMException('aborted', 'AbortError')))
                    failRequest = () => reject({ status: 500, detail: 'Query exceeded the memory limit' })
                })
        )

        const logic = relatedGroupsLogic({ groupTypeIndex: null, id: 'person-uuid' })
        logic.mount()
        logic.unmount()
        failRequest()
        await new Promise((resolve) => setTimeout(resolve, 0))

        expect(requestSignal?.aborted).toBe(true)
        expect(lemonToast.error).not.toHaveBeenCalled()
    })
})