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

breaking: Migrate codebase to typescript #101

Merged
merged 28 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5a7af8a
refactor: Migrate codebase to typescript
Ne3l Aug 15, 2022
7059af2
fix test
Ne3l Aug 15, 2022
b7545bb
use node14
Ne3l Aug 15, 2022
225d0e2
fix rebase
Ne3l Sep 11, 2022
e25a27c
Add feedback
Ne3l Sep 11, 2022
b6e868a
fix test
Ne3l Sep 11, 2022
17c5539
use StyleSheet
Ne3l Sep 11, 2022
101a212
fix rebase to-have-styles
Ne3l Sep 18, 2022
5252b75
fix tests
Ne3l Sep 18, 2022
59c41a8
Simplify code
Ne3l Sep 18, 2022
87184c0
Apply feedback
Ne3l Sep 18, 2022
a105a87
fix rebase d38fc801b0bd01934166cd50634cdb9530a64a4a
Ne3l Sep 26, 2022
470b57d
use RNTL tsconfig
Ne3l Sep 26, 2022
34c2d0b
change ts-ignore with ts-expect-error.
Ne3l Sep 26, 2022
487079f
Fix test
Ne3l Sep 26, 2022
9928fb2
refactor: tweak export matcher types
mdjastrzebski Sep 28, 2022
785a4e7
refactor: cleanup toHaveStyle
mdjastrzebski Sep 28, 2022
9a4c58a
chore: vlaidate types on CI
mdjastrzebski Sep 28, 2022
7dd9d6d
refactor: disable expected type errors
mdjastrzebski Sep 28, 2022
c738e45
refactor: re-apply types to toHaveStyle from main branch
mdjastrzebski Sep 28, 2022
cf7ea72
refactor: apply type assertions
mdjastrzebski Sep 28, 2022
0aa0f9d
refactor: reorder imports
mdjastrzebski Sep 28, 2022
071b059
refactor: re-order
mdjastrzebski Sep 28, 2022
bce1a22
refactor: restore `getType()`
mdjastrzebski Sep 28, 2022
fef4cfc
refactor: review logic changes
mdjastrzebski Sep 28, 2022
821615b
refactor: restore output to dist folder
mdjastrzebski Sep 28, 2022
5cf066e
fix: not.toBeEnabled == toBeDisabled.
mdjastrzebski Sep 29, 2022
bb67f0f
refactor: fix undefined StyleSheet.flatten() result handling
mdjastrzebski Sep 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/__tests__/to-contain-element.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ test('.toContainElement negative test cases', () => {
expect(() => expect(grandparent).toContainElement(nonExistantElement)).toThrow();
expect(() => expect(nonExistantElement).toContainElement(nonExistantElement)).toThrow();

// @ts-expect-error intentionally passing incorrec type
// @ts-expect-error intentionally passing incorrect type
expect(() => expect(nonExistantElement).toContainElement(fakeElement)).toThrow();
expect(() => expect(fakeElement).toContainElement(nonExistantElement)).toThrow();
expect(() => expect(fakeElement).not.toContainElement(nonExistantElement)).toThrow();
expect(() => expect(fakeElement).toContainElement(grandparent)).toThrow();

// @ts-expect-error intentionally passing incorrec type
// @ts-expect-error intentionally passing incorrect type
expect(() => expect(grandparent).toContainElement(fakeElement)).toThrow();

// @ts-expect-error intentionally passing incorrec type
// @ts-expect-error intentionally passing incorrect type
expect(() => expect(fakeElement).toContainElement(fakeElement)).toThrow();
expect(() => expect(grandparent).not.toContainElement(child)).toThrow();
expect(() => expect(grandparent).not.toContainElement(textElement)).toThrow();

// @ts-expect-error intentionally passing incorrec type
// @ts-expect-error intentionally passing incorrect type
expect(() => expect(grandparent).not.toContainElement(undefined)).toThrow();
});
2 changes: 1 addition & 1 deletion src/to-be-disabled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function toBeDisabled(this: jest.MatcherContext, element: ReactTestInstan
export function toBeEnabled(this: jest.MatcherContext, element: ReactTestInstance) {
mdjastrzebski marked this conversation as resolved.
Show resolved Hide resolved
checkReactElement(element, toBeEnabled, this);

const isEnabled = !(isElementDisabled(element) || isAncestorDisabled(element));
const isEnabled = !isElementDisabled(element);

return {
pass: isEnabled,
Expand Down
13 changes: 6 additions & 7 deletions src/to-have-text-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ function collectNormalizedText(element: ReactTestInstance) {
return normalize(childrenText);
}

function collectChildrenText(element: string | ReactTestInstance): string[] {
function collectChildrenText(element: ReactTestInstance | string): string[] {
if (typeof element === 'string') return [element];
if (!element || !element.children) return [''];
if (!element || !element.children) return [];

let result: string[] = [];

for (const child of element.children) {
result = result.concat(collectChildrenText(child));
}
const result: string[] = [];
element.children.forEach((child) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

out of curiosity, why the forEach instead of the for of?

Copy link
Collaborator

Choose a reason for hiding this comment

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

No particular reason beside subjectively slightly easier to read.

result.push(...collectChildrenText(child));
});

return result;
}
Expand Down
17 changes: 8 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ const VALID_ELEMENTS = [
];

class ReactElementTypeError extends Error {
constructor(
received: ReactTestInstance | null | undefined,
matcherFn: jest.CustomMatcher,
context: jest.MatcherContext,
) {
constructor(received: unknown, matcherFn: jest.CustomMatcher, context: jest.MatcherContext) {
super();

/* istanbul ignore next */
Expand All @@ -43,6 +39,7 @@ class ReactElementTypeError extends Error {
} catch (e) {
// Deliberately empty.
}

/* istanbul ignore next */
this.message = [
matcherHint(`${context.isNot ? '.not' : ''}.${matcherFn.name}`, 'received', ''),
Expand All @@ -62,7 +59,7 @@ function checkReactElement(
throw new ReactElementTypeError(element, matcherFn, context);
}

// @ts-expect-error not sure where to get this fiber property
// @ts-expect-error internal _fiber property of ReactTestInstance
if (!element._fiber && !VALID_ELEMENTS.includes(element.type.toString())) {
throw new ReactElementTypeError(element, matcherFn, context);
}
Expand All @@ -73,8 +70,11 @@ function getType({ type }: ReactTestInstance) {
return type.displayName || type.name || type;
}

function printElement(element: ReactTestInstance | null | undefined) {
if (!element) return '';
function printElement(element: ReactTestInstance | null) {
if (element == null) {
return 'null';
}

return ` ${prettyFormat(
{ props: element.props },
{
Expand Down Expand Up @@ -152,5 +152,4 @@ export {
normalize,
isEmpty,
printElement,
display,
};