To answer my own question -- it looks like Native.Json has an identity
function which does just what I want, but which I can't import. However, it
also looks like many of Json.Encode's functions just call
Native.Json.identity. So this works:
data = Encode.string complicatedNestedJson
On Friday, July 29, 2016 at 2:52:32 PM UTC-4, Paul Blair wrote:
>
> I'm using the nested Elm architecture pattern in an application. The
> nested components respond to messages coming in on a port. The messages are
> handled by the top-level component before being handed on to the nested
> components. So the outer data structure looks something like this:
>
> type alias FromServer =
> { message_type: MessageType
> , data: Json.Decode.Value
> }
>
> The data field is a Json.Decode.Value because the top-level component
> doesn't know about the message types defined by the nested components; it
> does a lookup on the incoming message type and decides to which inner
> component it will forward the message. So the top level looks like this:
>
> subscriptions : Model.Model -> Sub Message
> subscriptions model =
> theIncomingPort toMessage
>
>
> toMessage : FromServer -> Message
> toMessage incoming =
> case Dict.get incoming.message_type componentDictionary of
> Just MyInnerComponent ->
> InnerComponentModule.toMessage incoming |>
> MyInnerComponentMessageWrapper
> Nothing ->
> ...
>
> The inner component's toMessage function also uses the message_type field
> to choose the decoder to apply to the Json.Decode.Value in the data field.
>
> I want to test the inner component's toMessage function. This means that I
> need to create a FromServer record with the data contained in a
> Json.Decode.Value -- ideally created from a Json string.
>
> However, from what I can tell the only way to create this kind of raw
> Json.Decode.Value is by passing the string in through a port. This seems
> like a pretty complicated way to set up a test. Is there some other way to
> accomplish this?
>
>
--
You received this message because you are subscribed to the Google Groups "Elm
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.