From c90c312b56b4a9bc9a462f84b711435fbd035722 Mon Sep 17 00:00:00 2001
From: Dariusz Rybicki <dariusz@elixxir.io>
Date: Mon, 29 Aug 2022 11:30:39 +0100
Subject: [PATCH] Improve MessengerDestroy functor

---
 .../Messenger/Functors/MessengerDestroy.swift       |  2 +-
 .../Messenger/Functors/MessengerDestroyTests.swift  | 13 +++++++++++--
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/Sources/XXMessengerClient/Messenger/Functors/MessengerDestroy.swift b/Sources/XXMessengerClient/Messenger/Functors/MessengerDestroy.swift
index e584cf1a..51f60923 100644
--- a/Sources/XXMessengerClient/Messenger/Functors/MessengerDestroy.swift
+++ b/Sources/XXMessengerClient/Messenger/Functors/MessengerDestroy.swift
@@ -11,10 +11,10 @@ public struct MessengerDestroy {
 extension MessengerDestroy {
   public static func live(_ env: MessengerEnvironment) -> MessengerDestroy {
     MessengerDestroy {
-      try env.fileManager.removeDirectory(env.storageDir)
       env.ud.set(nil)
       env.e2e.set(nil)
       env.cMix.set(nil)
+      try env.fileManager.removeDirectory(env.storageDir)
     }
   }
 }
diff --git a/Tests/XXMessengerClientTests/Messenger/Functors/MessengerDestroyTests.swift b/Tests/XXMessengerClientTests/Messenger/Functors/MessengerDestroyTests.swift
index 810214ca..cfe3f9a5 100644
--- a/Tests/XXMessengerClientTests/Messenger/Functors/MessengerDestroyTests.swift
+++ b/Tests/XXMessengerClientTests/Messenger/Functors/MessengerDestroyTests.swift
@@ -13,30 +13,39 @@ final class MessengerDestroyTests: XCTestCase {
 
     var env: MessengerEnvironment = .unimplemented
     env.storageDir = storageDir
-    env.fileManager.removeDirectory = { didRemoveDirectory.append($0) }
     env.ud.set = { didSetUD.append($0) }
     env.e2e.set = { didSetE2E.append($0) }
     env.cMix.set = { didSetCMix.append($0) }
+    env.fileManager.removeDirectory = { didRemoveDirectory.append($0) }
     let destroy: MessengerDestroy = .live(env)
 
     try destroy()
 
-    XCTAssertNoDifference(didRemoveDirectory, [storageDir])
     XCTAssertNoDifference(didSetUD.map { $0 == nil }, [true])
     XCTAssertNoDifference(didSetE2E.map { $0 == nil }, [true])
     XCTAssertNoDifference(didSetCMix.map { $0 == nil }, [true])
+    XCTAssertNoDifference(didRemoveDirectory, [storageDir])
   }
 
   func testRemoveDirectoryFailure() {
     struct Error: Swift.Error, Equatable {}
     let error = Error()
+    var didSetUD: [UserDiscovery?] = []
+    var didSetE2E: [E2E?] = []
+    var didSetCMix: [CMix?] = []
 
     var env: MessengerEnvironment = .unimplemented
+    env.ud.set = { didSetUD.append($0) }
+    env.e2e.set = { didSetE2E.append($0) }
+    env.cMix.set = { didSetCMix.append($0) }
     env.fileManager.removeDirectory = { _ in throw error }
     let destroy: MessengerDestroy = .live(env)
 
     XCTAssertThrowsError(try destroy()) { err in
       XCTAssertEqual(err as? Error, error)
     }
+    XCTAssertNoDifference(didSetUD.map { $0 == nil }, [true])
+    XCTAssertNoDifference(didSetE2E.map { $0 == nil }, [true])
+    XCTAssertNoDifference(didSetCMix.map { $0 == nil }, [true])
   }
 }
-- 
GitLab