graphql-mock
Advanced tools
Comparing version 0.2.0 to 0.2.1
import { ApolloClient } from 'apollo-client'; | ||
import { NormalizedCacheObject } from 'apollo-cache-inmemory'; | ||
export default function MockClient(typeDefs: string, mocks?: any): ApolloClient<NormalizedCacheObject>; | ||
import { GraphQLSchema } from 'graphql'; | ||
export default function MockClient(typeDefs: string | GraphQLSchema, mocks?: any, resolvers?: any): ApolloClient<NormalizedCacheObject>; |
@@ -7,4 +7,4 @@ "use strict"; | ||
const graphql_tools_1 = require("graphql-tools"); | ||
function MockClient(typeDefs, mocks = {}) { | ||
const schema = graphql_tools_1.makeExecutableSchema({ typeDefs }); | ||
function MockClient(typeDefs, mocks = {}, resolvers) { | ||
const schema = typeof typeDefs === 'string' ? graphql_tools_1.makeExecutableSchema({ typeDefs, resolvers }) : typeDefs; | ||
graphql_tools_1.addMockFunctionsToSchema({ schema, mocks }); | ||
@@ -11,0 +11,0 @@ const cache = new apollo_cache_inmemory_1.InMemoryCache(window.__APOLLO_STATE__); |
@@ -5,2 +5,3 @@ export interface Mock { | ||
data?: any; | ||
error?: any; | ||
} | ||
@@ -13,3 +14,4 @@ export default class Expectations { | ||
reply(data: any): this; | ||
fail(error: any | any[] | string): this; | ||
forQuery(query: string): any | void; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const apollo_client_1 = require("apollo-client"); | ||
const utils_1 = require("./utils"); | ||
@@ -26,8 +27,17 @@ class Expectations { | ||
} | ||
fail(error) { | ||
const lastEntry = this.mocks[this.mocks.length - 1]; | ||
const errors = typeof error === 'string' ? [{ message: error }] : error; | ||
lastEntry.data = {}; | ||
lastEntry.error = error instanceof apollo_client_1.ApolloError ? error : new apollo_client_1.ApolloError({ | ||
graphQLErrors: Array.isArray(errors) ? errors : [errors] | ||
}); | ||
return this; | ||
} | ||
forQuery(query) { | ||
const normalizedQuery = utils_1.normalize(query); | ||
const entry = this.mocks.find(({ query }) => query === normalizedQuery); | ||
return entry ? entry.data : undefined; | ||
return entry ? { data: entry.data, error: entry.error, networkStatus: entry.error ? 'error' : 'ready' } : undefined; | ||
} | ||
} | ||
exports.default = Expectations; |
import { ApolloClient } from 'apollo-client'; | ||
import { NormalizedCacheObject } from 'apollo-cache-inmemory'; | ||
import Expectations from './expect'; | ||
import { GraphQLSchema } from 'graphql'; | ||
export * from './utils'; | ||
@@ -13,3 +14,3 @@ export interface GQLRequest { | ||
expectations: Expectations; | ||
constructor(schema: string, mocks?: object); | ||
constructor(schema: string | GraphQLSchema, mocks?: object, resolvers?: any); | ||
reset(): void; | ||
@@ -16,0 +17,0 @@ readonly queries: string[]; |
@@ -20,6 +20,6 @@ "use strict"; | ||
class GraphQLMock { | ||
constructor(schema, mocks = {}) { | ||
constructor(schema, mocks = {}, resolvers) { | ||
this.requests = []; | ||
this.expectations = new expect_1.default(); | ||
this.client = client_1.default(schema, mocks); | ||
this.client = client_1.default(schema, mocks, resolvers); | ||
this.patchClient(); | ||
@@ -57,3 +57,3 @@ } | ||
if (mockedResponse) { | ||
response.currentResult = () => ({ data: mockedResponse }); | ||
response.currentResult = () => (Object.assign({}, mockedResponse, { loading: false })); | ||
} | ||
@@ -60,0 +60,0 @@ } |
{ | ||
"name": "graphql-mock", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "GraphQL endpoint mockery library for testing", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -109,3 +109,3 @@ # GraphQL Client Side Mocking | ||
const schema = makeExecutableSchema({ typeDefs }); | ||
const schema = makeExecutableSchema({ typeDefs, resolvers }); | ||
const mock = new GraphqlMock(schema); | ||
@@ -112,0 +112,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11739
184