-
Notifications
You must be signed in to change notification settings - Fork 78
Port Relay test tooling from Reaction, update to Node 10 #1209
Conversation
New dependencies added: relay-mock-network-layerAuthor: Rob Richard Description: Relay modern network layer that returns schema correct mock data Homepage: https://github.com/1stdibs/relay-mock-network-layer#readme
|
Generated by 🚫 dangerJS |
package.json
Outdated
@@ -30,7 +30,7 @@ | |||
"test": "jest", | |||
"test:ci": "jest --outputFile test-results.json --json --runInBand", | |||
"testing": "jest --watch", | |||
"relay": "relay-compiler --src ./src --schema data/schema.graphql --language typescript --artifactDirectory ./src/__generated__ --persist --persistOutput ./src/__generated__/complete.queryMap.json", | |||
"relay": "relay-compiler --src ./src --schema data/schema.graphql --language typescript --artifactDirectory ./src/__generated__ --exclude '**/node_modules/**,**/__mocks__/**,**/__generated__/**'", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
relay-mock-network-layer
, used in createMockNetworkLayer
, depends on query text being present in the operation in order to client side resolve. Our current approach of used persisted queries however sets text
to null
in generated .graphql
artifacts so that queries are not sent over the network. We obviously don't want to disable persisted queries just to get these tests to work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some ideas @orta had to workaround this:
- Update compiler to include query text as some other field in the generated artifact, presumably wouldn't be sent over the wire at runtime as the runtime wouldn't know about the generated field.
- Update
relay-mock-network-layer
to resolve operation text differently
src/lib/tests/renderRelayTree.tsx
Outdated
* tree. | ||
*/ | ||
export const RelayFinishedLoading: RenderUntilPredicate<any, any, any> = tree => | ||
!tree.find(`[testID=${LoadingTestID}]`).length |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation of renderRelayTree
in reaction uses a class name as a loading sentinel. RN does not have classes, and (I think?) we need something that is flushed to a view primitive. I opted for View's testID prop. It comes with a caveat:
This disables the 'layout-only view removal' optimization for this view!
Though I think this is safe to ignore as we only pass it during tests. This gets passed wherever renderWithLoadProgress
is used, though I still believe the affected surface area is small - this would only affect Spinner
.
"id": "ff999f5e1d50496e9b0dcfc8e6561436", | ||
"text": null, | ||
"id": null, | ||
"text": "query ActiveBidsQuery {\n me {\n ...ActiveBids_me\n __id\n }\n}\n\nfragment ActiveBids_me on Me {\n lot_standings(live: true) {\n most_recent_bid {\n __id\n }\n ...ActiveBid_bid\n }\n __id\n}\n\nfragment ActiveBid_bid on LotStanding {\n is_leading_bidder\n sale {\n href\n is_live_open\n __id\n }\n most_recent_bid {\n __id\n max_bid {\n display\n }\n sale_artwork {\n artwork {\n href\n image {\n url\n }\n artist_names\n __id\n }\n counts {\n bidder_positions\n }\n highest_bid {\n display\n __id: id\n }\n lot_number\n reserve_status\n __id\n }\n }\n}\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This ^ is the query text needed in the mock layer ^
src/setupJest.ts
Outdated
} | ||
|
||
global.window = new JSDOM("<!doctype html><html><body></body></html>").window | ||
global.document = window.document |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was copied over from the approach seen here: enzymejs/enzyme#1436 (comment)
This is preferable (imo) to the alternate approach seen further up that thread (enzymejs/enzyme#1436 (comment)) and posted in slack of react-native-mock-render
as it requires no component mocking. Still, this approach is non-ideal as RN is not the DOM, but this is enough to get Enzyme to work for the time being. Ideally Enzyme would support RN first-class with an RN adapter, which enzymejs/enzyme#1436 tracks discussion of.
58b7ac1
to
d4d2df8
Compare
"graphql": "^0.13", | ||
"relay-runtime": "https://github.com/alloy/relay/releases/download/v1.5.0-artsy.5/relay-runtime-1.5.0-artsy.5.tgz", | ||
"@types/relay-runtime": "^1.3.5" | ||
"@types/relay-runtime": "^1.3.5", | ||
"cheerio": "0.22.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jest-environment-jsdom
depends on cheerio@^1.0.0-rc.2
. I ran into a serialization error with this version, where <Image />
elements rendered to string would be transformed into <img>
instead of the expected <image />
:
renderRelayTree › resolves a promise once the full tree (including nested query renderers) has been rendered
expect(received).toEqual(expected)
Expected value to equal:
"<view><img source=\"[object Object]\"><text ellipsizemode=\"tail\">Mona Lisa</text><text ellipsizemode=\"tail\">Leonardo da Vinci</text></view>"
Received:
"<view><image source=\"[object Object]\"></image><text ellipsizemode=\"tail\">Mona Lisa</text><text ellipsizemode=\"tail\">Leonardo da Vinci</text></view>"
My hunch is that enzyme must be passing slightly different configuration options down to cheerio when calling render
vs mount
, but not sure. Resolving to the latest non-rc cheerio version fixes this, but does appear to be a hack.
@@ -61,10 +61,10 @@ | |||
"lib" | |||
], | |||
"resolutions": { | |||
"parse5": "5.1.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This resolution does not appear to be necessary anymore. Original discussion here: https://github.com/artsy/emission/pull/1185/files#r217160876
@@ -116,12 +116,13 @@ | |||
"enzyme": "^3.1.0", | |||
"enzyme-adapter-react-16": "^1.0.2", | |||
"husky": "^0.14.3", | |||
"jest": "^22.4.2", | |||
"jest": "^23.6.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to upgrade jest as part of upgrading to node 10 due to needing this PR: jestjs/jest#6505
- // See https://github.com/facebook/relay/issues/1816 | ||
+ const query = | ||
+ operation.text || | ||
+ (resolveQueryFromOperation && resolveQueryFromOperation(operation)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lots of diff in this file due to running Prettier against it, but this is the only meaningful change. @orta and I paired on adding this in in response to the issue described in the frontend practice meeting. To put it into text: client side query resolving requires access to the relay query operation text which is not immediately available due to Emission's use of persisted queries, where only the ID is in the compiled artifact. This change allows us to pass in a query resolver function, where we then use the same approach used in fetchQuery.ts
to resolve the ID in the query map.
Our usage of this can be seen here: https://github.com/javamonn/emission/blob/084f721f766476e497ecd078add48c5de5050db8/src/lib/tests/createMockNetworkLayer/index.ts#L21-L23
I will PR these changes against the upstream repository once we land them here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heads up that I have a PR over in relay-mock-network-layer
that will allow us to pass additional options over: 1stdibs/relay-mock-network-layer#7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mainly that it increases the difference between dev and prod
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok.
@@ -9,13 +9,7 @@ exports[`renders properly 1`] = ` | |||
} | |||
> | |||
<View | |||
accessibilityComponentType={undefined} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to regenerate all snapshots, I'm assuming due to having upgraded Jest.
There's a fair amount of
|
// Typescript thinks we're in React Native, this file should be test-only i.e. executed within Node. | ||
declare const __dirname: string | ||
// @ts-ignore | ||
import * as fs from "fs" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a better way to make TS happy here? ts-jest
takes a tsConfig
option that looks promising, but I'm not knowledgeable enough about how the current setup works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, but that's worth doing in a separate PR IMO
src/lib/tests/Context.tsx
Outdated
return <Context.Provider value={providerValues}>{children}</Context.Provider> | ||
} | ||
|
||
export const ContextConsumer = Context.Consumer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this context provider be moved out of the tests
folder?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, nice call. Stuck it in there as only MockRelayRenderer
depends on it, but no reason we can't start using it more widely.
Very nice PR @javamonn 💯 |
bbe161a
to
31aab9a
Compare
Yep, cool - will rebase for you |
renderUntil
,renderRelayTree
,MockRelayRenderer
. As part of this work, made Enzyme static rendering work viajsdom
. This is a stop gap (RN is not the DOM), and we'll want to track Create Adapter for React Native & React 16 enzymejs/enzyme#1436 for first class RN static rendering support in enzyme.#skip_new_tests
I ported the existing tests in Reaction over, but not all new files had/need test coverage.