@@ -5,30 +5,34 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
55import { createRoot , type Root } from 'react-dom/client'
66import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
77
8- const mocks = vi . hoisted ( ( ) => ( { request : vi . fn ( ) } ) )
8+ const mocks = vi . hoisted ( ( ) => ( { request : vi . fn ( ) , refresh : vi . fn ( ) } ) )
99vi . mock ( '@/lib/api/client/request' , ( ) => ( { requestJson : mocks . request } ) )
10+ vi . mock ( 'next/navigation' , ( ) => ( { useRouter : ( ) => ( { refresh : mocks . refresh } ) } ) )
1011
1112import { ApiClientError } from '@/lib/api/client/errors'
1213import {
1314 disconnectPersonalOrganizationAccountContract ,
1415 listOrganizationAccountPeopleContract ,
1516 updateOrganizationAccountsContract ,
1617} from '@/lib/api/contracts/organization-accounts'
18+ import { resourceScopeKey } from '@/lib/core/resource-scope'
1719import {
1820 organizationAccountsKeys ,
1921 useDisconnectPersonalOrganizationAccount ,
2022 useOrganizationAccountPeople ,
2123 useUpdateOrganizationAccounts ,
2224} from '@/hooks/queries/organization-accounts'
2325import { slackSearchKeys } from '@/hooks/queries/slack-search'
26+ import { knowledgeKeys } from '@/hooks/queries/utils/knowledge-keys'
2427import { searchSourceKeys } from '@/hooks/queries/utils/search-source-keys'
2528
2629describe ( 'personal account disconnect' , ( ) => {
2730 it . each ( [ true , false ] ) (
28- 'refreshes this organization only after success=%s, including after unmount' ,
31+ 'clears content and refreshes the router only after success=%s, including after unmount' ,
2932 async ( success ) => {
3033 vi . stubGlobal ( 'IS_REACT_ACT_ENVIRONMENT' , true )
3134 mocks . request . mockReset ( )
35+ mocks . refresh . mockReset ( )
3236 const response = Promise . withResolvers < { success : true } > ( )
3337 mocks . request . mockReturnValue ( response . promise )
3438 const client = new QueryClient ( )
@@ -48,7 +52,39 @@ describe('personal account disconnect', () => {
4852 )
4953 const people = organizationAccountsKeys . people ( 'org-1' )
5054 const other = searchSourceKeys . list ( { kind : 'organization' , organizationId : 'org-2' } )
51- for ( const key of [ own , catalog , people , other ] ) client . setQueryData ( key , { existing : true } )
55+ const results = knowledgeKeys . search (
56+ resourceScopeKey ( { kind : 'organization' , organizationId : 'org-1' } ) ,
57+ 'private content'
58+ )
59+ const otherResults = knowledgeKeys . search (
60+ resourceScopeKey ( { kind : 'organization' , organizationId : 'org-2' } ) ,
61+ 'private content'
62+ )
63+ const workspaceResults = knowledgeKeys . search ( 'workspace-1' , 'private content' )
64+ const ownDocument = knowledgeKeys . document ( 'kb-1' , 'document-1' )
65+ const chunks = knowledgeKeys . chunks ( 'kb-2' , 'document-2' , '' )
66+ const otherDocument = knowledgeKeys . document ( 'kb-other' , 'document-other' )
67+ const resultOnlyDocument = knowledgeKeys . document ( 'kb-result-only' , 'document-result-only' )
68+ const sourcePages = {
69+ pages : [
70+ { sources : [ { knowledgeBaseId : 'kb-1' } ] , nextCursor : 'next' } ,
71+ { sources : [ { knowledgeBaseId : 'kb-2' } ] , nextCursor : null } ,
72+ ] ,
73+ pageParams : [ undefined , 'next' ] ,
74+ }
75+ for ( const key of [ own , catalog ] ) client . setQueryData ( key , sourcePages )
76+ client . setQueryData ( results , [ { knowledgeBaseId : 'kb-result-only' } ] )
77+ for ( const key of [
78+ people ,
79+ other ,
80+ otherResults ,
81+ workspaceResults ,
82+ ownDocument ,
83+ resultOnlyDocument ,
84+ chunks ,
85+ otherDocument ,
86+ ] )
87+ client . setQueryData ( key , { content : 'previously authorized content' } )
5288 try {
5389 await act ( async ( ) =>
5490 root . render (
@@ -78,9 +114,23 @@ describe('personal account disconnect', () => {
78114 disconnectPersonalOrganizationAccountContract ,
79115 { params : { credentialId : 'own-credential' } }
80116 )
81- for ( const key of [ own , catalog , people ] )
82- expect ( client . getQueryState ( key ) ?. isInvalidated ) . toBe ( success )
117+ for ( const key of [
118+ own ,
119+ catalog ,
120+ results ,
121+ ownDocument ,
122+ resultOnlyDocument ,
123+ chunks ,
124+ otherDocument ,
125+ ] ) {
126+ if ( success ) expect ( client . getQueryData ( key ) ) . toBeUndefined ( )
127+ else expect ( client . getQueryData ( key ) ) . toBeDefined ( )
128+ }
129+ expect ( client . getQueryState ( people ) ?. isInvalidated ) . toBe ( success )
83130 expect ( client . getQueryState ( other ) ?. isInvalidated ) . toBe ( false )
131+ expect ( mocks . refresh ) . toHaveBeenCalledTimes ( success ? 1 : 0 )
132+ for ( const key of [ otherResults , workspaceResults ] )
133+ expect ( client . getQueryData ( key ) ) . toEqual ( { content : 'previously authorized content' } )
84134 } finally {
85135 await act ( async ( ) => root . unmount ( ) )
86136 client . clear ( )
0 commit comments