Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anything with Browser.Navigation.Key cannot be tested #24

Open
sporto opened this Issue Sep 13, 2018 · 8 comments

Comments

Projects
None yet
7 participants
@sporto
Copy link

sporto commented Sep 13, 2018

When using Browser.application I will put the Key in my model for later use.

When testing I want to create a model, but I don't have a way to get a Key.
So I cannot test functions that take my Model.

Can test provide a way to get a navigation key?

Originally opened this here elm/browser#30

@mgold

This comment has been minimized.

Copy link
Collaborator

mgold commented Sep 14, 2018

If you are testing a function that calls pushUrl : Key -> String -> Cmd msg, you may want to pass it a String -> Cmd msg argument. This can be the real pushUrl model.key in production, and a stub for testing. I'm not sure how you're testing Cmds (elm-testable hasn't been upgraded for 0.19), but this is a fairly straightforward use of dependency injection.

@gampleman

This comment has been minimized.

Copy link
Contributor

gampleman commented Sep 14, 2018

I don't think this is necessarily for effect testing, but for anything that takes a model. So for example, when testing a view, that typically has a signature view : Model -> Html Msg, but one cannot construct a Model value in a test without the key.

@sporto

This comment has been minimized.

Copy link
Author

sporto commented Sep 14, 2018

In our case, we have two scenarios where we need this:

  • Testing views that take a Model (which has the key) like mentioned above.
  • Testing update functions which also take the key. We test that these update fns change the model as expected.
@mgold

This comment has been minimized.

Copy link
Collaborator

mgold commented Sep 15, 2018

Ah. Having dummyKey : Key that produces Cmd.none when used does sound helpful here.

You can also play around with record parameters, though I doubt this is news:

myView : { a | thing : Thing } -> Html Msg will accept a record with the thing field and anything else, so you can pass a Model or something simpler. This is actually a nice thing to do generally, if the fields aren't too numerous, to show that the view has limited dependencies.

type alias Model a = { a | thing : Thing } can be extended with { key : Key } to combine the records. It's a lot of boilerplate.

@timojaask

This comment has been minimized.

Copy link

timojaask commented Oct 31, 2018

I'm having a similar issue. I want to make sure that my init : () -> Url.Url -> Nav.Key -> ( Model, Cmd Msg ) is returning a particular command depending on the Url passed by the Elm runtime. There seems to be currently no way to test this.

EDIT: Just realized I can’t generally compare Platform.Cmd’s, so that whole idea that I’d make sure that my main returns the right things seems to be doomed.

@andys8

This comment has been minimized.

Copy link

andys8 commented Nov 15, 2018

One can compare Cmd.none and other Cmds and they're not equal. I had fuzz tests making sure there is "nothing" happening by comparing with Cmd.none. Not being able to create Key made them unusable.

The other option to the approach #24 (comment) is to have a model with key: Maybe Key. This way the Model can be initialized in the tests, but Maybe has to be dealt with at runtime.

@rtfeldman

This comment has been minimized.

Copy link
Collaborator

rtfeldman commented Dec 4, 2018

Just a quick update: we're working on designs for a possible API to address this!

jenmei added a commit to oaktown/calliope that referenced this issue Mar 8, 2019

Separating `Navigation.Key` from `update` function to make it testabl…
…e. Unfortunately, `Navigation.Key` is passed in from the runtime and there’s no way to create a testing stub for it currently. See elm-explorations/test#24 for updates.

jenmei added a commit to oaktown/calliope that referenced this issue Mar 8, 2019

Separating `Navigation.Key` from `update` function to make it testabl…
…e. Unfortunately, `Navigation.Key` is passed in from the runtime and there’s no way to create a testing stub for it currently. See elm-explorations/test#24 for updates.

jenmei added a commit to oaktown/calliope that referenced this issue Mar 8, 2019

Separating `Navigation.Key` from `update` function to make it testabl…
…e. Unfortunately, `Navigation.Key` is passed in from the runtime and there’s no way to create a testing stub for it currently. See elm-explorations/test#24 for updates.

jenmei added a commit to oaktown/calliope that referenced this issue Mar 8, 2019

Separating `Navigation.Key` from `update` function to make it testabl…
…e. Unfortunately, `Navigation.Key` is passed in from the runtime and there’s no way to create a testing stub for it currently. See elm-explorations/test#24 for updates.
@bryce13950

This comment has been minimized.

Copy link

bryce13950 commented Mar 27, 2019

Is there any news on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.