Skip to content

Commit

Permalink
Added tests for Service API
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickroberts committed Oct 22, 2020
1 parent c011e0b commit ef2ea8a
Show file tree
Hide file tree
Showing 13 changed files with 830 additions and 161 deletions.
15 changes: 13 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
"react-hooks"
],
"rules": {
"@typescript-eslint/consistent-type-definitions": ["error", "interface"],
"@typescript-eslint/consistent-type-definitions": [
"error",
"interface"
],
"@typescript-eslint/member-delimiter-style": "error",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-non-null-assertion": "off",
Expand Down Expand Up @@ -58,12 +61,20 @@
}
],
"react/jsx-props-no-spreading": "off",
"react/state-in-constructor": [
"error",
"never"
],
"semi": "off"
},
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".ts", ".tsx"]
"extensions": [
".js",
".ts",
".tsx"
]
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "suspense-service",
"version": "0.0.5-rc",
"version": "0.1.0",
"description": "Suspense integration library for React",
"repository": "github:patrickroberts/suspense-service",
"main": "dst/cjs/suspense-service.js",
Expand Down
106 changes: 0 additions & 106 deletions src/Context.test.tsx

This file was deleted.

49 changes: 0 additions & 49 deletions src/Context/Environment.test.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/Service/PromiseState.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @ignore */
interface PromiseStatePending {
promise: Promise<void>;
interface PromiseStatePending<TResponse> {
promise: Promise<TResponse>;
status: 'pending';
}

Expand All @@ -18,7 +18,7 @@ interface PromiseStateRejected {

/** @ignore */
type PromiseState<TResponse> =
PromiseStatePending |
PromiseStatePending<TResponse> |
PromiseStateFulfilled<TResponse> |
PromiseStateRejected;

Expand Down
4 changes: 4 additions & 0 deletions src/Service/useThenable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ export default function useThenable<TResponse>(
if (state.status === 'pending' && state.promise === promise) {
ref.current = { value, status: 'fulfilled' };
}

return value;
},
(reason) => {
const state = ref.current!;

if (state.status === 'pending' && state.promise === promise) {
ref.current = { reason, status: 'rejected' };
}

throw reason;
},
);

Expand Down
30 changes: 30 additions & 0 deletions src/__fixtures__/ErrorBoundary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ReactNode, Component } from 'react';

interface ErrorBoundaryProps {
fallback: ReactNode;
onError: (error: Error) => void;
}

interface ErrorBoundaryState {
hasError: boolean;
}

export default class ErrorBoundary extends Component<ErrorBoundaryProps> {
state: ErrorBoundaryState = { hasError: false };

static getDerivedStateFromError(): ErrorBoundaryState {
return { hasError: true };
}

componentDidCatch(error: Error): void {
const { onError } = this.props;
onError(error);
}

render(): ReactNode {
const { fallback, children } = this.props;
const { hasError } = this.state;

return hasError ? fallback : children;
}
}
9 changes: 9 additions & 0 deletions src/__fixtures__/mockRender.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React, { ReactNode } from 'react';

const mockRender = jest.fn((value: ReactNode) => (
<span>
{value}
</span>
));

export default mockRender;
13 changes: 13 additions & 0 deletions src/__fixtures__/withContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React, { ComponentType, ReactNode } from 'react';
import Id from '../Context/Id';
import Context, { useContext } from '../Context';

const withContext: (
context: Context<ReactNode>
) => ComponentType<{ id?: Id }> = (context) => ({ id = null }) => (
<div>
{useContext(context, id)}
</div>
);

export default withContext;
13 changes: 13 additions & 0 deletions src/__fixtures__/withService.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React, { ComponentType, ReactNode } from 'react';
import Id from '../Context/Id';
import Service, { useService } from '../Service';

const withService: (
service: Service<any, ReactNode>
) => ComponentType<{ id?: Id }> = (service) => ({ id = null }) => (
<div>
{useService(service, id)}
</div>
);

export default withService;
Loading

0 comments on commit ef2ea8a

Please sign in to comment.