@@ -823,11 +823,12 @@ describe('actions/Policy', () => {
823823 it ( "should remove all non-owned workspace chats and keep the user's own workspace chat when leaving a workspace" , async ( ) => {
824824 await Onyx . set ( ONYXKEYS . SESSION , { email : ESH_EMAIL , accountID : ESH_ACCOUNT_ID } ) ;
825825 const policyID = Policy . generatePolicyID ( ) ;
826- await Onyx . set ( ` ${ ONYXKEYS . COLLECTION . POLICY } ${ policyID } ` , {
826+ const policy : PolicyType = {
827827 ...createRandomPolicy ( 0 , CONST . POLICY . TYPE . TEAM ) ,
828828 id : policyID ,
829829 name : WORKSPACE_NAME ,
830- } ) ;
830+ } ;
831+ await Onyx . set ( `${ ONYXKEYS . COLLECTION . POLICY } ${ policyID } ` , policy ) ;
831832 await waitForBatchedUpdates ( ) ;
832833
833834 const ownWorkspaceChat : Report = {
@@ -856,7 +857,7 @@ describe('actions/Policy', () => {
856857 const getAllWorkspaceReportsSpy = jest . spyOn ( ReportUtils , 'getAllWorkspaceReports' ) . mockReturnValue ( [ ownWorkspaceChat , ...nonOwnedWorkspaceChats ] ) ;
857858 const apiWriteSpy = jest . spyOn ( require ( '@libs/API' ) , 'write' ) . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
858859
859- Policy . leaveWorkspace ( ESH_ACCOUNT_ID , policyID ) ;
860+ Policy . leaveWorkspace ( ESH_ACCOUNT_ID , policy ) ;
860861 await waitForBatchedUpdates ( ) ;
861862
862863 expect ( apiWriteSpy ) . toHaveBeenCalledWith (
@@ -1929,6 +1930,43 @@ describe('actions/Policy', () => {
19291930 } ) ;
19301931 } ) ;
19311932
1933+ describe ( 'leaveWorkspace' , ( ) => {
1934+ it ( 'should leave workspace and archive reports' , async ( ) => {
1935+ // Given a policy and a report in Onyx
1936+ const policy = createRandomPolicy ( 1 ) ;
1937+ const report = {
1938+ ...createRandomReport ( 1 , undefined ) ,
1939+ policyID : policy . id ,
1940+ } ;
1941+
1942+ // Set initial state in Onyx
1943+ await Onyx . merge ( `${ ONYXKEYS . COLLECTION . POLICY } ${ policy . id } ` , policy ) ;
1944+ await Onyx . merge ( `${ ONYXKEYS . COLLECTION . REPORT } ${ report . reportID } ` , report ) ;
1945+
1946+ // When leaveWorkspace is called
1947+ Policy . leaveWorkspace ( 1 , policy ) ;
1948+
1949+ await waitForBatchedUpdates ( ) ;
1950+
1951+ // Then the policy should be removed
1952+ const policyAfter = await getOnyxValue ( `${ ONYXKEYS . COLLECTION . POLICY } ${ policy . id } ` ) ;
1953+ expect ( policyAfter ) . toBeFalsy ( ) ;
1954+
1955+ // And the report should be closed and archived
1956+ const reportAfter = await getOnyxValue ( `${ ONYXKEYS . COLLECTION . REPORT } ${ report . reportID } ` ) ;
1957+ expect ( reportAfter ) . toMatchObject ( {
1958+ statusNum : CONST . REPORT . STATUS_NUM . CLOSED ,
1959+ stateNum : CONST . REPORT . STATE_NUM . APPROVED ,
1960+ } ) ;
1961+
1962+ const reportNameValuePairsAfter = await getOnyxValue ( `${ ONYXKEYS . COLLECTION . REPORT_NAME_VALUE_PAIRS } ${ report . reportID } ` ) ;
1963+ expect ( reportNameValuePairsAfter ?. private_isArchived ) . toBeTruthy ( ) ;
1964+
1965+ const reportMetadataAfter = await getOnyxValue ( `${ ONYXKEYS . COLLECTION . REPORT_METADATA } ${ report . reportID } ` ) ;
1966+ expect ( reportMetadataAfter ?. pendingChatMembers ) . toBeFalsy ( ) ;
1967+ } ) ;
1968+ } ) ;
1969+
19321970 describe ( 'generateDefaultWorkspaceName' , ( ) => {
19331971 beforeAll ( ( ) => {
19341972 Onyx . set ( ONYXKEYS . COLLECTION . POLICY , { } ) ;
0 commit comments