1- module Client.Tests
1+ module Client.Tests
22
33open Fable.Mocha
44open SAFE
@@ -124,6 +124,112 @@ let remoteData =
124124 | RemoteDataCase.LoadingPopulated -> Loading ( Some true )
125125 | RemoteDataCase.Loaded -> Loading ( Some true ))
126126 ]
127+ let optimistic =
128+ testList " Optimistic" [
129+ testList " create" [
130+ testCase " creates new value with no history" <| fun _ ->
131+ let opt = Optimistic.create 42
132+ match opt with
133+ | Exists ( value, prev) ->
134+ Expect.equal value 42 " Current value should be set"
135+ Expect.equal prev None " Previous value should be None"
136+ | NonExistant ->
137+ failtest " Should not be NonExistant"
138+ ]
139+
140+ testList " empty" [
141+ testCase " creates empty optimistic value" <| fun _ ->
142+ let opt = Optimistic.empty
143+ Expect.equal opt NonExistant " Should be NonExistant"
144+ ]
145+
146+ testList " Value property" [
147+ testCase " returns Some for existing value" <| fun _ ->
148+ let opt = Optimistic.create 42
149+ Expect.equal opt.Value ( Some 42 ) " Should return Some with current value"
150+
151+ testCase " returns None for NonExistant" <| fun _ ->
152+ let opt = Optimistic.empty
153+ Expect.equal opt.Value None " Should return None for NonExistant"
154+ ]
155+
156+ testList " update" [
157+ testCase " updates value and shifts previous" <| fun _ ->
158+ let opt = Optimistic.create 42
159+ let updated = opt.Update 84
160+ match updated with
161+ | Exists ( value, prev) ->
162+ Expect.equal value 84 " Current value should be updated"
163+ Expect.equal prev ( Some 42 ) " Previous value should be old current"
164+ | NonExistant ->
165+ failtest " Should not be NonExistant"
166+
167+ testCase " update on NonExistant remains NonExistant" <| fun _ ->
168+ let opt = Optimistic.empty
169+ let updated = opt.Update 42
170+ Expect.equal updated NonExistant " Should remain NonExistant"
171+ ]
172+
173+ testList " rollback" [
174+ testCase " rolls back to previous value" <| fun _ ->
175+ let opt = Optimistic.create 42 |> fun o -> o.Update 84
176+ let rolled = opt.Rollback()
177+ match rolled with
178+ | Exists ( value, prev) ->
179+ Expect.equal value 42 " Current value should be previous"
180+ Expect.equal prev None " Previous value should be None"
181+ | NonExistant ->
182+ failtest " Should not be NonExistant"
183+
184+ testCase " rollback on NonExistant remains NonExistant" <| fun _ ->
185+ let opt = Optimistic.empty
186+ let rolled = opt.Rollback()
187+ Expect.equal rolled NonExistant " Should remain NonExistant"
188+ ]
189+
190+ testList " map" [
191+ testCase " maps both current and previous values" <| fun _ ->
192+ let opt = Optimistic.create 42 |> fun o -> o.Update 84
193+ let mapped = opt.Map string
194+ match mapped with
195+ | Exists ( value, prev) ->
196+ Expect.equal value " 84" " Current value should be mapped"
197+ Expect.equal prev ( Some " 42" ) " Previous value should be mapped"
198+ | NonExistant ->
199+ failtest " Should not be NonExistant"
200+
201+ testCase " map on NonExistant remains NonExistant" <| fun _ ->
202+ let opt = Optimistic.empty
203+ let mapped = opt.Map string
204+ Expect.equal mapped NonExistant " Should remain NonExistant"
205+ ]
206+
207+ testList " module functions" [
208+ testCase " update function matches member" <| fun _ ->
209+ let opt = Optimistic.create 42
210+ let memberUpdate = opt.Update 84
211+ let moduleUpdate = Optimistic.update 84 opt
212+ Expect.equal moduleUpdate memberUpdate " Module update should match member update"
213+
214+ testCase " rollback function matches member" <| fun _ ->
215+ let opt = Optimistic.create 42 |> fun o -> o.Update 84
216+ let memberRollback = opt.Rollback()
217+ let moduleRollback = Optimistic.rollback opt
218+ Expect.equal moduleRollback memberRollback " Module rollback should match member rollback"
219+
220+ testCase " map function matches member" <| fun _ ->
221+ let opt = Optimistic.create 42
222+ let memberMap = opt.Map string
223+ let moduleMap = Optimistic.map string opt
224+ Expect.equal moduleMap memberMap " Module map should match member map"
225+ ]
226+ ]
227+
228+ let allTests =
229+ testList " All Tests" [
230+ remoteData
231+ optimistic
232+ ]
127233
128234[<EntryPoint>]
129- let main _ = Mocha.runTests remoteData
235+ let main _ = Mocha.runTests allTests
0 commit comments