Open nikoskalogridis opened 4 years ago
I found a different way, sending explicitly a null
value on logout:
storeCredWith : Cred -> Cmd msg
storeCredWith cred =
storeCache (encode cred)
simulateStoreCredWith : Cred -> ProgramTest.SimulatedEffect msg
simulateStoreCredWith cred =
SimulatedEffect.Ports.send "storeCache" (encode cred)
logout : Cmd msg
logout =
storeCache Encode.null
port storeCache : Value -> Cmd msg
Its more explicit and I prefer it but I think it could be beneficial for users to have a note regarding this on the documentation of Port.send
api.
For your original port
port storeCache : Maybe Value -> Cmd msg
since the port type is Maybe Value
, you'll need to encode the Maybe Value
as a JSON Value
when you use SimulatedEffect.Ports.send
. When Json.Decode
provides a maybe
decoder, interestingly Json.Encode
does not provide a maybe
encoder, though I see that Json.Encode.Extra
does have a maybe
encoder.
So I think something like this should work:
import Json.Encode.Extra
simulateStoreCredWith : Maybe Cred -> ProgramTest.SimulatedEffect msg
simulateStoreCredWith maybeCred =
SimulatedEffect.Ports.send "storeCache" (Json.Encode.Extra.maybe encode maybeCred)
I am sending a
Maybe Value
on a port in order to clear the localstorage key when it receives a null value (through sending a Nothing on logout). Trying to do the same in the following code:fails with:
is there a way to simulate this (ie Port.send a Maybe Value) ?