Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. import { access } from 'fs'
  2.  
  3. // given types:
  4. /*
  5. type Post {
  6. title: String!
  7. comments: [Comment!]!
  8. }
  9.  
  10. type Comment {
  11. author: String!
  12. authorAvatar: String
  13. likes: Int!
  14. }
  15.  
  16. type CreatePostResponse {
  17. post: Post!
  18. }
  19.  
  20. */
  21.  
  22.  
  23. // create:
  24.  
  25.  
  26.  
  27. type MockPostRequirements = {
  28. title?():string;
  29. comments(): Array<ReturnType<typeof mockComment>>;
  30. }
  31.  
  32. const mockPost = (fieldGens:MockPostRequirements) => {
  33. const fields = Object.keys(fieldGens).reduce((acc,key) => ({
  34. ...acc,
  35. [key]: fieldGens[key]()
  36. }),{});
  37.  
  38. return {
  39. title: 'faker.string',
  40. ...fields,
  41. }
  42. };
  43.  
  44. const mockComment = () => {
  45. return {
  46. author: 'faker.string',
  47. authorAvatar: 'faker.string',
  48. likes: 'faker.int'
  49. }
  50. }
  51.  
  52. const post = mockPost({
  53. comments: () => [mockComment(), mockComment()]
  54. })
  55.  
  56.  
  57. const mockGetPosts = (variables, resultFields) => {
  58. return {
  59. query: GetPostsDocument,
  60. variables,
  61. result: resultFields,
  62. }
  63. }
  64.  
  65. const commentA = mockComment();
  66. const commentB = mockComment();
  67.  
  68. const postA = mockPost({
  69. title: 'post A',
  70. comments: [commentA, commentB]
  71. });
  72.  
  73. const postB = mockPost({
  74. title: 'post B',
  75. comments: [commentA, commentB]
  76. });
  77.  
  78. const posts = [postA, postB];
  79.  
  80. const query = mockGetPosts(variables, { posts, });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement