relay icon indicating copy to clipboard operation
relay copied to clipboard

Unable to find unit/component test samples for relay-react components

Open maheshk172 opened this issue 6 years ago • 8 comments

We are trying to write the Unit and Component tests for our react relay components which has fragments and query renderers. I am referring testing-relay-components link provided below. https://relay.dev/docs/en/testing-relay-components

The documentation seems incomplete. There are many small items which this page does not explain well and to make things worse, I am not able to find single example on internet where this pattern has been implemented well.

So could you please share any working samples of this guide. https://relay.dev/docs/en/testing-relay-components

Questions:

  1. @relay_test_operation - Where is this located ? I included this in my query, by relay method can not identify this as valid thing

  2. Fragment container Tests - https://relay.dev/docs/en/testing-relay-components#fragment-container-tests

Please share the sample component used here, its not clear how to write the test query renderer which matches your component. I tried to write a query, but somehow my props are always empty. Its not populating any data.

  1. MockPayloadGenerator.generate -> I cant see the generated data landing under props. I tried adding custom generators too, but still my 'props' always show empty data. No Mock data populated.

I am using TypeScript,babel-plugin-macros for graphql.

maheshk172 avatar Dec 10 '19 10:12 maheshk172

Thanks for the feedback! I agree that we don't have any actual working examples in OSS right now.

I've created this PR to relay examples to illustrate how those testing tools may be used.

https://github.com/relayjs/relay-examples/pull/127

This example has a test query renderer (with @relay_test_operation).

alunyov avatar Dec 10 '19 19:12 alunyov

Stumbled across this issue via google - I'm having the same issue with props coming through as empty. Curious if you ever figured out what you were doing wrong, @maheshk172?

The example is super-helpful for writing tests that require mutations. I would love to see an example of the best way to test a simple Fragment Container (perhaps without pagination to limit the number of concepts covered?) using relay-test-utils as well.

If someone makes the example, I'd be happy to take those examples and use them to beef up the documentation of this area.

albertreed avatar May 28 '20 20:05 albertreed

I have 3 exercises (with solutions) in my Relay Workshop

  • testing usePreloadedQuery: https://github.com/sibelius/relay-workshop/tree/master/workshop/10-testUsePreloadQuery
  • testing useFragment: https://github.com/sibelius/relay-workshop/tree/master/workshop/11-testUseFragment
  • testing useMutation: https://github.com/sibelius/relay-workshop/tree/master/workshop/12-testUseMutation

is this enough?

sibelius avatar May 28 '20 20:05 sibelius

Looks like a great place to start (and I appreciate the tutorials, thanks!). Got a non-hooks example?

albertreed avatar May 29 '20 12:05 albertreed

the idea/concept is the same

check here https://twitter.com/sseraphini/status/1242841595503935488

sibelius avatar May 29 '20 12:05 sibelius

The call to queuePendingOperation in your example seems odd as I think that function is not intended to be part of the public interface and also the call shouldn't be needed. However, it appears that calls to preloadQuery don't properly find it's way into the operation queue without it. That's not related to this issue, I'll file another one.

albertreed avatar May 30 '20 17:05 albertreed

This part is not documented yet as hooks are still experimental

But this happens because you need to mock the query before calling preloadQuery on tests

sibelius avatar May 30 '20 17:05 sibelius

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Dec 25 '20 14:12 stale[bot]