All files / app/stories serviceMocks.ts

0% Statements 0/38
0% Branches 0/4
0% Functions 0/15
0% Lines 0/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127                                                                                                                                                                                                                                                             
import { service as originalService } from "service/index";
import comments from "test/fixtures/comments.json";
import groupChat from "test/fixtures/groupChat.json";
import messages from "test/fixtures/messages.json";
import notifications from "test/fixtures/notifications.json";
import users from "test/fixtures/users.json";
 
const [user1, user2, user3, user4] = users;
 
const userMap = {
  "1": user1,
  "2": user2,
  "3": user3,
  "4": user4,
  funnycat: user1,
  funnyChicken: user4,
  funnydog: user2,
  funnykid: user3,
};
 
const serviceStubs = Object.keys(originalService).reduce(
  (serviceStub: Record<string, unknown>, serviceKey) => {
    serviceStub[serviceKey] = {};
    return serviceStub;
  },
  {}
);
 
export const mockedService = {
  ...serviceStubs,
  api: {
    listFriends: () => Promise.resolve([users[1].userId, users[2].userId]),
  },
  conversations: {
    getGroupChat: () => Promise.resolve(groupChat),
    getGroupChatMessages: () => Promise.resolve([messages[0], messages[1]]),
    listGroupChats: () =>
      Promise.resolve({
        groupChatsList: [groupChat],
        noMore: true,
      }),
  },
  notifications: {
    getNotificationSettings: () => Promise.resolve(notifications),
  },
  resources: {
    getCommunityGuidelines: () =>
      Promise.resolve({
        communityGuidelinesList: [
          {
            title: "Guideline 1",
            guideline:
              "Follow guideline 1. Follow it very carefully. It is important.",
            iconSvg:
              '<svg viewBox="0 0 24 24"><circle cx="12" cy="12" r="9" stroke="green" stroke-width="4" fill="yellow" /></svg>',
          },
          {
            title: "Guideline 2",
            guideline:
              "Follow guideline 2. Follow it very carefully. It is important.",
            iconSvg:
              '<svg viewBox="0 0 24 24"><circle cx="12" cy="12" r="9" stroke="green" stroke-width="4" fill="yellow" /></svg>',
          },
        ],
      }),
  },
  threads: {
    getThread: () =>
      Promise.resolve({ nextPageToken: "", repliesList: comments.slice(0, 4) }),
  },
  user: {
    getUser: (id: string) => {
      const result = userMap[id as keyof typeof userMap];
      return Promise.resolve(result);
    },
  },
} as unknown as typeof originalService;
 
function wait(milliSeconds: number) {
  return new Promise((resolve) => setTimeout(resolve, milliSeconds));
}
 
export const service = new Proxy(
  {},
  {
    get(target: unknown, serviceName: PropertyKey): unknown {
      return new Proxy(
        {},
        {
          get(target: unknown, methodName: PropertyKey): unknown {
            const serviceMethod =
              //eslint-disable-next-line
              (mockedService as Record<PropertyKey, any>)[serviceName] &&
              //eslint-disable-next-line
              (mockedService as Record<PropertyKey, Record<PropertyKey, any>>)[
                serviceName
              ][methodName];
            if (serviceMethod) {
              return async (...args: unknown[]) => {
                console.log(
                  `Service method '${String(serviceName)}.${String(
                    methodName
                  )}' is called with args:`,
                  ...args
                );
                await wait(1e3);
                const result = await serviceMethod(...args);
                console.log("Result: ", result);
                return Promise.resolve(result);
              };
            } else {
              return () => {
                console.warn(
                  `Service method '${String(serviceName)}.${String(
                    methodName
                  )}' is called. You should probably mock it.`
                );
                return Promise.resolve();
              };
            }
          },
        }
      );
    },
  }
);