Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { access } from 'fs'
- // given types:
- /*
- type Post {
- title: String!
- comments: [Comment!]!
- }
- type Comment {
- author: String!
- authorAvatar: String
- likes: Int!
- }
- type CreatePostResponse {
- post: Post!
- }
- */
- // create:
- type MockPostRequirements = {
- title?():string;
- comments(): Array<ReturnType<typeof mockComment>>;
- }
- const mockPost = (fieldGens:MockPostRequirements) => {
- const fields = Object.keys(fieldGens).reduce((acc,key) => ({
- ...acc,
- [key]: fieldGens[key]()
- }),{});
- return {
- title: 'faker.string',
- ...fields,
- }
- };
- const mockComment = () => {
- return {
- author: 'faker.string',
- authorAvatar: 'faker.string',
- likes: 'faker.int'
- }
- }
- const post = mockPost({
- comments: () => [mockComment(), mockComment()]
- })
- const mockGetPosts = (variables, resultFields) => {
- return {
- query: GetPostsDocument,
- variables,
- result: resultFields,
- }
- }
- const commentA = mockComment();
- const commentB = mockComment();
- const postA = mockPost({
- title: 'post A',
- comments: [commentA, commentB]
- });
- const postB = mockPost({
- title: 'post B',
- comments: [commentA, commentB]
- });
- const posts = [postA, postB];
- const query = mockGetPosts(variables, { posts, });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement