Skip to content
This repository was archived by the owner on Jul 30, 2020. It is now read-only.

fix(typescript): fix query typings #32

Merged
merged 2 commits into from
Jun 20, 2019

Conversation

manakuro
Copy link
Contributor

What:
Fix Query typings.

Why:
Typings error occurred because of incompatible with Query interface as noted in #30.

How:

  • Use NativeTestInstance type for returning Query interface.
  • Remove NativeTestInstance type from testRenderer argument in Query interface.
  • Fix ReactTestInstance.getProps type.

Checklist:

  • Documentation added to the
    docs site
  • Typescript definitions updated
  • Tests
  • Ready to be merged

Results:

$ yarn tsc                                                                                                                                                                          
yarn run v1.15.2
$ /Users/me/workspace/react-native/react-native-sample/node_modules/.bin/tsc
✨  Done in 30.35s.

* fix(typescript): fix query typings

* fix(typescript): remove NativeTestInstance from function argument in Query interface

* chore: remove unnecessary code
@codecov-io
Copy link

codecov-io commented Jun 13, 2019

Codecov Report

Merging #32 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #32   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          20     20           
  Lines         262    262           
  Branches       65     65           
=====================================
  Hits          262    262

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 204ff08...ec81517. Read the comment docs.

@manakuro
Copy link
Contributor Author

@bcarroll22 I'm sorry for the late reply. I would love to get feedback on this!

Copy link
Collaborator

@bcarroll22 bcarroll22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome, thanks for tackling this! If you could please address those few things I mentioned we’ll get this merged ASAP!

@@ -10,12 +10,12 @@ import { getQueriesForElement, BoundFunction } from './get-queries-for-element';
declare const within: typeof getQueriesForElement;

interface Query extends Function {
(testRenderer: ReactTestRenderer | NativeTestInstance, ...args: any[]):
(testRenderer: ReactTestRenderer, ...args: any[]):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this may be backwards. A query only takes a NativeTestInstance, never a ReactTestRenderer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I just thought testRenderer should be ReactTestRenderer by seeing these changes.

manakuro@d8c3b30#diff-0da64c26f363af99c40bbae0dc3fb0bfR8

Should it be replaced with NativeTestInstance in typings/queries.d.ts like this ?

export type QueryByBoundProp = (
  testRenderer: NativeTestInstance,
  id: Matcher,
  options?: MatcherOptions,
) => NativeTestInstance | null;
...

and Query just takes NativeTestInstance ?

interface Query extends Function {
  (testRenderer: NativeTestInstance, ...args: any[]):
    | Error
    | Promise<NativeTestInstance[]>
    | Promise<NativeTestInstance>
    | NativeTestInstance[]
    | NativeTestInstance
    | null;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep this is right. And to make it more clear, we might want to change the name of the argument from testRenderer as a part of this as well 👍

@@ -4,12 +4,12 @@ import { Matcher, MatcherOptions } from './matches';

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

export interface SelectorMatcherOptions extends MatcherOptions {
export type SelectorMatcherOptions = Omit<MatcherOptions, 'selector'> & {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you help me understand what this one does?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous one tried to extend MatcherOptions and override selector property but TypeScript cannot override interface property. So to do that, you can use Omit to exclude selector and extend it again.

https://stackoverflow.com/questions/41285211/overriding-interface-property-type-defined-in-typescript-d-ts-file?answertab=votes#tab-top

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks 👍


type ReactTestInstance = {
getProp: (string) => NativeTestInstance;
getProp: (str: string) => NativeTestInstance;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this something more descriptive of what this is? Like name possibly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This is just a syntax error. You need to use type and name together.

https://www.typescriptlang.org/docs/handbook/functions.html#writing-the-function-type

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, can we make the name of the argument name so that it’s clear from the typings what getProp is looking for? ☺️

@mAAdhaTTah
Copy link
Contributor

This should also replace asFragment with asJSON, which is incorrect in the typings.

@bcarroll22
Copy link
Collaborator

@mAAdhaTTah in case this PR doesn’t address your concern or you need it faster, I’ll review one from you today that fixes it if you’d like?

@mAAdhaTTah
Copy link
Contributor

@bcarroll22 PR'd as #34 but we're going to need both of these PRs to get TS to stop complaining.

@bcarroll22
Copy link
Collaborator

@manakuro any ideas when we can wrap this one up?

@bcarroll22
Copy link
Collaborator

I'm going to go ahead and get this merged and make the few changes so that this change is working for others as well. Thanks for the help!

@allcontributors please add @manakuro for code.

@allcontributors
Copy link
Contributor

@bcarroll22

I've put up a pull request to add @manakuro! 🎉

@bcarroll22 bcarroll22 merged commit c6088ad into testing-library:master Jun 20, 2019
@bcarroll22
Copy link
Collaborator

🎉 This PR is included in version 4.0.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants