graphql-mock
Advanced tools
Comparing version 0.0.2 to 0.0.3
import { ApolloClient } from 'apollo-client'; | ||
import { NormalizedCacheObject } from "apollo-cache-inmemory"; | ||
export default function MockClient(typeDefs: string, mocks?: object): ApolloClient<NormalizedCacheObject>; | ||
import { NormalizedCacheObject } from 'apollo-cache-inmemory'; | ||
export default function MockClient(typeDefs: string, mocks?: any): ApolloClient<NormalizedCacheObject>; |
@@ -9,3 +9,3 @@ "use strict"; | ||
const schema = graphql_tools_1.makeExecutableSchema({ typeDefs }); | ||
graphql_tools_1.addMockFunctionsToSchema({ schema: schema, mocks }); | ||
graphql_tools_1.addMockFunctionsToSchema({ schema, mocks }); | ||
const cache = new apollo_cache_inmemory_1.InMemoryCache(window.__APOLLO_STATE__); | ||
@@ -12,0 +12,0 @@ const link = new apollo_link_schema_1.SchemaLink({ schema }); |
import { ApolloClient } from 'apollo-client'; | ||
import { NormalizedCacheObject } from "apollo-cache-inmemory"; | ||
import { NormalizedCacheObject } from 'apollo-cache-inmemory'; | ||
import Expectations from './expect'; | ||
export * from './utils'; | ||
export interface GQLRequest { | ||
variables: any; | ||
query: string; | ||
} | ||
export default class GraphQLMock { | ||
client: ApolloClient<NormalizedCacheObject>; | ||
requests: GQLRequest[]; | ||
expectations: Expectations; | ||
constructor(schema: string, mocks?: object); | ||
readonly requests: any[]; | ||
reset(): void; | ||
readonly queries: string[]; | ||
readonly lastRequest: GQLRequest | void; | ||
readonly lastQuery: string | void; | ||
expect(query: string): Expectations; | ||
private patchClient(); | ||
} |
"use strict"; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) | ||
t[p[i]] = s[p[i]]; | ||
return t; | ||
}; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const client_1 = require("./client"); | ||
const expect_1 = require("./expect"); | ||
const utils_1 = require("./utils"); | ||
__export(require("./utils")); | ||
class GraphQLMock { | ||
constructor(schema, mocks = {}) { | ||
this.requests = []; | ||
this.expectations = new expect_1.default(); | ||
this.client = client_1.default(schema, mocks); | ||
this.patchClient(); | ||
} | ||
get requests() { | ||
return []; | ||
reset() { | ||
this.requests = []; | ||
this.expectations.reset(); | ||
} | ||
@@ -14,3 +33,33 @@ get queries() { | ||
} | ||
get lastRequest() { | ||
return this.requests[this.requests.length - 1]; | ||
} | ||
get lastQuery() { | ||
const request = this.lastRequest; | ||
if (request) { | ||
const { query, variables = {} } = request; | ||
return utils_1.fillIn(query, variables); | ||
} | ||
} | ||
expect(query) { | ||
return this.expectations.expect(query); | ||
} | ||
patchClient() { | ||
['query', 'watchQuery', 'mutate'].forEach(name => { | ||
const original = this.client[name]; | ||
this.client[name] = (_a) => { | ||
var { query, variables } = _a, rest = __rest(_a, ["query", "variables"]); | ||
const response = original.call(this, Object.assign({ query, variables }, rest)); | ||
if (query) { | ||
this.requests.push({ query: utils_1.stringify(query), variables }); | ||
const mockedResponse = this.expectations.forQuery(query); | ||
if (mockedResponse) { | ||
response.currentResult = () => ({ data: mockedResponse }); | ||
} | ||
} | ||
return response; | ||
}; | ||
}); | ||
} | ||
} | ||
exports.default = GraphQLMock; |
import * as parser from 'graphql/language/parser'; | ||
export declare const stringify: (query: object) => string; | ||
import * as printer from 'graphql/language/printer'; | ||
export declare const parse: typeof parser.parse; | ||
export declare const normalize: (query: string) => string; | ||
export declare const stringify: typeof printer.print; | ||
export declare const normalize: (query: string | object) => string; | ||
export declare const fillIn: (query: string, variables?: any) => string; |
@@ -5,4 +5,15 @@ "use strict"; | ||
const printer = require("graphql/language/printer"); | ||
exports.stringify = (query) => printer.print(query).trim(); | ||
exports.parse = parser.parse; | ||
exports.normalize = (query) => exports.stringify(exports.parse(query)); | ||
exports.stringify = printer.print; | ||
exports.normalize = (query) => { | ||
const queryObject = typeof (query) === 'string' ? exports.parse(query) : query; | ||
return exports.stringify(queryObject); | ||
}; | ||
// TODO: make a more serious implementation of this | ||
exports.fillIn = (query, variables = {}) => { | ||
let fullQuery = `${query}`; | ||
Object.keys(variables).forEach(key => { | ||
fullQuery = fullQuery.replace(`$${key}`, JSON.stringify(variables[key])); | ||
}); | ||
return fullQuery; | ||
}; |
{ | ||
"name": "graphql-mock", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "GraphQL endpoint mockery library for testing", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -80,5 +80,13 @@ # GraphQL Client Side Mocking | ||
`#client` -> a reference to the underline ApolloClient instance | ||
`#reset()` -> to reset all the mocks and queries history | ||
`#queries` -> the list of (string and normalized) queries that sent to the endpoint | ||
`#lastQuery` -> returns the last query that sent | ||
`#requests` -> requests (queries + variables) that sent to the server | ||
`#lastRequest` -> return the last request that sent | ||
`#expect(query: string)` -> an API to mock the exact responses | ||
@@ -85,0 +93,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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
10104
170
108
1