Skip to content

Commit

Permalink
fix: clean up tests, fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
acao committed Feb 14, 2020
1 parent fa5475c commit 9046803
Show file tree
Hide file tree
Showing 33 changed files with 122 additions and 101 deletions.
1 change: 0 additions & 1 deletion .yarnrc

This file was deleted.

25 changes: 12 additions & 13 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');
const { jsWithTs: tsjPreset } = require('ts-jest/presets');
const { jsWithBabel: jsWithBabelPreset } = require('ts-jest/presets');
// const { jsWithTs: tsjPreset } = require('ts-jest/presets');
// const { jsWithBabel: jsWithBabelPreset } = require('ts-jest/presets');

module.exports = {
globals: {
Expand All @@ -11,11 +11,7 @@ module.exports = {
clearMocks: true,
collectCoverage: true,
coverageDirectory: 'coverage/jest',
setupFiles: [path.join(__dirname, '/resources/test.config.js')],
testMatch: [
'<rootDir>/packages/*/src/**/*-test.{js,ts}',
'<rootDir>/packages/*/src/**/*.spec.{js,ts}',
],
setupFilesAfterEnv: [path.join(__dirname, '/resources/test.config.js')],
moduleNameMapper: {
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'identity-obj-proxy',
Expand All @@ -25,12 +21,13 @@ module.exports = {
'<rootDir>/packages/codemirror-graphql/src/$1',
'^example-([^/]+)': '<rootDir>/examples/$1/src',
},
transform: {
'^.+\\.jsx?$': require.resolve('./resources/jestBabelTransform'),
...tsjPreset.transform,
...jsWithBabelPreset.transform,
},
testEnvironment: require.resolve('jest-environment-jsdom-global'),
testMatch: [
'**/?(*.)+(spec|test).[jt]s?(x)',
'!**/cypress/**',
'**/?(*)-test.[jt]s?(x)',
'!**/cypress/**',
],
testEnvironment: require.resolve('jest-environment-jsdom'),
testPathIgnorePatterns: ['node_modules', 'dist', 'codemirror-graphql'],
collectCoverageFrom: [
'**/src/**/*.{js,jsx,ts,tsx}',
Expand All @@ -44,5 +41,7 @@ module.exports = {
'!**/examples/**',
'!**/codemirror-graphql/**',
'!**/graphql-language-service-types/**',
'!**/*.d.ts',
'!**/types.ts',
],
};
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@
"@commitlint/config-conventional": "^8.1.0",
"@commitlint/config-lerna-scopes": "^8.1.0",
"@strictsoftware/typedoc-plugin-monorepo": "^0.2.1",
"@testing-library/jest-dom": "^5.1.1",
"@types/codemirror": "^0.0.84",
"@types/fetch-mock": "^7.3.2",
"@types/jest": "^25.1.1",
"@typescript-eslint/parser": "^2.18.0",
"@types/codemirror": "^0.0.84",
"@types/node": "^13.7.1",
"@typescript-eslint/eslint-plugin": "^2.16.0",
"@typescript-eslint/parser": "^2.18.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^25.1.0",
"chai": "4.2.0",
"codecov": "^3.6.4",
"conventional-changelog-conventionalcommits": "^4.1.0",
Expand Down Expand Up @@ -107,7 +109,7 @@
"mocha": "7.0.1",
"prettier": "^1.18.2",
"rimraf": "^3.0.1",
"ts-jest": "^25.1.0",
"ts-jest": "^25.2.0",
"typedoc": "^0.15.6",
"typescript": "^3.6.3"
}
Expand Down
1 change: 1 addition & 0 deletions packages/graphiql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"@types/codemirror": "0.0.82",
"@types/markdown-it": "^0.0.9",
"@types/node": "^13.7.1",
"@types/testing-library__jest-dom": "^5.0.1",
"babel-plugin-macros": "^2.8.0",
"cross-env": "^7.0.0",
"css-loader": "3.4.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/graphiql/src/components/DocExplorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const initialNav: NavStackItem = {
};

type DocExplorerProps = {
schema?: GraphQLSchema;
schema?: GraphQLSchema | null;
};

type DocExplorerState = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type DefaultValueProps = {
};

export default function DefaultValue({ field }: DefaultValueProps) {
if ('defaultValue' in field) {
// field.defaultValue could be null or false, so be careful here!
if ('defaultValue' in field && field.defaultValue !== undefined) {
return (
<span>
{' = '}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import React from 'react';
import MD from 'markdown-it';
import Maybe from 'graphql/tsutils/Maybe';
import { Maybe } from '../../types';

const md = new MD();

Expand Down
2 changes: 1 addition & 1 deletion packages/graphiql/src/components/DocExplorer/TypeDoc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export default class TypeDoc extends React.Component<
);

const deprecatedFields = fields.filter(field =>
'isDeprecated' in field ? !field.isDeprecated : true,
'isDeprecated' in field ? field.isDeprecated : true,
);
if (deprecatedFields.length > 0) {
deprecatedFieldsDef = (
Expand Down
16 changes: 6 additions & 10 deletions packages/graphiql/src/components/DocExplorer/TypeLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,18 @@ import {
GraphQLType,
GraphQLNamedType,
} from 'graphql';
import Maybe from 'graphql/tsutils/Maybe';
import { OnClickTypeFunction } from './types';

import { Maybe } from '../../types';

type TypeLinkProps = {
type?: Maybe<GraphQLType>;
onClick: OnClickTypeFunction;
onClick?: OnClickTypeFunction;
};

export default class TypeLink extends React.Component<TypeLinkProps> {
shouldComponentUpdate(nextProps: TypeLinkProps) {
return this.props.type !== nextProps.type;
}

render() {
return renderType(this.props.type, this.props.onClick);
}
export default function TypeLink(props: TypeLinkProps) {
const onClick = props.onClick ? props.onClick : () => null;
return renderType(props.type, onClick);
}

function renderType(type: Maybe<GraphQLType>, onClick: OnClickTypeFunction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import React from 'react';
import { render } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';

import FieldDoc from '../FieldDoc';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';

import { GraphQLString } from 'graphql';

Expand All @@ -23,6 +22,7 @@ import {
describe('TypeDoc', () => {
it('renders a top-level query object type', () => {
const { container } = render(
// @ts-ignore
<TypeDoc
schema={ExampleSchema}
type={ExampleQuery}
Expand Down Expand Up @@ -61,6 +61,7 @@ describe('TypeDoc', () => {

it('renders deprecated fields when you click to see them', () => {
const { container } = render(
// @ts-ignore
<TypeDoc
schema={ExampleSchema}
type={ExampleQuery}
Expand All @@ -84,6 +85,7 @@ describe('TypeDoc', () => {

it('renders a Union type', () => {
const { container } = render(
// @ts-ignore
<TypeDoc schema={ExampleSchema} type={ExampleUnion} />,
);
expect(container.querySelector('.doc-category-title')).toHaveTextContent(
Expand All @@ -93,6 +95,7 @@ describe('TypeDoc', () => {

it('renders an Enum type', () => {
const { container } = render(
// @ts-ignore
<TypeDoc schema={ExampleSchema} type={ExampleEnum} />,
);
expect(container.querySelector('.doc-category-title')).toHaveTextContent(
Expand All @@ -105,6 +108,7 @@ describe('TypeDoc', () => {

it('shows deprecated enum values on click', () => {
const { getByText, container } = render(
// @ts-ignore
<TypeDoc schema={ExampleSchema} type={ExampleEnum} />,
);
const showBtn = getByText('Show deprecated values...');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';

import TypeLink from '../TypeLink';

Expand Down
14 changes: 7 additions & 7 deletions packages/graphiql/src/components/GraphiQL.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import debounce from '../utility/debounce';
import find from '../utility/find';
import { GetDefaultFieldNamesFn, fillLeafs } from '../utility/fillLeafs';
import { getLeft, getTop } from '../utility/elementPosition';
import { mergeAST } from '../utility/mergeAst';
import mergeAST from '../utility/mergeAst';
import {
introspectionQuery,
introspectionQueryName,
Expand Down Expand Up @@ -180,7 +180,7 @@ export class GraphiQL extends React.Component<GraphiQLProps, GraphiQLState> {
const query =
props.query !== undefined
? props.query
: this._storage.get('query') !== undefined
: this._storage.get('query')
? (this._storage.get('query') as string)
: props.defaultQuery !== undefined
? props.defaultQuery
Expand All @@ -201,7 +201,7 @@ export class GraphiQL extends React.Component<GraphiQLProps, GraphiQLState> {
? props.operationName
: getSelectedOperationName(
undefined,
this._storage.get('operationName'),
this._storage.get('operationName') as string,
queryFacts && queryFacts.operations,
);

Expand All @@ -223,7 +223,7 @@ export class GraphiQL extends React.Component<GraphiQLProps, GraphiQLState> {
this.state = {
schema: props.schema,
query,
variables,
variables: variables as string,
operationName,
docExplorerOpen,
response: props.response,
Expand Down Expand Up @@ -1113,14 +1113,14 @@ export class GraphiQL extends React.Component<GraphiQLProps, GraphiQLState> {
}
};

private handleResizeStart: MouseEventHandler<HTMLDivElement> = downEvent => {
private handleResizeStart = (downEvent: React.MouseEvent) => {
if (!this._didClickDragBar(downEvent)) {
return;
}

downEvent.preventDefault();

const offset = downEvent.clientX - getLeft(downEvent.currentTarget);
const offset = downEvent.clientX - getLeft(downEvent.target as HTMLElement);

let onMouseMove: OnMouseMoveFn = moveEvent => {
if (moveEvent.buttons === 0) {
Expand Down Expand Up @@ -1153,7 +1153,7 @@ export class GraphiQL extends React.Component<GraphiQLProps, GraphiQLState> {
if (event.button !== 0 || event.ctrlKey) {
return false;
}
let target = event.currentTarget || (event.target as Element);
let target = event.target as Element;
// We use codemirror's gutter as the drag bar.
if (target.className.indexOf('CodeMirror-gutter') !== 0) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
import React from 'react';
import { render } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import { DocExplorer } from '../DocExplorer';

import { ExampleSchema } from './ExampleSchema';
Expand Down
2 changes: 0 additions & 2 deletions packages/graphiql/src/components/__tests__/GraphiQL.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*/
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';

import { GraphiQL, Fetcher } from '../GraphiQL';
import { getMockStorage } from './helpers/storage';
import { codeMirrorModules } from './helpers/codeMirror';
Expand Down
2 changes: 2 additions & 0 deletions packages/graphiql/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export namespace GraphiQL {
export namespace CodeMirror {
export type ShowHintOptions = HintOptions & { container: HTMLElement | null };
}

export type Maybe<T> = T | null | undefined;
2 changes: 1 addition & 1 deletion packages/graphiql/src/utility/CodeMirrorSizer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import CodeMirror from 'codemirror';
import Maybe from 'graphql/tsutils/Maybe';
import { Maybe } from '../types';

export interface SizerComponent {
getClientHeight: () => number | null;
Expand Down
5 changes: 3 additions & 2 deletions packages/graphiql/src/utility/StorageAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,20 @@ export default class StorageAPI {
storage || (typeof window !== 'undefined' ? window.localStorage : null);
}

get(name: string) {
get(name: string): string | null {
if (this.storage) {
const value = this.storage.getItem('graphiql:' + name);
// Clean up any inadvertently saved null/undefined values.
if (value === 'null' || value === 'undefined') {
this.storage.removeItem('graphiql:' + name);
return;
return null;
}

if (value) {
return value;
}
}
return null;
}

set(name: string, value: string) {
Expand Down
Loading

0 comments on commit 9046803

Please sign in to comment.