Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { expectLogic } from 'kea-test-utils'

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

import { AppContext } from '../../../../frontend/src/types'
import * as api from '../generated/api'
import { legalDocumentsLogic } from './legalDocumentsLogic'

jest.mock('../generated/api', () => ({
legalDocumentsList: jest.fn().mockResolvedValue({ results: [] }),
}))

describe('legalDocumentsLogic', () => {
let logic: ReturnType<typeof legalDocumentsLogic.build>

afterEach(() => {
logic?.unmount()
})

it('does not call the API with the "@current" sentinel while the organization is still loading', async () => {
// No organization in context yet, and organizationLogic won't resolve it
// synchronously — currentOrganizationId falls back to '@current' during
// this window, which the backend 404s on.
window.POSTHOG_APP_CONTEXT = { current_user: null } as unknown as AppContext
initKeaTests(false)
logic = legalDocumentsLogic()
logic.mount()

await expectLogic(logic).toDispatchActions(['loadLegalDocumentsSuccess'])

expect(api.legalDocumentsList).not.toHaveBeenCalled()
expect(logic.values.legalDocuments).toEqual([])
})
})
13 changes: 9 additions & 4 deletions products/legal_documents/frontend/scenes/legalDocumentsLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,22 @@ export const legalDocumentsLogic = kea<legalDocumentsLogicType>([
[] as LegalDocument[],
{
loadLegalDocuments: async () => {
if (!values.currentOrganizationId) {
if (!values.currentOrganization?.id) {
// Organization hasn't loaded yet — currentOrganizationId would
// fall back to the '@current' sentinel, which the backend can't
// resolve. No-op until the real organization is available.
return []
}
if (values.isAdminOrOwner === false && values.currentOrganization) {
if (values.isAdminOrOwner === false) {
return []
}
try {
const response = await api.legalDocumentsList(values.currentOrganizationId)
return (response.results ?? []) as LegalDocument[]
} catch (error) {
if (error instanceof ApiError && error.status === 403) {
// 403 (non-admin) and 404 (organization not resolvable, e.g. a
// stale/transient race) both degrade to an empty list.
if (error instanceof ApiError && (error.status === 403 || error.status === 404)) {
return []
}
throw error
Expand Down Expand Up @@ -287,7 +292,7 @@ export const legalDocumentsLogic = kea<legalDocumentsLogicType>([
actions.setLegalDocumentValue('dpa_mode', dpaMode)
},
deleteLegalDocument: async ({ id, documentType }) => {
if (!values.currentOrganizationId) {
if (!values.currentOrganization?.id) {
return
}
actions.setDeletingId(id)
Expand Down
3 changes: 3 additions & 0 deletions products/legal_documents/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"kea-loaders": "catalog:",
"kea-router": "catalog:"
},
"devDependencies": {
"kea-test-utils": "catalog:"
},
"peerDependencies": {
"@posthog/icons": "catalog:",
"@posthog/lemon-ui": "*",
Expand Down
Loading