facebook / relay

Relay is a JavaScript framework for building data-driven React applications.
https://relay.dev
MIT License
18.36k stars 1.82k forks source link

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

Open maheshk172 opened 4 years ago

maheshk172 commented 4 years ago

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.

3) 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.

alunyov commented 4 years ago

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).

albertreed commented 4 years ago

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.

sibelius commented 4 years ago

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

is this enough?

albertreed commented 4 years ago

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

sibelius commented 4 years ago

the idea/concept is the same

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

albertreed commented 4 years ago

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.

sibelius commented 4 years ago

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

stale[bot] commented 3 years ago

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.