Skip to content

Commit

Permalink
Add prettier config, update required deps and run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
freiksenet committed Jan 25, 2022
1 parent ac98ccd commit e42eb28
Show file tree
Hide file tree
Showing 20 changed files with 1,224 additions and 1,140 deletions.
7 changes: 6 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@
"es6": true,
"node": true
},
"ignorePatterns": ["**/coverage", "**/lib", "**/temp"]
"ignorePatterns": ["**/coverage", "**/lib", "**/temp"],
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error"
},
"extends": ["plugin:prettier/recommended"]
}
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"trailingComma": "all"
}
4 changes: 4 additions & 0 deletions packages/nova-react-test-utils/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": ["../../.eslintrc.json"],
"root": true
}
44 changes: 22 additions & 22 deletions packages/nova-react-test-utils/src/nova-mock-environment.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import React from "react";
import type { ComponentType } from "react";
import {
NovaCentralizedCommanding,
NovaGraphQL
} from "@nova/types";
import {
MockFunctions
} from "@graphitation/apollo-mock-client";
import { NovaCentralizedCommanding, NovaGraphQL } from "@nova/types";
import { MockFunctions } from "@graphitation/apollo-mock-client";

import { GraphQLTaggedNode, NovaCentralizedCommandingProvider, NovaGraphQLProvider } from "@nova/react";
import {
GraphQLTaggedNode,
NovaCentralizedCommandingProvider,
NovaGraphQLProvider,
} from "@nova/react";

export interface NovaMockEnvironment {
commanding: jest.Mocked<NovaCentralizedCommanding>;
Expand All @@ -17,24 +16,25 @@ export interface NovaMockEnvironment {
* A React component that will be used to wrap the NovaFacadeProvider children. This is used by the test-utils to
* inject a ApolloProvider.
*/
providerWrapper: ComponentType;
providerWrapper: ComponentType;
}

interface NovaMockEnvironmentProviderProps {
environment: NovaMockEnvironment;
}

export const NovaMockEnvironmentProvider: React.FunctionComponent<NovaMockEnvironmentProviderProps> = ({
children,
environment,
}) => {
return (
<NovaCentralizedCommandingProvider commanding={environment.commanding}>
<NovaGraphQLProvider graphql={environment.graphql}>
{React.createElement(environment.providerWrapper, undefined, children)}
</NovaGraphQLProvider>
</NovaCentralizedCommandingProvider>
);
};
export const NovaMockEnvironmentProvider: React.FunctionComponent<NovaMockEnvironmentProviderProps> =
({ children, environment }) => {
return (
<NovaCentralizedCommandingProvider commanding={environment.commanding}>
<NovaGraphQLProvider graphql={environment.graphql}>
{React.createElement(
environment.providerWrapper,
undefined,
children,
)}
</NovaGraphQLProvider>
</NovaCentralizedCommandingProvider>
);
};
NovaMockEnvironmentProvider.displayName = "NovaMockEnvironmentProvider";

33 changes: 18 additions & 15 deletions packages/nova-react-test-utils/src/test-utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { buildASTSchema, parse } from "graphql";
import {
act,
create as createTestRenderer,
ReactTestRenderer
ReactTestRenderer,
} from "react-test-renderer";
import { EntityCommand } from "@nova/types";

Expand All @@ -13,9 +13,12 @@ import {
_createMockEnvironmentWithSchema,
MockPayloadGenerator,
getOperationName,
getOperationType
getOperationType,
} from "./test-utils";
import { NovaMockEnvironment, NovaMockEnvironmentProvider } from "./nova-mock-environment";
import {
NovaMockEnvironment,
NovaMockEnvironmentProvider,
} from "./nova-mock-environment";

const schema = buildASTSchema(
parse(`
Expand All @@ -36,7 +39,7 @@ const schema = buildASTSchema(
type Avatar {
url: String!
}
`)
`),
);

const QuerySubject: React.FC = () => {
Expand All @@ -48,7 +51,7 @@ const QuerySubject: React.FC = () => {
}
}
`,
{}
{},
);
return data ? <span>{data.user.name}</span> : null;
};
Expand All @@ -66,7 +69,7 @@ describe(_createMockEnvironmentWithSchema, () => {
tree = createTestRenderer(
<NovaMockEnvironmentProvider environment={environment}>
<div>42</div>
</NovaMockEnvironmentProvider>
</NovaMockEnvironmentProvider>,
);
});
const expectedProvider = tree!.root.findByType("div").parent
Expand All @@ -89,20 +92,20 @@ describe(_createMockEnvironmentWithSchema, () => {
tree = createTestRenderer(
<NovaMockEnvironmentProvider environment={environment}>
<QuerySubject />
</NovaMockEnvironmentProvider>
</NovaMockEnvironmentProvider>,
);
});

expect(environment.graphql.mock.getAllOperations().length).toEqual(1);

await act(async () =>
environment.graphql.mock.resolveMostRecentOperation(operation =>
MockPayloadGenerator.generate(operation)
)
environment.graphql.mock.resolveMostRecentOperation((operation) =>
MockPayloadGenerator.generate(operation),
),
);

expect(tree!.root.findByType("span").children).toEqual([
`<mock-value-for-field-"name">`
`<mock-value-for-field-"name">`,
]);
});
});
Expand All @@ -113,12 +116,12 @@ describe(_createMockEnvironmentWithSchema, () => {
createTestRenderer(
<NovaMockEnvironmentProvider environment={environment}>
<QuerySubject />
</NovaMockEnvironmentProvider>
</NovaMockEnvironmentProvider>,
);
});

expect(
getOperationName(environment.graphql.mock.getMostRecentOperation())
getOperationName(environment.graphql.mock.getMostRecentOperation()),
).toEqual("MockTestQuery");
});

Expand All @@ -127,12 +130,12 @@ describe(_createMockEnvironmentWithSchema, () => {
createTestRenderer(
<NovaMockEnvironmentProvider environment={environment}>
<QuerySubject />
</NovaMockEnvironmentProvider>
</NovaMockEnvironmentProvider>,
);
});

expect(
getOperationType(environment.graphql.mock.getMostRecentOperation())
getOperationType(environment.graphql.mock.getMostRecentOperation()),
).toEqual("query");
});
});
Expand Down
30 changes: 15 additions & 15 deletions packages/nova-react-test-utils/src/test-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import { buildClientSchema, DocumentNode, GraphQLSchema } from "graphql";

import {
createMockClient,
MockFunctions
MockFunctions,
} from "@graphitation/apollo-mock-client";
import * as GraphQLHooks from "@graphitation/apollo-react-relay-duct-tape";
import {
generate as payloadGenerator,
OperationDescriptor
OperationDescriptor,
} from "@graphitation/graphql-js-operation-payload-generator";

import { GraphQLTaggedNode } from "@nova/react";
import { NovaGraphQL } from "@nova/types";

type Generate<Schema, Node> = (
operation: OperationDescriptor<Schema, Node>,
mockResolvers?: Parameters<typeof payloadGenerator>[1]
mockResolvers?: Parameters<typeof payloadGenerator>[1],
) => ReturnType<typeof payloadGenerator>;

export const MockPayloadGenerator: {
Expand All @@ -30,27 +30,27 @@ export const MockPayloadGenerator: {
*/
generate: Generate<unknown, GraphQLTaggedNode>;
} = {
generate: payloadGenerator as Generate<any, any>
generate: payloadGenerator as Generate<any, any>,
};

/**
* Exported only for testing purposes. Use `createMockEnvironment()` instead.
*/
export function _createMockEnvironmentWithSchema(
schema: GraphQLSchema
schema: GraphQLSchema,
): NovaMockEnvironment {
const client = createMockClient(schema);
const env: NovaMockEnvironment = {
commanding: {
trigger: jest.fn()
trigger: jest.fn(),
},
graphql: {
...(GraphQLHooks as NovaGraphQL),
mock: client.mock as MockFunctions<any, any>
mock: client.mock as MockFunctions<any, any>,
},
providerWrapper: ({ children }) => (
<ApolloProvider client={client}>{children}</ApolloProvider>
)
),
};
return env;
}
Expand Down Expand Up @@ -89,21 +89,21 @@ export function createMockEnvironment() {
// TODO: This should eventually use the 1GQL shared schema.
// https://domoreexp.visualstudio.com/MSTeams/_workitems/edit/1763201
SCHEMA = buildClientSchema(
require("@msteams/data-schema/generated/server-schema.json")
require("@msteams/data-schema/generated/server-schema.json"),
);
}
return _createMockEnvironmentWithSchema(SCHEMA);
}

function getOperationDefinition(
operation: OperationDescriptor<unknown, GraphQLTaggedNode>
operation: OperationDescriptor<unknown, GraphQLTaggedNode>,
) {
const definition = _getOperationDefinition(
(operation.request.node as unknown) as DocumentNode
operation.request.node as unknown as DocumentNode,
);
if (!definition) {
throw new Error(
"Expected operation descriptor to contain a operation definition"
"Expected operation descriptor to contain a operation definition",
);
}
return definition;
Expand All @@ -114,12 +114,12 @@ function getOperationDefinition(
* @returns The name of the operation.
*/
export function getOperationName(
operation: OperationDescriptor<unknown, GraphQLTaggedNode>
operation: OperationDescriptor<unknown, GraphQLTaggedNode>,
) {
const name = getOperationDefinition(operation).name?.value;
if (!name) {
throw new Error(
"Expected operation descriptor to contain a named operation"
"Expected operation descriptor to contain a named operation",
);
}
return name;
Expand All @@ -130,7 +130,7 @@ export function getOperationName(
* @returns The type of the operation.
*/
export function getOperationType(
operation: OperationDescriptor<unknown, GraphQLTaggedNode>
operation: OperationDescriptor<unknown, GraphQLTaggedNode>,
): "query" | "mutation" | "subscription" {
return getOperationDefinition(operation).operation;
}
4 changes: 4 additions & 0 deletions packages/nova-react/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": ["../../.eslintrc.json"],
"root": true
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @jest-environment jsdom
*/

import React from "react";
import { render } from "@testing-library/react";
import {
Expand All @@ -21,7 +25,7 @@ describe(useNovaCentralizedCommanding, () => {
useNovaCentralizedCommanding();
} catch (e) {
expect((e as Error).message).toMatch(
"Nova Centralized Commanding provider must be initialized prior to consumption!"
"Nova Centralized Commanding provider must be initialized prior to consumption!",
);
}
return null;
Expand All @@ -33,9 +37,9 @@ describe(useNovaCentralizedCommanding, () => {
it("is able to access the commanding instance provided by the provider", () => {
expect.assertions(2);

const commanding = ({
const commanding = {
trigger: jest.fn(),
} as unknown) as NovaCentralizedCommanding;
} as unknown as NovaCentralizedCommanding;

const TestPassedContextComponent: React.FC = () => {
const facadeFromContext = useNovaCentralizedCommanding();
Expand All @@ -55,9 +59,9 @@ describe(useNovaCentralizedCommanding, () => {
};

render(
<NovaCentralizedCommandingProvider commanding={commanding} >
<NovaCentralizedCommandingProvider commanding={commanding}>
<TestPassedContextComponent />
</NovaCentralizedCommandingProvider>
</NovaCentralizedCommandingProvider>,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,31 @@ import type { NovaCentralizedCommanding } from "@nova/types";
import invariant from "invariant";

// Initializing default with null to make sure providers are correctly placed in the tree
const NovaCommandingContext = React.createContext<NovaCentralizedCommanding | null>(
null
);
const NovaCommandingContext =
React.createContext<NovaCentralizedCommanding | null>(null);

interface NovaCentralizedCommandingProviderProps {
commanding: NovaCentralizedCommanding;
}

export const NovaCentralizedCommandingProvider: React.FunctionComponent<NovaCentralizedCommandingProviderProps> = ({
children,
commanding,
}) => {
return (
<NovaCommandingContext.Provider value={commanding}>
{children}
</NovaCommandingContext.Provider>
);
};
NovaCentralizedCommandingProvider.displayName = "NovaCentralizedCommandingProvider";
export const NovaCentralizedCommandingProvider: React.FunctionComponent<NovaCentralizedCommandingProviderProps> =
({ children, commanding }) => {
return (
<NovaCommandingContext.Provider value={commanding}>
{children}
</NovaCommandingContext.Provider>
);
};
NovaCentralizedCommandingProvider.displayName =
"NovaCentralizedCommandingProvider";

export const useNovaCentralizedCommanding = (): NovaCentralizedCommanding => {
const commanding = React.useContext<NovaCentralizedCommanding | null>(
NovaCommandingContext
NovaCommandingContext,
);
invariant(
commanding,
"Nova Centralized Commanding provider must be initialized prior to consumption!"
"Nova Centralized Commanding provider must be initialized prior to consumption!",
);
return commanding;
};
Loading

0 comments on commit e42eb28

Please sign in to comment.